CRB and Tree

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 967    Accepted Submission(s): 308

Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …, N. They are connected by N – 1 edges. Each edge has a weight.
For any two vertices u and v(possibly equal), f(u,v) is xor(exclusive-or) sum of weights of all edges on the path from u to v.
CRB’s task is for given s, to calculate the number of unordered pairs (u,v) such that f(u,v) = s. Can you help him?
 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer N denoting the number of vertices.
Each of the next N - 1 lines contains three space separated integers a, b and c denoting an edge between a and b, whose weight is c.
The next line contains an integer Q denoting the number of queries.
Each of the next Q lines contains a single integer s.
1 ≤ T ≤ 25
1 ≤ N ≤ 105
1 ≤ Q ≤ 10
1 ≤ a, b ≤ N
0 ≤ c, s ≤ 105
It is guaranteed that given edges form a tree.

 
Output
For each query, output one line containing the answer.
 
Sample Input
1
3
1 2 1
2 3 2
3
2
3
4
 
Sample Output
1
1
0

Hint

For the first query, (2, 3) is the only pair that f(u, v) = 2.
For the second query, (1, 3) is the only one.
For the third query, there are no pair (u, v) such that f(u, v) = 4.

 
 
题目大意:给你一棵n个顶点n-1条边的树,每条边有一个权重,定义f(u,v)为结点u到结点v的边权异或值的和,让你求在该树上有多少f(u,v)=s的无序对。
 
解题思路:由于异或的性质。a^a=0。f(u,v)=f(1,u)^f(1,v)。f(u,v)=s  =>  f(1,u)^f(1,v)=s  =>   f(1,v)=f(1,u)^s。那么我们可以枚举u,然后得出f(1,v)为 f(1,u)^s的有多少个。对于s为0的情况,我们发现对于f(1,u)^0的结果还是f(1,u)我们在计算的时候会加一次f(1,u)本身的情况。那么我们最后只需要加上n就可以满足f(u,v)=f(u,u)的情况了。
如样例:
555 
3
1 2 1
1 3 1
1
0
如果不加上n且没除以2之前的结果会是5。如果加上n那么结果就是8。最后除以2以后就是4。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+200;
int n,tot;
struct Edge{
int to,w,next;
Edge(){}
Edge(int too,int ww,int nextt){
to=too;w=ww;next=nextt;
}
}edges[maxn*3];
typedef long long INT;
int head[maxn*2],val[maxn*2];
int times[maxn*2];
void init(){
tot=0;
memset(head,-1,sizeof(head));
memset(times,0,sizeof(times));
memset(val,0,sizeof(val));
}
void addedge(int fro , int to,int wei){
edges[tot]=Edge(to,wei,head[fro]);
head[fro]=tot++;
edges[tot]=Edge(fro,wei,head[to]);
head[to]=tot++;
}
void dfs(int u,int fa){
times[val[u]]++;
for(int i=head[u];i!=-1;i=edges[i].next){
Edge &e=edges[i];
if(e.to==fa)
continue;
val[e.to]=val[u]^e.w;
dfs(e.to,u);
}
return ;
}
int main(){
// freopen("1011.in","r",stdin);
// freopen("why.txt","w",stdout);
int t , m, a,b,c ,s;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
dfs(1,-1);
scanf("%d",&m);
while(m--){
INT ans=0;
scanf("%d",&s);
for(int i=1;i<=n;++i){
if(times[val[i]^s]){
ans+=times[val[i]^s];
}
}
if(s==0)
ans+=n;
printf("%lld\n",ans/2);
}
}
return 0;
}

  

 
 
 

HDU 5416——CRB and Tree——————【DFS搜树】的更多相关文章

  1. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  2. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. HDOJ 5416 CRB and Tree DFS

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  5. HDU 5416 CRB and Tree (2015多校第10场)

    欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536 ...

  6. HDU 5416 CRB and Tree

    题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...

  7. HDU 5416 CRB and Tree (技巧)

    题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...

  8. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

随机推荐

  1. ReentrantReadWriteLock原理

    原文链接:https://www.jianshu.com/p/9f98299a17a5 前言 本篇适用于了解ReentrantLock或ReentrantReadWriteLock的使用,但想要进一步 ...

  2. IE兼容css3圆角的htc解决方法

    IE兼容css教程3圆角的htc解决方法 现在css3的border-radius属性可以很方便的实现圆角功能,对网站前台人员无疑是一件喜事,但悲剧的是IE6/7/8并不支持,让我们弃新技术不用,是不 ...

  3. javascript 中function(){}(),new function(),new Function(),Function

    和java比起来,javascript真的是松散的无以复加,不过这也让我们在无聊之余,有精力去探讨一些复杂的应用,从而在开发之路上,获得一些新的想法. javascript中的类的构造 javascr ...

  4. Solr学习笔记(3) —— SolrJ管理索引库&集群

    一.什么是SolrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图: 二.SolrJ的基本 ...

  5. Educational Codeforces Round 13 B

    Description The girl Taylor has a beautiful calendar for the year y. In the calendar all days are gi ...

  6. jq ajax超时设置

    var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...

  7. Java中利用JFrame创建窗体

    1. 一个简单例子: public class Test(){ public static void main(String[] args){ JFrame frame = new JFrame(); ...

  8. Python 基础学习之字典

    1.基础代码 ##第六章 字典学习 test={'} print(test['a']) print(test['b']) 返回结果为: black123 2.基础知识: Python中的字典是一系列键 ...

  9. sp_executesq用法

    第一种用法: --@sqlstring :就是你要执行的sql语句字符串--@ParmDefinition: @sqlstring里边用到的参数在这里声明 输出的参数要加output --sp_exe ...

  10. ajax 工作原理以及其优缺点

    1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...