1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rep(a,s,t,i) for (int a = s; a <= t; a += i) #define repp(a,s,t,i) for (int a = s; a < t; a += i) #define countdown(s) while (s --> 0) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
int getint() { int x; scanf("%d", &x); return x; } long long int getll() { long long int x; scanf("%lld", &x); return x; }
const int MAXN = 50 + 10;
int T, n, m; struct Graph{ struct Edge { int next, weight; Edge() { next = weight = 0; } Edge(int next, int weight) : next(next), weight(weight) {} };
std::vector<Edge> head[MAXN];
int prefix[MAXN]; bool vis[MAXN], exitNeeded;
Graph() { memset(prefix, 0, sizeof prefix); memset(vis, 0, sizeof vis); exitNeeded = false; } inline void addEdge(int prev, int next, int weight) { head[prev].push_back((Edge) { next, weight }); head[next].push_back((Edge) { prev, weight }); } void DFS(int now, int last, int ans) { if (exitNeeded) return; vis[now] = true; prefix[now] = ans; for (int i = 0, siz = (int) head[now].size(); i < siz && !exitNeeded; ++i) { int next = head[now][i].next; if (!vis[next]) DFS(next, now, ans ^ head[now][i].weight); else { if (now != last) { if (head[now][i].weight ^ prefix[now] ^ prefix[next]) { exitNeeded = true; return; } } } } } };
int main() { T = getint(); countdown (T) { Graph G; n = getint(); m = getint(); for (int i = 1; i <= m; ++i) { int prev = getint(); int next = getint(); int weight = getint(); G.addEdge(prev, next, weight); } for (int i = 1; i <= n; ++i) { if (!G.vis[i]) G.DFS(i, 0, 0); if (G.exitNeeded) break; } puts(G.exitNeeded ? "No" : "Yes"); } return 0; }
|