标签:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 967 Accepted Submission(s): 308
#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搜树】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4756870.html