AtCoder Grand Contest 052 B

神仙题%%%
场上想了两个小时都没想出来

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
#define fi first
#define se second
#define mp make_pair
#define sz(v) ((int)(v).size())
#define debug(a) cout << #a << " = " << a << endl;
typedef long long ll;
struct node {
    int to, w1, w2;
};
vector < node > edge[N];
const ll MOD = 1e9 + 7;
int n, m;
int a[N], W, p[N], t[N];
void dfs(int u, int fa) {
    W ^= p[u] ^ t[u];
    for (int i = 0; i < sz(edge[u]); i++) {
        node x = edge[u][i];
        int v = x.to;
        if (v == fa)    continue;
        p[v] = p[u] ^ x.w1, t[v] = t[u] ^ x.w2;
        dfs(v, u);
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v, w1, w2;
        cin >> u >> v >> w1 >> w2;
        edge[u].push_back(node{v, w1, w2});
        edge[v].push_back(node{u, w1, w2});
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++)    t[i] ^= W;
    sort(p + 1, p + n + 1); 
    sort(t + 1, t + n + 1);
    for (int i = 1; i <= n; i++)
        if (p[i] != t[i]) {
            cout << "NO";
            return 0;
        }
    cout << "YES";
    return 0;
}
原文地址:https://www.cnblogs.com/cminus/p/14518844.html