题意:给你一个无向图,如今要求你把边改成有向的。 使得入度为0的点最少,输出有多少个点入度为0

思路:脑补一波结论。假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000+1000
#define LL long long
int cas=1,T;
vector<int> e[maxn];
int vis[maxn];
int flag=0;
void dfs(int u,int fa)
{
if (vis[u])
{
flag=1;
return;
}
vis[u]=1;
for (int i=0;i<e[u].size();i++)
{
int v = e[u][i];
if (v==fa)
continue;
dfs(v,u);
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
int ans = 0;
for (int i = 1;i<=n;i++)
{
if (!vis[i])
{
                        flag = 0;
dfs(i,-1);
if (!flag)
ans++;
}
}
printf("%d\n",ans);
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}

Description

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.

Input

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 ≤ nxi ≠ 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.

Output

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

Sample Input

Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1

Hint

In the first sample the following road orientation is allowed: .

The second sample: .

The third sample: .


CodeForces 659E New Reform的更多相关文章

  1. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  2. CodeForces 659E New Reform (图的遍历判环)

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  3. [图中找环] Codeforces 659E New Reform

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

  4. codeforces 659E . New Reform 强连通

    题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...

  5. codeforces 659E E. New Reform(图论)

    题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces 732F. Tourist Reform (Tarjan缩点)

    题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...

  7. CodeForces 732F Tourist Reform

    边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...

  8. CodeForces 723E One-Way Reform

    构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...

  9. 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 ...

随机推荐

  1. 如何使用python发送邮件

    使用python发送邮件,用的是SMTP协议. 因此在qq邮箱中,要设置开启SMTP服务 只要能开启一个就行 在我们执行程序的时候,会发现邮件被发送过来了 在python中还有一个更简单的第三方模块, ...

  2. 【Android开发日记】之入门篇(三)——Android目录结构

    本来的话,这一章想要介绍的是Android的系统架构,毕竟有了这些知识的储备,再去看实际的项目时才会更清楚地理解为什么要这样设计,同时在开发中遇到难题,也可以凭借着对Android的了解,尽快找出哪些 ...

  3. 浅谈正则表达式-PHP为例

    第一次比较系统的学习正则表达式,本篇文章以PHP语言为例来学习. 基本概念 正则表达式=普通字符(如a-z)+分隔符(正斜线(/).hash符号(#) 以及取反符号(~))+特殊字符(称为元字符) 两 ...

  4. eclipse CreateProcess error=87

    http://blog.csdn.net/mylove709834360/article/details/9253697 完美解决~

  5. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  6. 允许root用户登录ssh

    使用普通用户登录Ubuntu系统,打开命令行窗口 更改root用户密码,命令:sudo passwd root 首先输入当前用户的密码 然后输入root账户的密码 确认root用户的密码 编辑ssh的 ...

  7. 源码安装cmake(或者叫升级cmake)

    cmake source install as follows: 0 cd ~ 1 wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz 2 tar ...

  8. sublime text mac使用技巧

    工欲善其事,必先利其器 1.列选择 鼠标左键+OPTION 2.查找替换 COMMAND+OPTION+F 3.分屏 COMMAND+OPTION+数字,具体数字代表要分几个屏

  9. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  10. [COCI2015]TRAKTOR

    题目大意: 一个$X\times Y(X,Y\leq10^5)$的格子中,每秒钟依次$n(n\leq10^6)$个蘑菇, 告诉你每个蘑菇出现的时间和位置,问何时第一次出现$k(2\leq k\leq ...