bzoj1116 [POI2008]CLO

[POI2008]CLO

Time Limit: 10 Sec Memory Limit: 162 MB

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

Sample Output

TAK

上图给出了一种连接方式.



题目意思有点毒。。。应该是说有一些边定方向,剩下的边就随风消逝了233Z
想一想。。。。一棵树不行,其他就很好的啦~


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<int> point[maxn];
int n, m, fa[maxn], size[maxn], edge[maxn];

int find(int t) {return (fa[t] == t) ? t : (fa[t] = find(fa[t]));}

inline void connect(int a, int b)
{
    a = find(a); b = find(b); edge[a]++;
    if(a == b) return;
    if(a > b) swap(a, b); 
    fa[b] = a; size[a] += size[b]; edge[a] += edge[b]; 
}

int main()
{
    int x, y; scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) fa[i] = i, size[i] = 1;
    for(int i = 1; i <= m; ++i){
        scanf("%d%d", &x, &y); connect(x, y);
    }
    for(int now, i = 1; i <= n; ++i){
        now = find(i); 
        if(edge[now] < size[now]){
            printf("NIE"); exit(0);
        }
    }
    printf("TAK");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/LLppdd/p/9162206.html