标签:
中文题
直接dfs下去即可,遇到重复访问的点,判断一下该环三部分异或和是否大于0
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; const int M = 400005; int n, m; struct Edge { int u, v, w; Edge() {} Edge(int u, int v, int w) { this->u = u; this->v = v; this->w = w; } } edge[M]; int head[N], nxt[M], en; void init() { memset(head, -1, sizeof(head)); en = 0; } void add_edge(int u, int v, int w) { edge[en] = Edge(u, v, w); nxt[en] = head[u]; head[u] = en++; } int vis[N], Xor[N]; bool dfs(int u, int x) { vis[u] = 1; Xor[u] = x; for (int i = head[u]; i + 1; i = nxt[i]) { int v = edge[i].v; int w = edge[i].w; if (vis[v]) { if ((Xor[u]^Xor[v]^w) > 0) return true; continue; } if (dfs(v, x^w)) return true; } return false; } bool judge() { for (int i = 1; i <= n; i++) if (!vis[i]) if (dfs(i, 0)) return true; return false; } int main() { while (~scanf("%d%d", &n, &m)) { init(); int u, v, w; while (m--) { scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w); add_edge(v, u, w); } memset(vis, 0, sizeof(vis)); printf("%s\n", judge() ? "Yes" : "No"); } return 0; }
标签:
原文地址:http://blog.csdn.net/accelerator_/article/details/44835909