1. 程式人生 > >[POI2008]CLO

[POI2008]CLO

define AD wol ret tro #define script ron poi

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

HINT
技術分享圖片
上圖給出了一種連接方式.

這題可以把邊刪掉……也就是直接忽略一些邊……

那就直接判斷,有樹就不可行,否則就可行,點也算樹。並查集維護一波

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline
int read(){ int x=0,f=1;char ch=getchar(); for (;ch<‘0‘||ch>‘9‘;ch=getchar()) if (ch==‘-‘) f=-1; for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=(x<<1)+(x<<3)+ch-‘0‘; return x*f; } inline void print(int x){ if (x>=10) print(x/10); putchar(x%10+‘0‘
); } const int N=1e5; int fa[N+10]; bool can[N+10]; int find(int x){ if (fa[x]==x) return x; fa[x]=find(fa[x]); return fa[x]; } int main(){ int n=read(),m=read(); for (int i=1;i<=n;i++) fa[i]=i; for (int i=1;i<=m;i++){ int x=find(read()),y=find(read()); x!=y?(fa[x]=y,can[y]|=can[x]):can[fa[x]]=1; } for (int i=1;i<=n;i++) if (!can[find(i)]){printf("NIE\n");return 0;} printf("TAK\n"); return 0; }

[POI2008]CLO