ICPC2017 Urumqi A Possible Tree

5220: A Possible Tree

時間限制: 2 Sec  內存限制: 128 MB
提交: 80  解決: 27
[提交] [狀態] [討論版] [命題人:admin]

題目描述

Alice knows that Bob has a secret tree (in terms of graph theory) with n nodes with n − 1 weighted edges with integer values in [0, 260 −1]. She knows its structure but does not know the specific information about edge weights.
Thanks to the awakening of Bob’s conscience, Alice gets m conclusions related to his tree. Each conclusion provides three integers u, v and val saying that the exclusive OR (XOR) sum of edge weights in the unique shortest path between u and v is equal to val.
Some conclusions provided might be wrong and Alice wants to find the maximum number W such that the first W given conclusions are compatible. That is say that at least one allocation of edge weights satisfies the first W conclusions all together but no way satisfies all the first W + 1 conclusions (or there are only W conclusions provided in total).
Help Alice find the exact value of W.

 

輸入

The input has several test cases and the first line contains an integer t (1 ≤ t ≤ 30) which is the number of test cases.
For each case, the first line contains two integers n (1 ≤ n ≤ 100000) and c (1 ≤ c ≤ 100000) which are the number of nodes in the tree and the number of conclusions provided. Each of the following n−1 lines contains two integers u and v (1 ≤ u, v ≤ n) indicating an edge in the tree between the u-th node and the v-th node. Each of the following c lines provides a conclusion with three integers u, v and val where 1 ≤ u, v ≤ n and val ∈ [0, 260 − 1].

 

輸出

For each test case, output the integer W in a single line.

 

樣例輸入

2
7 5
1 2
2 3
3 4
4 5
5 6
6 7
1 3 1
3 5 0
5 7 1
1 7 1
2 3 2
7 5
1 2
1 3
1 4
3 5
3 6
3 7
2 6 6
4 7 7
6 7 3
5 4 5
2 5 6

 

樣例輸出

3
4

題目是說給你一課樹,然後每次給你兩個點之間的所有邊的亦或和的值,讓你判斷前面多少個給的信息是正確的。

其實答案和給的樹並沒有關係,用帶權並查集維護一下一個點到他的祖先點的距離的亦或和。每次讀入兩個點和他們之間的距離的亦或和,如果和現有的並查集裏的數據發生衝突了,則得到了結果,否則把新讀入的兩點之間的距離的亦或和存入並查集。

#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
typedef long long ll;
int n,c;
const int maxn = 100005;
int f[maxn];
ll vxor[maxn];
void init()
{
    for (int i = 0; i <= n; i++)
        f[i] = i;
    memset(vxor, 0, sizeof(vxor));
}
int findd(int x)
{
    if (f[x] == x)
        return f[x];
//    vxor[x] ^= vxor[f[x]];
    ll tmp = f[x];
    f[x] = findd(f[x]);
    vxor[x] ^= vxor[tmp];
    return f[x];
}
int main()
{
//    freopen("in.txt","r",stdin);
    int t,x,y,ans;
    scanf("%d", &t);
    while (t--)
    {
        ll w;
        scanf("%d%d", &n, &c);
        ans = c;
        init();
        for (int i = 1; i < n; i++)
            scanf("%d%d", &x, &y);
        scanf("%d%d%lld", &x, &y, &w);
        int fx = findd(x);
        int fy = findd(y);
        f[fx] = fy;
        vxor[fx] = vxor[x] ^ vxor[y] ^ w;
        for (int i = 1; i < c; i++)
        {
            scanf("%d%d%lld", &x, &y, &w);
            fx = findd(x);
            fy = findd(y);
            if (fx == fy && (vxor[x] ^ vxor[y]) != w)
            {
                ans = i;
                for (; i < c - 1; i++) //把剩餘的數據讀完
                    scanf("%d%d%lld", &x, &y, &w);
                break;
            }
            f[fx] = fy;
            vxor[fx] = vxor[x] ^ vxor[y] ^ w;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章