您的位置:首页 > 其它

bzoj1116: [POI2008]CLO

2016-06-12 17:19 369 查看
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1116

题目大意:Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个town都有且只有一个入度

题解:并查集

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define maxn 100005
using namespace std;
int n,m;
int fa[maxn];
bool mark[maxn];
int read()
{
int x=0; char ch; bool bo=0;
while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;
while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9') ;
if (bo) return -x; return x;
}
int find(int x)
{
if (fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
int main()
{
n=read(); m=read();
for (int i=1; i<=n; i++) fa[i]=i;
for (int i=1; i<=m; i++)
{
int u=read(),v=read();
int q=find(u),p=find(v);
if (q!=p)
{
fa[q]=p; mark[p]=mark[p] | mark[q];
}
else
mark[q]=1;
}
for (int i=1; i<=n; i++)
if (!mark[find(i)])
{
printf("NIE");
return 0;
}
printf("TAK"); return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: