比赛 20110730 评测结果 C
题目名称 线段 最终得分 0
用户昵称 Vani 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2011-07-30 09:52:40
显示代码纯文本
  1. #define Debug
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cctype>
  5. namespace Solve {
  6. const int MAXN = 500;
  7.  
  8. inline int ScanInt(void) {
  9. int r = 0, c;
  10. while (!isdigit(c = getchar()));
  11. r = c - '0';
  12. while ( isdigit(c = getchar())) r = r * 10 + c - '0';
  13. return r;
  14. }
  15.  
  16. int f[MAXN], d[MAXN], n;
  17.  
  18. inline int Find(int x) {
  19. if (f[x] == x) return x;
  20. int ret = Find(f[x]);
  21. return d[x] = (d[x] + d[f[x]]) & 1, f[x] = ret;
  22. }
  23.  
  24. void solve(void) {
  25. int n = ScanInt();
  26. for (int i = 1; i <= n; i++) f[i] = i, d[i] = 0;
  27. int m = ScanInt(), _ = true;
  28. while (m--) {
  29. int x = ScanInt(), y = ScanInt(), type = ScanInt();
  30. int t1 = Find(x), t2 = Find(y);
  31. if (t1 == t2) {
  32. if (((d[x] + d[y]) & 1) != type)
  33. _ = false;
  34. }else {
  35. f[t1] = t2;
  36. d[t1] = ((d[x] + d[y]) & 1) ^ type;
  37. }
  38. }
  39. if (_) {
  40. if (Find(1) != Find(n)) puts("2");
  41. else printf("%d\n", (d[1] + d[n]) & 1);
  42. } else puts("No Answer");
  43. }
  44. }
  45. int main(int argc, char** argv) {
  46. #ifdef Debug
  47. freopen("line.in", "r", stdin);
  48. freopen("line.out", "w", stdout);
  49. #endif
  50. int T_T; scanf("%d", &T_T); while (T_T--)
  51. Solve::solve();
  52. return 0;
  53. }
  54.