E. New Reform

题目连接:

http://www.codeforces.com/contest/659/problem/E

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

Output

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

Sample Input

4 3

2 1

1 3

4 3

Sample Output

1

Hint

题意

给你一个图,现在要你给这个图里面的边定方向,使得入度为0的点最少。

题解:

对于一个连通块而言,如果里面存在一个环,那么必然所有点的入度都可以大于等于1

否则的话,就存在一个点的入度为0。

dfs一波就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int> E[maxn];
int n,m,flag;
int vis[maxn];
void dfs(int x,int fa)
{
if(vis[x])
{
flag = 1;
return;
}
vis[x]=1;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(v==fa)continue;
dfs(v,x);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
int ans = 0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
flag = 0;
dfs(i,-1);
if(!flag)ans++;
}
}
cout<<ans<<endl;
}

Codeforces Round #346 (Div. 2) E. New Reform dfs的更多相关文章

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

  2. Codeforces Round #346 (Div. 2) E - New Reform 无相图求环

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

  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. New Reform--- 并查集(或连通图)

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

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

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

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

  8. Codeforces Round #346 (Div. 2)

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

  9. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

随机推荐

  1. UML相关说明

    在java开发中,有很多时候我们是在不断的处理类与类之间关系,其中这六种关系是:依赖.关联.聚合.组合.继承.实现. 它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合& ...

  2. 64_t4

    texlive-hardwrap-svn21396.0.2-33.fc26.2.noarch.rpm 24-May-2017 15:41 35930 texlive-harmony-doc-svn15 ...

  3. Ubuntu或者Ubuntu server重新设置IP地址

    1.打开终端输入: sudo vi /etc/network/interfaces 2.进入编辑页面 改一处,添加5行内容,如下图: 3.修改好后esc    :wq进行保存 4.输入: sudo / ...

  4. mac上卸载mysql

    在终端输入一下命令 sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MyS ...

  5. PHP缓存加速插件 XCache 、 ZendOpcache 安装

    PHP缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(OperateCode,简称opcode)文件,该文件是PHP代码的一种二进制表示方式.默 ...

  6. acm专题---dfs+bfs

    题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...

  7. 使用extjs做的一个简单grid

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  8. 结构体对齐及#pragma详细解释

    在linux下c语言结构体对齐: 1.自然对齐 struct 是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float 等)的变量,也可以是一些复合数据类型(如array.s ...

  9. hdu 5920(模拟)

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  10. Adding Completion to (interactive)

      Adding Completion to (interactive) Author: Tubo Question: Is there any way to add my own completio ...