Codeforces Round #346 (Div. 2) E. New Reform
1 second
256 megabytes
standard input
standard 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.
4 3
2 1
1 3
4 3
1
5 5
2 1
1 3
2 3
2 5
4 3
0
6 5
1 2
2 3
4 5
4 6
5 6
1
In the first sample the following road orientation is allowed:
,
,
.
The second sample:
,
,
,
,
.
The third sample:
,
,
,
,
.
一开始觉得 是联通分量的个数-1 后来画图发现,每个联通分量如果存在环,那么他就没有 separate点。
一开始用并查集做,没调出来.....好弱..
/* ***********************************************
Author :
Created Time :2016/4/2 16:53:35
File Name :346e.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int vis[maxn];
int n,m;
vector<int>v[maxn];
int mark=;
void dfs(int u,int fa){
if(vis[u]){
mark=;return;
}
vis[u]=;
for(int i=;i<v[u].size();i++){
int V=v[u][i];
if(V==fa)continue;
dfs(V,u);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int x,y;
while(cin>>n>>m){
cle(vis);mark=;
for(int i=;i<=m;i++){
scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int ans=;
for(int i=;i<=n;i++){
mark=;
if(!vis[i]){
dfs(i,-);
if(mark==)ans++;
}
}
cout<<ans<<endl;
}
return ;
}
Codeforces Round #346 (Div. 2) E. New Reform的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #346 (Div. 2) E - New Reform 无相图求环
题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #346 (Div. 2)
前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...
- Codeforces Round #346 (Div. 2) D Bicycle Race
D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...
随机推荐
- VS链接错误: LNIK1123
问题:编译一个VS工程程序,出现连接错误:"LNK1123: 转换到 COFF 期间失败: 文件无效或损坏" 原因分析:连接器LNK是通过调用cvtres.exe完成文件向coff ...
- 更改navigationBar 颜色
if (IS_IOS7()) { /* iOS7 时 Navigation 颜色 */ [[UINavigationBar appearance] setBarTintColor: HexCo ...
- elasticsearch入门使用(一)es 6.2.2安装,centos 7
elasticsearch(一般叫es)是基于Lucene的搜索服务器,提供http协议接口使用json格式数据,也提供相应的客户端,更详细的信息[优点&场景]请百度百科, 以下官网截图,官网 ...
- LCA rmq st model
LCA:倍增 memset(p,-,sizeof(p)); inline void dfs(int u) { ;i=e[i].next) { int v=e[i].v; ) { deep[v]=dee ...
- Spring基于Setter函数的依赖注入(DI)
以下内容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-setter-based-dependenc ...
- C++字符串转数字,数字转字符串
1. 字符串转数字 如将"32"转为32,将"3.1415"转为3.1415,将"567283"转为567283.使用: //Convert ...
- 天下文章一大抄 之 修改excel 创建时间
Sub ChangeDate()ThisWorkbook.BuiltinDocumentProperties("Creation Date") = #2 28 2016 13:25 ...
- 用JAVA编写浏览器内核之实现javascript的document对象与内置方法
原创文章.转载请注明. 阅读本文之前,您须要对浏览器怎样载入javascript有一定了解. 当然,对java与javascript本身也须要了解. 本文首先介绍浏览器载入并执行javascript的 ...
- eclipse svn org.tigris.subversion.javahl.ClientException: RA layer request failed
突然之间eclipse使用svn更新项目时报错,org.tigris.subversion.javahl.ClientException: RA layer request failed 网上搜的都是 ...
- Python演绎的精彩故事(二)
书接上回.在展示了App最顶层的代码后,我们去看看各模块怎样编程. 为了能看懂各模块的代码,首先须要铺垫一下Softchip架构的基本概念和设计规范. 1.随意模块不持有其它模块的实例.自然不再显式使 ...