POJ 2114 Boatherds 点分治
2016-04-03 17:10
351 查看
和POJ 1714差不多。
问树中存在多少条路径使其长度为K。
Total Submissions: 1867 Accepted: 608
The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the prices of the segments along the only path between the villages.
One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen, they have asked the Abacus Calculator Makers for help.
You are given a description of the river network with costs of river segments and a sequence of integers x1,…, xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is exactly xi.
A single line containing a single integer: the number of villages N (1 <= N <= 10 000).
N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj’s are numbers of villages from which the rivers flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di can ever be equal to 1.
M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000).
The instance is finished by a single line containing the number 0.
The whole input is ended by a single line containing the number 0.
Output for each instance must be followed by a single line containing just the dot character.
问树中存在多少条路径使其长度为K。
#include <cstdio> #include <algorithm> #include <cstring> const int N = 10005, M = 50005, inf = 2147483647; #define adj(i,j) for(int i=h[j];i;i=p[i])if(v[i]!=fa&&!vis[i]) using namespace std; int h , p[M], w[M], v[M], g , sz , dis , cnt; int n, k, vis[M], ans, rt, c, node; void add(int a, int b, int c) { v[++cnt] = b; w[cnt] = c; p[cnt] = h[a]; h[a] = cnt; } void root(int x, int fa) { sz[x] = 1; g[x] = 0; for (int i = h[x]; i; i = p[i]) if (v[i] != fa && !vis[i]) { root(v[i], x); sz[x] += sz[v[i]]; g[x] = max(g[x], sz[v[i]]); } g[x] = max(g[x], node - sz[x]); if (g[x] < g[rt]) rt = x; } int get_root(int x, int fa, int sz) { rt = 0; node = sz; g[0] = inf; root(x, fa); return rt; } void dfs(int x, int d, int fa) { dis[c++] = d; adj(i,x) dfs(v[i], d + w[i], x); } int calc(int x, int d) { int ans = 0, a, b; c = 0; dfs(x, d, 0); sort(dis, dis + c); for (int i = 0, j = c - 1; i < j; ) if (dis[i] + dis[j] < k) ++i; else if (dis[i] + dis[j] > k) --j; else if (dis[i] == dis[j]) { ans += (j - i) * (j - 1 + 1) / 2; break; } else { a = i; b = j; while (dis[a] == dis[i]) ++i; while (dis[b] == dis[j]) --j; ans += (i - a) * (b - j); } return ans; } void work(int x) { int fa = 0; ans += calc(x, 0); adj(i, x) { vis[i] = vis[i ^ 1] = 1; ans -= calc(v[i], w[i]); work(get_root(v[i], x, sz[v[i]])); } } int main() { int u, v, w; while(scanf("%d", &n) != EOF && (n || k)) { memset(h, 0, sizeof h); cnt = 1; for (u = 1; u <= n; u++) while (scanf("%d", &v), v) { scanf("%d", &w); add(u, v, w); add(v, u, w); } while (scanf("%d", &k), k) { memset(vis, 0, sizeof vis); ans = 0; work(get_root(1, 0, n)); puts(ans ? "AYE" : "NAY"); } puts("."); } return 0; }
Boatherds
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 1867 Accepted: 608
Description
Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to the lowlands they gradually join and finally the single resulting river flows to the sea. Moreover, the Trabantian villages are exactly at the rivers’ springs, junctions and at the mouth of the largest river. Please note that more than 2 rivers can join at a junction. However, the rivers always form a tree (with villages as vertices).The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the prices of the segments along the only path between the villages.
One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen, they have asked the Abacus Calculator Makers for help.
You are given a description of the river network with costs of river segments and a sequence of integers x1,…, xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is exactly xi.
Input
The input consists of several instances. Each instance is described by (in the following order):A single line containing a single integer: the number of villages N (1 <= N <= 10 000).
N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj’s are numbers of villages from which the rivers flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di can ever be equal to 1.
M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000).
The instance is finished by a single line containing the number 0.
The whole input is ended by a single line containing the number 0.
Output
For each instance you should produce a sequence of M lines (where M is the number of queries in the particular instance). The i-th of these lines contains the word “AYE” if there exists a pair of cities in the river network which is connected by a path of cost xi, or the word “NAY” otherwise.Output for each instance must be followed by a single line containing just the dot character.
Sample Input
6 2 5 3 7 4 1 0 0 5 2 6 3 0 0 0 0 1 8 13 14 0 0
Sample Output
AYE AYE NAY AYE .
Source
CTU Open 2004相关文章推荐
- Codeforces Round #346 (Div. 2) ABCDE
- lnmp环境下 nginx.conf的常见配置
- 1003. Emergency
- 我的linux学习之路:Git常用命令
- 11. Container With Most Water
- bzoj 1044 [HAOI2008]木棍分割(二分+贪心,DP+优化)
- 简单工厂模式
- FTP 、TFTP、HTTP
- 使用火狐插件 YSLOW
- HTML学习笔记(十四)URL
- Ruby语言基础学习十:Ruby文件输入输出
- JAVA中的管道流
- 基于AVPlayer 自定义播放器
- MySQL数据库小结---待完善
- android onSaveInstanceState方法 横坚屏切换
- 第29题:推断一个序列是否是还有一个push序列的pop序列
- 22中编程语言的HelloWorld
- Mount挂载命令使用方法
- 【湖南集训 4.2】正12面体
- Sencha Touch 历史支持