Codeforces--659E--New Reform(深搜好题)
来源:互联网 发布:数据分析平台交互设计 编辑:程序博客网 时间:2024/05/16 10:47
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 32 11 34 3
1
5 52 11 32 32 54 3
0
6 51 22 34 54 65 6
1
Hint
In the first sample the following road orientation is allowed: , , .
The second sample: , , , , .
The third sample: , , , , .
Source
Codeforces Round #346 (Div. 2)
题意:n个点中存在m条无向边,如果在这m条边加上方向,方向唯一,但是可以是任意的,求入度为0的点的个数最多是多少
思路:可以先画个图试试,如果一个点想让自己的入度不为0,最起码要有一个点指向他,但是这样的话指向它的那一个点也需要有一个点指向自己,如果是一个环的话最好了,大家可以相互指,所以在一个图中,如果有一个环的话点的入度都不会是0,因为环上的点可以相互指,不在环上的点可以由环上的点发出的线指,
这道题并查集判环应该不可以吧,我是没想到这个方法,因为可能有多个独立子图,因为这样,我们必须判断每一个独立子图是否有一个环,但是并查集做不到,我们只能判断有没有环生成,scc吗?应该也不是。如果从一个点开始访问到达一个已经访问过的点,这样的话就可以由这个访问过的点直接或者间接的给与起点入度,可以想象一下,一个环上的一个分支的起点开始访问,然后因为环的原因,访问又会回单一个已经访问过的点,如果没有环的话那这个点就要牺牲,给其他的点入度
因为是无向边,所以访问的时候要防止出现1--2,2--1这种情况,传参的时候加上起点,每次往下访问判断这次的起点还有上次的终点是否相同
#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>using namespace std;#define MAXN 100000+10vector<int>g[MAXN];int vis[MAXN],n,m,ans;void dfs(int pos,int p){if(vis[pos]){ans=0;return ;}vis[pos]=1;for(int i=0;i<g[pos].size();i++){if(g[pos][i]!=p)dfs(g[pos][i],pos);}}int main(){while(scanf("%d%d",&n,&m)!=EOF){memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++)g[i].clear();int x,y;for(int i=0;i<m;i++){scanf("%d%d",&x,&y);g[x].push_back(y);g[y].push_back(x);}int sum=0;for(int i=1;i<=n;i++){if(vis[i])continue;ans=1;dfs(i,0);sum+=ans;}printf("%d\n",sum);}return 0;}
- Codeforces--659E--New Reform(深搜好题)
- codeforces 659 E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- codeforces 659E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- Codeforces 659E New Reform【DFS】
- CodeForces 659E New Reform (DFS)
- Codeforces 659E New Reform【思维+Dfs】
- Codeforces 659E New Reform 【DFS 求环】
- codeforce 659E New Reform
- CodeForces 659E New Reform (图的遍历判环)
- CodeForces - 659E New Reform (网络流)求最少的入度为0的点的个数
- Codeforces Round #346 (Div. 2)E. New Reform 乱搞dfs
- Codeforces Round #346 (Div. 2) E. New Reform
- Codeforces Round #346 (Div. 2) E. New Reform
- Codeforces Round #346 (Div. 2)-E. New Reform
- Introduction to Linear Algebra 4ED 学习笔记(2)
- hdu1160 FatMouse's Speed 最长上升子序列 LIS
- ( Leetcode 24) Swap Nodes in Pairs
- 压缩感知重构算法之OLS算法python实现
- Cassandra原理介绍
- Codeforces--659E--New Reform(深搜好题)
- 浅谈SQL注入基础
- 宏的全解
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
- Ubuntu使用MAC主题
- Android 5 及之后版本rom解包
- 最死板的计算一个人活多久
- POJ3254 状压dp入门