zoukankan      html  css  js  c++  java
  • Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip

    地址:http://codeforces.com/contest/766/problem/E

    题目:

    E. Mahmoud and a xor trip
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number ai attached to it.

    We define the distance from city x to city y as the xor of numbers attached to the cities on the path from x to y (including both x and y). In other words if values attached to the cities on the path from x to y form an array p of length l then the distance between them is , where  is bitwise xor operation.

    Mahmoud and Ehab want to choose two cities and make a journey from one to another. The index of the start city is always less than or equal to the index of the finish city (they may start and finish in the same city and in this case the distance equals the number attached to that city). They can't determine the two cities so they try every city as a start and every city with greater index as a finish. They want to know the total distance between all pairs of cities.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of cities in Mahmoud and Ehab's country.

    Then the second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 106) which represent the numbers attached to the cities. Integer ai is attached to the city i.

    Each of the next n  -  1 lines contains two integers u and v (1  ≤  u,  v  ≤  nu  ≠  v), denoting that there is an undirected road between cities u and v. It's guaranteed that you can reach any city from any other using these roads.

    Output

    Output one number denoting the total distance between all pairs of cities.

    Examples
    input
    3
    1 2 3
    1 2
    2 3
    output
    10
    input
    5
    1 2 3 4 5
    1 2
    2 3
    3 4
    3 5
    output
    52
    input
    5
    10 9 8 7 6
    1 2
    2 3
    3 4
    3 5
    output
    131
    Note

    A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.

    In the first sample the available paths are:

    • city 1 to itself with a distance of 1,
    • city 2 to itself with a distance of 2,
    • city 3 to itself with a distance of 3,
    • city 1 to city 2 with a distance of ,
    • city 1 to city 3 with a distance of ,
    • city 2 to city 3 with a distance of .
    The total distance between all pairs of cities equals 1 + 2 + 3 + 3 + 0 + 1 = 10.
     
    思路:思路很巧妙地树形dp+二进制拆位。
      每次考虑第k位对答案的贡献。
      dp[i][0]表示经过i节点的异或和为0的路径的数量,dp[i][1]同理。
      具体见代码:
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e5+7;
    12 const int mod=1e9+7;
    13 
    14 
    15 int n,v[K],dp[K][2];
    16 LL ans;
    17 vector<int>mp[K];
    18 
    19 void dfs(int x,int f,int k)
    20 {
    21     int t=v[x]>>k&1;//判断第k位是0还是1
    22     LL sum=0;
    23     dp[x][t]=1,dp[x][t^1]=0;//初始化
    24     for(int i=0;i<mp[x].size();i++)
    25     if(mp[x][i]!=f)
    26     {
    27         int v=mp[x][i];
    28         dfs(v,x,k);
    29         sum+=dp[x][0]*dp[v][1]+dp[x][1]*dp[v][0];//只有0^1,1^0对答案有贡献
    30         dp[x][t^0]+=dp[v][0];//很巧妙的更新状态,因为异或值,
    31         dp[x][t^1]+=dp[v][1];//所以是t^0后的结果加上dp[v][0]
    32     }
    33     ans+=(sum<<k);
    34 }
    35 int main(void)
    36 {
    37     cin>>n;
    38     for(int i=1;i<=n;i++)
    39         scanf("%d",v+i),ans+=v[i];
    40     for(int i=1,u,v;i<n;i++)
    41         scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
    42     for(int i=0;i<=20;i++)
    43         dfs(1,0,i);
    44     printf("%lld
    ",ans);
    45     return 0;
    46 }

     

  • 相关阅读:
    PHP输出中文乱码的问题(转)
    phpmyadmin导出数据库为什么是php文件
    phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增
    phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
    利用eclipse开发php<转>
    apache 2.4 You don't have permission to access / on this server
    (转)如果“打开方式”里面没有想要的打开方式,怎样创建一种文件打开方式?
    (转)安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
    关于ISAPI和CGI限制,这个要设为允许
    Sqlserver数据库日志太大如何快速删除
  • 原文地址:https://www.cnblogs.com/weeping/p/6419075.html
Copyright © 2011-2022 走看看