• 第十四届华中科技大学程序设计竞赛决赛同步赛 A


    A - Beauty of Trees

    题意: 

    链接:https://www.nowcoder.com/acm/contest/119/A
    来源:牛客网

    Beauty of Trees
    时间限制:C/C++ 2秒,其他语言4秒
    空间限制:C/C++ 131072K,其他语言262144K
    64bit IO Format: %lld

    题目描述

    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≤10
    5
    ), 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≤10
    9
    ), indicating that the beauty of [L
    i
    ,R
    i
    ] 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.


    题意: 
            有nn个数,每次给你一个信息l,r,kl,r,k,代表al xor al+1... xor ar=kal xor al+1... xor ar=k,问你哪些信息是错误的,如果xx信息和yy信息只可以xx对yy错或者xx错yy对,那么认为先给出的信息是对的。 

    思路: 
    并查集路径压缩。 记aa数组的前缀异或和是sumsum,那么信息l,r,kl,r,k实际上就是sumr xor suml1=ksumr xor suml−1=k,如果已知sumr xor sumx=k1,suml1 xor sumx=k2sumr xor sumx=k1,suml−1 xor sumx=k2,那么只要判断k1 xor k2k1 xor k2是否等于kk即可,否则设sumr xor sumx=k1,suml1 xor sumy=k2sumr xor sumx=k1,suml−1 xor sumy=k2,那么有sumx xor sumy=k1 xor k2 xor ksumx xor sumy=k1 xor k2 xor k,可以合并x,yx,y,并设sum[x] xor sum[y]=k1 xor k2 xor ksum[x] xor sum[y]=k1 xor k2 xor k, 然后直接用路径压缩就好了。 

    #include<bits/stdc++.h>
    using namespace std;
     
    const int N = 1e5 + 5;
    const int INF = 1e9;
     
    int f[N], a[N];
     
    int father(int x)
    {
        if (x != f[x])
        {
            int tmp = father(f[x]);
            a[x] ^= a[f[x]];
            f[x] = tmp;
        }
        return f[x];
    }
     
    int main()
    {
        int n, m; scanf("%d%d", &n, &m);
        for (int i = 0; i <= n; i++)
        {
            f[i] = i;
            a[i] = 0;
        }
        bool flag = 1;
        for (int i = 1; i <= m; i++)
        {
            int l, r, k; scanf("%d%d%d", &l, &r, &k);
            --l;
            int fl = father(l), fr = father(r);
            if (fl == fr)
            {
                if ((a[l] ^ a[r]) != k)
                {
                    printf("%d
    ", i);
                    flag = 0;
                }
            }
            else
            {
                f[fr] = fl;
                a[fr] = k ^ a[l] ^ a[r];
            }
        }
        if (flag) printf("-1");
        return 0;
    }
  • 相关阅读:
    C++ 编译、调试错误总结
    git submodule 使用
    SGI STL双端队列deque
    前端监控系统备忘
    webpack原理系列
    Fiddler+proxifier解决抓取不到客户端接口的问题
    AutoFac中常用方法说明
    SQL查询表结构
    linux安装consul集群
    Sqlserver查看所有表数据行数,查找包含xxx字符串的SP
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271031.html
Copyright © 2020-2023  润新知