比赛 |
20110730 |
评测结果 |
C |
题目名称 |
线段 |
最终得分 |
0 |
用户昵称 |
Vani |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2011-07-30 09:52:40 |
显示代码纯文本
#define Debug
#include <cstdio>
#include <algorithm>
#include <cctype>
namespace Solve {
const int MAXN = 500;
inline int ScanInt(void) {
int r = 0, c;
while (!isdigit(c = getchar()));
r = c - '0';
while ( isdigit(c = getchar())) r = r * 10 + c - '0';
return r;
}
int f[MAXN], d[MAXN], n;
inline int Find(int x) {
if (f[x] == x) return x;
int ret = Find(f[x]);
return d[x] = (d[x] + d[f[x]]) & 1, f[x] = ret;
}
void solve(void) {
int n = ScanInt();
for (int i = 1; i <= n; i++) f[i] = i, d[i] = 0;
int m = ScanInt(), _ = true;
while (m--) {
int x = ScanInt(), y = ScanInt(), type = ScanInt();
int t1 = Find(x), t2 = Find(y);
if (t1 == t2) {
if (((d[x] + d[y]) & 1) != type)
_ = false;
}else {
f[t1] = t2;
d[t1] = ((d[x] + d[y]) & 1) ^ type;
}
}
if (_) {
if (Find(1) != Find(n)) puts("2");
else printf("%d\n", (d[1] + d[n]) & 1);
} else puts("No Answer");
}
}
int main(int argc, char** argv) {
#ifdef Debug
freopen("line.in", "r", stdin);
freopen("line.out", "w", stdout);
#endif
int T_T; scanf("%d", &T_T); while (T_T--)
Solve::solve();
return 0;
}