第十四届华中科技大学程序设计竞赛决赛 A-Beauty of Trees 【并查集】

题目描述 
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
One day the tree manager wants to play a game with you. There are N trees lining up in a straight road. The beauty of a set of trees is defined as the bitwise XOR sum of the heights of these trees. The game will last for M rounds, and each time he will tell you an interval [Li,Ri] and its beauty. However, he mixed some fake messages with the correct ones. Your task is to find the messages that cannot logically correspond to its former correct messages. Otherwise you’ll think the message is correct.
输入描述:
The first line contains two integer N and M(1≤N,M≤105), the number of trees and the rounds of game.
Then M lines followed, in each line are three integer L, R and k(1≤L≤R≤N,0≤k≤109), indicating that the beauty of [Li,Ri] is k.
输出描述:
If the i-th message is wrong, then print i in a single line.
If there is no mistake, print -1.
示例1
输入


3 4
1 3 6
1 2 5
3 3 10000
3 3 3
输出


3
说明


You can infer from the first two messages that the height of the third tree is 3.点击打开链接

#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
int pre[MAX];
int XOR[MAX];
int findx(int x)
{
    if(x != pre[x])
    {
        int tmp = pre[x];
        pre[x] = findx(pre[x]);
        XOR[x] = XOR[x] ^ XOR[tmp];
    }
    return pre[x];
}
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i <= n; i++)
    {
        XOR[i] = 0;
        pre[i] = i;
    }
    bool flag = 0;
    for(int i = 1, l, r, v; i <= m; i++)
    {
        cin >> l >> r >> v;
        l--;
        int L = findx(l);
        int R = findx(r);
        if(L == R)
        {
            if((XOR[l] ^ XOR[r]) != v)
            {
                flag = 1;
                cout << i << endl;
            }
        }
        else
        {
            pre[L] = R;
            XOR[L] = XOR[l] ^ XOR[r] ^ v;
        }
    }
    if(!flag)
        cout << "-1" << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值