HDU 5416
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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?
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.
3
1 2 1
2 3 2
3
2
3
4
1
0
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.
///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define push_back pb
#define mem(a) memset(a,0,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a) scanf("%d",&a)
#define mod 1000000007
#define inf 100000
#define maxn 300000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//******************************************************************
struct ss
{
int to,next,v;
}e[];
int vis[],head[],t,n;
ll hashs[];
void add(int u,int v,int w)
{
ss kk={v,head[u],w};
e[t]=kk;
head[u]=t++;
}
void dfs(int x,int pre)
{
hashs[pre]++;
vis[x]=;
for(int i=head[x];i;i=e[i].next)
{//TS;
if(vis[e[i].to])continue;
dfs(e[i].to,pre^e[i].v);
}
}
int main()
{
int a,b,c;
int T=read();
while(T--)
{
t=;mem(head);mem(hashs);mem(vis);
n=read();
FOR(i,,n-)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
//cout<<t<<endl;
dfs(,);
/// FOR(i,0,3)cout<<hashs[i]<<" ";
int q=read();
FOR(i,,q)
{ ll ans=;
a=read(); for(int j=;j<=maxn;j++)
{if(!hashs[j])continue;
if((a^j)==j)ans+=(hashs[j]*(hashs[j]-));
else {
ans+=(hashs[j]*hashs[a^j]);
}
//if(ans!=0)cout<<j<<" "<<ans<<endl;
}ans=ans/;
if(a==)ans+=n;
cout<<ans<<endl;
} }
return ;
}
代码
HDU 5416的更多相关文章
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Hdu 5416 CRB and Tree (bfs)
题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...
- HDU 5416 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...
- HDU 5416——CRB and Tree——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
CRB and Tree Time Limit: 8000/4000 MS (J ...
- HDU 5416 CRB and Tree (2015多校第10场)
欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5416 CBR and tree
#include<bits/stdc++.h> using namespace std; #define for(i,a,b) for(int i=a;i<=b;++i) //T,N ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- mybatis传多个参数(不使用@param注解情况下),3.4.2版本之后出现#{0}-#{n}参数绑定异常
解决方案: 在mybatis配置文件中声明setting属性的useActualParamName 参数值为false ** 这种方法解决mybatis3.4.2之后的版本产生该问题的解决方法**
- openwrt 配置samba && ubuntu 配置samba
前言:在修改opkg update的下载目录,公司里不能连外网,尝试用samba. 配置samba很简单,修改/etc/config/samba文件,拷贝一下share项,再改一下name就可以了. ...
- Linux命令学习(6):paste合并几列文件
如果我们有三个文件: $ cat name.txt #姓名文档 Kevin Mary Tom $ cat gender.txt #性别文档 M F M $ cat age.txt #年龄文档 我们想把 ...
- laravel框架应用和composer扩展包开发
laravel5.5+ laravel官方地址 laravel是目前最流行的php框架,发展势头迅猛,应用非常广泛,有丰富的扩展包可以应付你能想到的各种应用场景,laravel框架思想前卫,跟随时代潮 ...
- 【二维树状数组】See you~
https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A ...
- bzoj 1413 [ZJOI2009]取石子游戏
1413: [ZJOI2009]取石子游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 747 Solved: 490[Submit][Statu ...
- [USACO08NOV]时间管理Time Management
题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- 创建Django项目(四)——模型
2013-08-06 22:24:06| 1.创建模型 (1) "mysite\blog\models.py"文件中的内容: # -*- co ...
- jquery控制Request Payload和Form Data
Request Payload方式,会发起两次请求 Form Data只发起一次请求 若要把一个ajax请求改为Payload方式,设置contentType即可,发现请求参数不是对象,再把参数转换为 ...