ACdream 1028 Path
来源:互联网 发布:骨朵网络剧排行榜 编辑:程序博客网 时间:2024/06/03 05:03
那么给出一条路径,长度为X,(X>2),我们考虑他的两端,可能全为1,可能也有2,如果把边权为2的边删掉,或者把两端边权为1的都删了,那么必然存在一条长度为X-2的边。
如果
Path
Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext Problem
Problem Description
Check if there exists a path of length
Input
The first line contains two integers
The following
Note that the nodes are labled by
The last line contains
Output
For each query, print the result in seperated line. If there exists path of given length, print "Yes". Otherwise, print "No".
Sample Input
4 61 2 22 3 13 4 20 1 2 3 4 5
Sample Output
YesYesYesYesNoYes
Source
ftiasch
Manager
nanae
SubmitStatistic
const int inf = 0x3f3f3f3f;const int N = 201010;int n, Q;int fa[N];int f(int x){ return x==fa[x] ? x : fa[x] = f(fa[x]);}#define UPD(x, a) x=max(x,a);int to[N], head[N], cost[N], next[N];int en;void init_edge(){ en = 0; memset(head, -1, sizeof head); for(int i=1; i<=n; i++) fa[i] = i;}void add(int u, int v, int w){ to[++en] = v, cost[en] = w; next[en] = head[u]; head[u] = en;}int down[N][2];int lim[N];void dfs(int u, int p){ down[u][0] = 0; down[u][1] = -inf; for(int i=head[u]; ~i; i=next[i]) { int v = to[i], w = cost[i]; if(v == p) continue; dfs(v, u); for(int x = 0; x<2; x++) for(int y=0; y<2; y++) { UPD(lim[x+y+w & 1], down[u][x] + down[v][y] + w); } for(int y=0; y<2; y++) UPD(down[u][y+w & 1], down[v][y] + w); }}int main(){ while (cin >> n >>Q) { init_edge(); for (int i=1; i<n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v, w); add(v, u, w); fa[f(u)] = f(v); } lim[0] = lim[1] = -inf; dfs(1, -1); while (Q --) { int l; scanf("%d", &l); if (l < 0) puts("No"); else { puts(l <= lim[l%2] ? "Yes" : "No"); } } }}
0 0
- ACdream 1028 Path
- ACdream 1028Path
- ACdream 1028
- ACdream 1028
- ACdream
- Acdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream 1022
- private, public, protected
- 组合,关联,聚合的区别
- Problem 007——UVa 1585 - Score
- 【Linux】Linux下配置apache - 安装目录详解
- 实现自己的printf函数
- ACdream 1028 Path
- log4j 详细使用
- JAVA基础练习题总结
- 设计模式---------工厂方法模式
- Extjs 4.0 查询翻页问题
- oracle 字符替换 replace
- openWRT上的nginx服务器搭建
- 剑指offer---和为s的两个数字VS和为s的连续正数序列
- 使用 /sys 文件系统访问 Linux 内核