1116: [POI2008]CLO
Description
Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个town都有且只有一个入度
Input
第一行输入n m.1 <= n<= 100000,1 <= m <= 200000 下面M行用于描述M条边.
Output
TAK或者NIE 常做POI的同学,应该知道这两个单词的了...
Sample Input
4 5
1 2
2 3
1 3
3 4
1 4
1 2
2 3
1 3
3 4
1 4
Sample Output
TAK
上图给出了一种连接方式.
上图给出了一种连接方式.
能看出每个连通块都必须有环。。
所以就用并查集搞搞就好了。。
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 100000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int fa[maxn],mark[maxn]; 30 int find(int x){ 31 return fa[x]==x?x:fa[x]=find(fa[x]); 32 } 33 int main(){ 34 //freopen("input.txt","r",stdin); 35 //freopen("output.txt","w",stdout); 36 int n=read(),m=read(); 37 for1(i,n)fa[i]=i; 38 for1(i,m){ 39 int u=read(),v=read(),fu=find(u),fv=find(v); 40 if(fv!=fu){ 41 fa[fv]=fu;mark[fu]=(mark[fv]|mark[fu]); 42 } 43 else mark[fv]=1; 44 } 45 for1(i,n) 46 if(!mark[find(i)]){ 47 printf("NIE\n");return 0; 48 } 49 printf("TAK\n"); 50 return 0; 51 }