传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1116
题目大意:Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个town都有且只有一个入度
题解:并查集
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #include<cmath> 6 #define maxn 100005 7 using namespace std; 8 int n,m; 9 int fa[maxn]; 10 bool mark[maxn]; 11 int read() 12 { 13 int x=0; char ch; bool bo=0; 14 while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1; 15 while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9') ; 16 if (bo) return -x; return x; 17 } 18 int find(int x) 19 { 20 if (fa[x]!=x) fa[x]=find(fa[x]); 21 return fa[x]; 22 } 23 int main() 24 { 25 n=read(); m=read(); 26 for (int i=1; i<=n; i++) fa[i]=i; 27 for (int i=1; i<=m; i++) 28 { 29 int u=read(),v=read(); 30 int q=find(u),p=find(v); 31 if (q!=p) 32 { 33 fa[q]=p; mark[p]=mark[p] | mark[q]; 34 } 35 else 36 mark[q]=1; 37 } 38 for (int i=1; i<=n; i++) 39 if (!mark[find(i)]) 40 { 41 printf("NIE"); 42 return 0; 43 } 44 printf("TAK"); return 0; 45 }