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 ...
随机推荐
- 算法复习——迭代加深搜索(骑士精神bzoj1085)
题目: Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相 ...
- uva 11916 解模方程a^x=b (mod n)
Emoogle Grid You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K ...
- 星球大战 BZOJ 1015
星球大战 [问题描述] 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- Andrew Stankevich's Contest (21) J dp+组合数
坑爹的,,组合数模板,,, 6132 njczy2010 1412 Accepted 5572 MS 50620 KB C++ 1844 B 2014-10-02 21:41:15 J - 2-3 T ...
- mysql续
接上篇博客,写完以后看了看,还是觉的写的太简单,就算是自己复习都不够,所以再补充一些 1.创建多表关联 需求:图书管理系统,创建几张表,包含书籍,出版社,作者,作者详细信息等内容 分析: (1)图书只 ...
- VScode开发Vue初尝试(一)
由于公司近期有新的H5项目开发,而前端的同事也离职了,所以就临时顶缸,研究学习一下Vue框架开发. 本人也是初学,在学习过程中,把一些学习所得分享出来,可能会有很多问题和疏漏,希望大家能够多多指正,共 ...
- jquery.fullPage.js全屏滚动插件
注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...
- C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- 2017-10-28-morning-清北模拟赛
T1 立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是 ...
- SPOJ MIXTURES 区间dp
Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 differen ...