并查集
并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
基本代码如下:
void init() //初始化
{
for(int i=0;i<=n;i++){
a[i]=i;
}
}
int find(int x) //查找x的父代及路径压缩
{
return x==a[x] ? x : a[x]=find(a[x]);
}
void mix(int p,int q) //将p和q联合到一个集合
{
int x=find(p);
int y=find(q);
if(x!=y){
a[x]=y;
num--;
}
}
并查集判环
int mix(int x,int y)
{
int p=find(x);
int q=find(y);
if(p!=q)
{
a[p]=q;
if(b[q]||b[p]||b[x]||b[y]) b[x]=b[y]=b[p]=b[q]=1; //与环在同一集合内的点都进行标记
}
else b[x]=b[y]=b[p]=b[q]=1; //若形成环,则进行标记
return 0;
}
int check() //检查入度为0的点
{
int ans=0;
for(int i=1;i<=n;i++)
{
if(b[find(i)]==false&&find(i)==i) ans++;
}
return ans;
}
E. New Reform
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 isnot 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 consideredseparate, 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: thei-th road is determined by two distinct integersxi, yi (1 ≤ xi, yi ≤ n,xi ≠ yi), wherexi andyi are the numbers of the cities connected by thei-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.
Examples
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
题意:给你n个点,m条边, 让你给每条边一个方向, 求入度为0的点的最小个数
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100000+5;
const int inf=0x3f3f3f3f;
int a[maxn];
int b[maxn];
int n,m;
void init()
{
for(int i=0;i<=n;i++)
a[i]=i;
}
int find(int x)
{
return x==a[x] ? x : a[x]=find(a[x]);
}
int mix(int x,int y)
{
int p=find(x);
int q=find(y);
if(p!=q)
{
a[p]=q;
if(b[q]||b[p]||b[x]||b[y]) b[x]=b[y]=b[p]=b[q]=1;
}
else b[x]=b[y]=b[p]=b[q]=1;
return 0;
}
int check(){
int ans=0;
for(int i=1;i<=n;i++)
{
if(b[find(i)]==false&&find(i)==i) ans++;
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
init();
for(int i=1;i<=m;i++)
{
int p,q;
cin>>p>>q;
mix(p,q);
}
cout<<check()<<endl;
return 0;
}