码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1116 [POI2008]CLO

时间:2018-02-05 10:44:00      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:des   sample   连接   out   post   ast   并查集   zoj   src   

Description

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

Input

第一行输入\(n\) \(m\) .\(1\leq n\leq 100000\),\(1 \leq m \leq 200000\) 下面 \(M\) 行用于描述 \(M\) 条边.

Output

\(TAK\) 或者 \(NIE\) 常做 \(POI\) 的同学,应该知道这两个单词的了...

Sample Input

4 5
1 2
2 3
1 3
3 4
1 4
技术分享图片

Sample Output

TAK
技术分享图片
上图给出了一种连接方式.

Solution

听说下午要讲并查集的各种玩法...
对于每一个连通块考虑,如果一个块没有环,那么这个连通块必然无法满足题目要求。

#include<bits/stdc++.h>
using namespace std;

#define N 100001
#define rep(i, a, b) for (int i = a; i <= b; i++)

inline int read() {
    int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }
    while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
}

int fa[N]; bool tag[N];
int find(int x) { return fa[x] ? fa[x] = find(fa[x]) : x; }
int main() {
    int n = read(), m = read(); while (m--) {
        int u = read(), v = read(), x = find(u), y = find(v);
        if (x ^ y) fa[x] = y, tag[x] |= tag[y], tag[y] |= tag[x];
        else tag[x] = 1;
    }
    rep(i, 1, n) if (!tag[find(i)]) { puts("NIE"); return 0; }
    puts("TAK");
    return 0;
}

bzoj1116 [POI2008]CLO

标签:des   sample   连接   out   post   ast   并查集   zoj   src   

原文地址:https://www.cnblogs.com/aziint/p/8416273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!