【BZOJ1116】[POI2008]CLO【BFS】

【题目链接】

同【这个题】

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 100005, maxm = 200005, maxq = maxn;

int n, m, head[maxn], cnt, q[maxq];
bool vis[maxn];

struct _edge {
	int v, next;
} g[maxm << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

inline bool bfs(int x) {
	int h = 0, t = 0, node = 0, edge = 0;
	vis[q[t++] = x] = 1;
	while(h != t) {
		int u = q[h++];
		node++;
		for(int i = head[u]; ~i; i = g[i].next) {
			edge++;
			if(!vis[g[i].v]) vis[q[t++] = g[i].v] = 1;
		}
	}
	return (edge >> 1) == node - 1;
}

int main() {
	n = iread(); m = iread();
	for(int i = 0; i <= n; i++) head[i] = -1; cnt = 0;

	for(int i = 1; i <= m; i++) {
		int u = iread(), v = iread();
		add(u, v); add(v, u);
	}

	bool ans = 0;
	for(int i = 1; i <= n; i++) if(!vis[i]) {
		ans = bfs(i);
		if(ans) break;
	}

	printf(ans ? "NIE\n" : "TAK\n");
	return 0;
}


你可能感兴趣的:(【BZOJ1116】[POI2008]CLO【BFS】)