题目链接:

题目

E. New Reform

time limit per test 1 second

memory limit per test 256 megabytes

inputstandard input

outputstandard output

问题描述

Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.

The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).

In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.

Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.

输入

The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).

Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road.

It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.

输出

Print a single integer — the minimum number of separated cities after the reform.

样例

input

4 3

2 1

1 3

4 3

output

1

题意

给你一个无向图,现在要把双向边变成有向边,问使得入度为零的边最小的方案

题解

对每个连通分量求环

如果存在环,则这个连通分量的贡献为1

否则,这个连通分量的贡献为0

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
using namespace std; const int maxn=1e5+10;
int n,m; vector<int> G[maxn]; int vis[maxn];
bool dfs(int u,int fa){
vis[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
if(vis[v]||dfs(v,u)) return true;
}
return false;
} int main(){
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
while(m--){
int u,v;
scanf("%d%d",&u,&v),u--,v--;
G[u].push_back(v);
G[v].push_back(u);
}
int ans=0;
for(int i=0;i<n;i++){
if(!vis[i]){
if(!dfs(i,-1)) ans++;
}
}
printf("%d\n",ans);
return 0;
}

Codeforces Round #346 (Div. 2) E - New Reform 无相图求环的更多相关文章

  1. Codeforces Round #346 (Div. 2) E. New Reform dfs

    E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...

  2. Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #346 (Div. 2) E. New Reform

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  5. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  6. Codeforces Round #346 (Div. 2) A Round-House

    A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...

  7. Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径

    E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...

  8. Codeforces Round #346 (Div. 2) A. Round House 水题

    A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...

  9. Codeforces Round #346 (Div. 2)

    前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...

随机推荐

  1. Lombok(1.14.8) - @SneakyThrows

    @SneakyThrows @SneakyThrows,声明异常. package com.huey.lombok; import java.io.UnsupportedEncodingExcepti ...

  2. Oracle 12c Dataguard 数据库恢复

    http://allthingsoracle.com/rolling-forward-a-physical-standby-database-using-the-recover-command/ 当主 ...

  3. C#学习笔记之线程 - 使用线程

    基于事件的异步模式 (EAP -- The Event-Based Asynchronous Pattern) EAP提供了一个简单的办法,通过它的类能够提供多线程能力,而不需显式的开启或管理线程.它 ...

  4. C#判断输入的是否是汉字

    第一种方法:正则表达式 string text = "是不是汉字"; for (int i = 0; i < text.Length; i++) { if (Regex.Is ...

  5. mybatis like 查询

    SELECT * FROM user WHERE name like CONCAT('%',#{name},'%')

  6. bzoj 1005 HNOI2008 明明的烦恼

    这题做的我欲哭无泪啊…… 我已经没心情多说啥了…… 高精度T啊!我太弱啊!改了一天啊!还不如滚粗啊! 想好式子在写啊! 能用高精度乘单精度就不要用高精度乘高精度啊!     能用高精度除单精度就不要用 ...

  7. 专家解说IT行业存在哪些安全风险

    本人为原创作品:e良师益友 ,转载是并且注明 网络带动了IT行业的发展,同时也带来了安全风险,国外的专家分析2014年IT行业存在的11个安全隐患,随着越来越多技术的不断开发,为方便人们记忆账号,产生 ...

  8. OpenCV3读取视频或摄像头

    我们可以利用OpenCV读取视频文件或者摄像头的数据,将其保存为图像,以用于后期处理.下面的实例代码展示了简单的读取和显示操作: // This is a demo introduces you to ...

  9. .net 页面跳转方式【转】

    1 Response.Redirect这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳 转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录 ...

  10. @+id/和android:id的区别

    android:id="@+id/btn",表示在R.java文件里面新增一个id为btn的控件索引,最常用的一种声明控件id的方式.android:id="@andro ...