bzoj 1116

思路:每个连通块都判是否有环。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> >

using namespace std;

const int N = 1e5 + 10;
const int M = 10 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

int n, m, vis[N];
vector<int> edge[N];

bool dfs(int u, int p) {
    vis[u] = -1;
    for(int i = 0; i < edge[u].size(); i++) {
        int v = edge[u][i];
        if(v == p) continue;
        if(vis[v] == -1) {
            return false;
        } else if(!vis[v] && !dfs(v, u)) {
            return false;
        }
    }
    vis[u] = 1;
    return true;
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        int u, v; scanf("%d%d", &u, &v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    for(int i = 1; i <= n; i++) {
        if(!vis[i] && dfs(i, 0)) {
            puts("NIE");
            return 0;
        }
    }

    puts("TAK");
    return 0;
}
/*
*/

 

posted @ 2018-06-15 14:14  NotNight  阅读(101)  评论(0编辑  收藏  举报