bzoj1116: [POI2008]CLO

传送门: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 }
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值