zoukankan      html  css  js  c++  java
  • 【codeforces 766E】Mahmoud and a xor trip

    【题目链接】:http://codeforces.com/contest/766/problem/E

    【题意】

    定义树上任意两点之间的距离为这条简单路径上经过的点;
    那些点上的权值的所有异或;
    求任意两点之间的距离和;

    【题解】

    权值最大为1e6
    所以每个点的权值的二进制形式最多20位左右;
    则我们可以对权值的二进制形式的每一位独立考虑;
    我们枚举第i位;
    并且在计算的时候只考虑这第i位;
    可以做树形dp;
    算出穿过当前这个节点的路径(并且以其为lca->最高点)
    异或和的二进制形式在第i为上权值为1的路径的个数x;
    (1<<i)x就是答案了;
    累加这个答案就好;
    这里穿过当前这个节点且路径的距离(异或和)在第i位的权值为1;
    也就是说剩余的节点,要么左边异或和为0且右边异或和为1或者是左边疑惑和为1右边疑惑和为0;同时为1或同时为0都不行;
    记录每个节点下到该节点的异或和第i位为0和1的路径个数就好;这个很容易维护的;
    当然因为有说起点和终点可以相同;所以一开始累加a[i]值;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define ps push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    #define ref(x) scanf("%lf",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 1e5+100;
    
    int n,a[N],bit;
    LL ans = 0,cnt[N][2];
    vector <int> G[N];
    
    void dfs(int x, int fa) {
        int t = (a[x] >> bit) & 1;
        cnt[x][t] = 1, cnt[x][1 - t] = 0;
        for (int y : G[x]){
            if (y == fa) continue;
            dfs(y, x);
            ans += ((cnt[x][0] * cnt[y][1] + cnt[x][1] * cnt[y][0])<<bit);
            cnt[x][t ^ 1] += cnt[y][1];
            cnt[x][t ^ 0] += cnt[y][0];
        }
    }
    
    int main(){
        //freopen("F:\rush.txt", "r", stdin);
        rei(n);
        rep1(i, 1, n) rei(a[i]),ans+=a[i];
        rep1(i, 1, n - 1) {
            int x, y;
            rei(x), rei(y);
            G[x].ps(y), G[y].ps(x);
        }
        for (bit = 0;bit <= 22;bit++) dfs(1, 0);
        printf("%lld
    ", ans);
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    123457123456---com.threeObj3.BabyShizi02--- 宝宝识字02
    协议
    123457---com.threeObj.Baobaoshizi01--- 宝宝识字01
    123456---com.twoapp.ErTongNongChangPinTu---儿童农场拼图
    Mysql
    MySQL的四种事务隔离级别
    Git撤销&回滚操作
    java.util.Timer简介
    git常用命令
    BigDecimal转String
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626477.html
Copyright © 2011-2022 走看看