题目链接:

题目

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. 你不知道的JSON的高效率用法

    1.JSON JSON是JavaScript Object Notation的缩写,是JavaScript标准的一个子集.官方Android API已经内置支持读写JSON数据.这种格式非常适合表示不 ...

  2. [Jquery] 获取地址栏参数的方法 备忘

    <script type="text/javascript"> (function ($) { $.getUrlParam = function (name) { va ...

  3. JSP之session

    index.jsp: <form id="form1" name="form1" method="post" action=" ...

  4. 给 Android 初学者的 Gradle 知识普及

    给 Android 初学者的 Gradle 知识普及:http://gold.xitu.io/entry/5778f8bd165abd0054b443b0/promote?utm_source=bai ...

  5. UIViewSubviews多个views之间的关系

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  6. (转)JVM参数调优八大技巧

    这里和大家分享一下JVM参数调优的八条经验,JVM参数调优,这是很头痛的问题,设置的不好,JVM不断执行FullGC,导致整个系统变得很慢,网站停滞时间能达10秒以上,相信通过本文的学习你对JVM参数 ...

  7. 从0开始学习react(三)

    这次我们来讲解第三节知识,考虑了下,先不去讲什么理论了,毕竟网上一搜一大堆,而且理论真心看不太懂啊!!! 今天我们就直接上实例喽! 大家HIGH起来!!!(想了好久,还是没舍得删这句话) 1.根据下图 ...

  8. C#调用WinAPI及窗口过程

    C#调用WINAPI及Windows窗口消息的发与送 最近在做一个餐饮项目(C#Winform),其中有一块是做点菜宝接口的对接,点菜宝的厂商提供了一个WX.exe的驱动程序,这个驱动程序无直接打开, ...

  9. Java中浮点数的基础知识

    偶然查看Math.round的JDK public static int round(float a) { if (a != 0x1.fffffep-2f) // greatest float val ...

  10. CSU-ACM2016暑假集训训练1-二分搜索 A - Can you find it?

    Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Give yo ...