Hdu 5416 CRB and Tree (bfs)
题目链接:
题目描述:
给一棵树有n个节点,树上的每条边都有一个权值。f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v)有多少中情况(u, v有可能相同)?
解题思路:
由于xor的特殊性质。x^x=0,对于求f(u, v) == f(u, 1) ^ f(1, u)。 又因为x^y == z可以推出x^z == y,对于f(u, 1) ^ f(1, v) == m可以转化为m ^ f(1, v) == f(u, 1)。
可以先遍历一遍树,然后记录下来根节点到每个节点所经过路径的xor值,hash保存xor值出现的次数,然后枚举节点v即可。复杂度O(n*q)
hash表要开到pow(2,18)左右,如果小的话会wa。
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = ;
struct node
{
int to, next, w;
}edge[maxn]; int arr[maxn], ans[maxn];
int head[maxn], tot; void Add (int from, int to, int w)
{
edge[tot].to = to;
edge[tot].w = w;
edge[tot].next = head[from];
head[from] = tot++;
}
void bfs ()
{
queue <int> Q;
Q.push();
arr[] = ;
while (!Q.empty())
{
int u = Q.front ();
Q.pop();
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (arr[v] == -)
{
arr[v] = arr[u] ^ edge[i].w;
Q.push(v);
}
}
}
} int main ()
{
int t, n, q, m;
scanf ("%d", &t);
while (t --)
{
memset (head, -, sizeof(head));
memset (ans, , sizeof(ans));
memset (arr, -, sizeof(arr));
tot = ; scanf ("%d", &n);
for (int i=; i<n; i++)
{
int x, y, z;
scanf ("%d %d %d", &x, &y, &z);
Add (x, y, z);
Add (y, x, z);
} bfs ();
for (int i=; i<=n; i++)
ans[arr[i]] ++; scanf ("%d", &q);
while (q --)
{
LL res = ;
scanf ("%d", &m);
for (int i=; i<=n; i++)
res += ans[m^arr[i]];
if (m == )
res += n;
printf ("%lld\n", res/);
}
}
return ;
}
Hdu 5416 CRB and Tree (bfs)的更多相关文章
- 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——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- HDOJ 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 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 5416 CRB and Tree
题目传送门 题意:给一棵树,问f (u, v) 意思是u到v的所有路径的边权值的异或和,问f (u, v) == s 的u,v有几对 异或+构造:首先计算f (1, u) 的值,那么f (u, v) ...
随机推荐
- Jupyter Notebook 基本使用
Jupyter 官网 IPython Interactive Computing IPython Notebook使用浏览器作为界面,向后台的IPython服务器发送请求,并显示结果.在浏览器的界面中 ...
- Working with Validators and Messages in AngularJS
原文:http://odetocode.com/blogs/scott/archive/2014/10/16/working-with-validators-and-messages-in-angul ...
- [libcurl]_[0基础]_[使用libcurl下载大文件]
场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...
- STL vector的介绍(1)
尝试下翻译STL里面的一些easy和算法.四级过了.六级刚考.顺便练练自己的英语水平.翻译的不好的地方请大神多多不吝赐教哈.方便我改正. 原来均来自:http://www.cplusplus.com/ ...
- @Html.ValidationMessageFor客户端验证
<%=Html.LabelFor(model => model.sUser) %><%=Html.TextBoxFor(model => model.sUser) %&g ...
- H5的localStorage简单存储删除
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- swift中的@objc的作用
转载:https://www.jianshu.com/p/6c5b45d9d042 自动清除冗余代码减小包大小 得益于 Swift 的静态语言特性,每个函数的调用在编译期间就可以确定.因此在编译完成后 ...
- Lightoj 1012 - Guilty Prince
bfs遍历一遍就行了. /* *********************************************** Author :guanjun Created Time :2016/6/ ...
- jfreechart应用1--环境配置
jfreechart应用1--环境配置 JFreeChart是一组功能强大.灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图.饼图.曲线图.甘特图等.它能够用在Swing和We ...
- Webdriver中关于driver.navigate().to()和driver.get()使用的区别
先是有一个父页上button弹开一个子页,总共有4个子页,必须前一个页上的必填信息录完,才能在这个页面触发下一个子页. 用driver.navigate().to(baseUrl2),直接跳转到第2个 ...