POJ2114-Boatherds
2017-10-16 20:51
204 查看
Boatherds
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
Sample Output
Source
CTU Open 2004
题意:给你一棵树,有m次询问,每次询问存不存在两个点的距离为k
解题思路:树分治
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int n, m, x, w;
int s[maxn], nt[maxn], e[maxn], val[maxn], cnt;
int sum[maxn], mx[maxn], dis[maxn], vis[maxn],tot;
int a[maxn],flag[maxn],sz;
map<int, int>mp;
int dfs(int k, int fa, int p)
{
int ans = mx[k] = (sum[k] = 1) - 1;
for (int i = s[k]; ~i; i = nt[i])
{
if (e[i] == fa || vis[e[i]]) continue;
int temp = dfs(e[i], k, p);
sum[k] += sum[e[i]];
mx[k] = max(mx[k], sum[e[i]]);
if (mx[temp] < mx[ans]) ans = temp;
}
mx[k] = max(mx[k], p - sum[k]);
return mx[k] < mx[ans] ? k : ans;
}
void get(int k, int fa, int len)
{
dis[tot++] = len;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]] || e[i] == fa) continue;
get(e[i], k, len + val[i]);
}
}
void Find(int k)
{
mp.clear(), mp[0] = 1;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
tot = 0;
get(e[i], k, val[i]);
for (int j = 0; j < sz; j++)
{
if (flag[j]) continue;
for (int k = 0; k < tot; k++)
if (mp.count(a[j] - dis[k])) { flag[j] = 1; break; }
}
for (int j = 0; j < tot; j++) mp[dis[j]] = 1;
}
}
void solve(int k, int p)
{
int y = dfs(k, k, p);
Find(y), vis[y] = 1;
for (int i = s[y]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
if (sum[e[i]] < sum[y]) solve(e[i], sum[e[i]]);
else solve(e[i], p - sum[y]);
}
vis[y] = 0;
}
int main()
{
while (~scanf("%d", &n)&&n)
{
memset(s, -1, sizeof s);
mx[cnt = 0] = INF;
for (int i = 1; i <= n; i++)
{
while (scanf("%d", &x) && x)
{
scanf("%d", &w);
nt[cnt] = s[x], s[x] = cnt, e[cnt] = i, val[cnt++] = w;
nt[cnt] = s[i], s[i] = cnt, e[cnt] = x, val[cnt++] = w;
}
}
sz = 0;
while (scanf("%d", &a[sz]) && a[sz]) flag[sz++] = 0;
solve(1, n);
for (int i = 0; i < sz; i++)
{
if (flag[i]) printf("AYE\n");
else printf("NAY\n");
}
printf(".\n");
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3049 | Accepted: 1001 |
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
题意:给你一棵树,有m次询问,每次询问存不存在两个点的距离为k
解题思路:树分治
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int n, m, x, w;
int s[maxn], nt[maxn], e[maxn], val[maxn], cnt;
int sum[maxn], mx[maxn], dis[maxn], vis[maxn],tot;
int a[maxn],flag[maxn],sz;
map<int, int>mp;
int dfs(int k, int fa, int p)
{
int ans = mx[k] = (sum[k] = 1) - 1;
for (int i = s[k]; ~i; i = nt[i])
{
if (e[i] == fa || vis[e[i]]) continue;
int temp = dfs(e[i], k, p);
sum[k] += sum[e[i]];
mx[k] = max(mx[k], sum[e[i]]);
if (mx[temp] < mx[ans]) ans = temp;
}
mx[k] = max(mx[k], p - sum[k]);
return mx[k] < mx[ans] ? k : ans;
}
void get(int k, int fa, int len)
{
dis[tot++] = len;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]] || e[i] == fa) continue;
get(e[i], k, len + val[i]);
}
}
void Find(int k)
{
mp.clear(), mp[0] = 1;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
tot = 0;
get(e[i], k, val[i]);
for (int j = 0; j < sz; j++)
{
if (flag[j]) continue;
for (int k = 0; k < tot; k++)
if (mp.count(a[j] - dis[k])) { flag[j] = 1; break; }
}
for (int j = 0; j < tot; j++) mp[dis[j]] = 1;
}
}
void solve(int k, int p)
{
int y = dfs(k, k, p);
Find(y), vis[y] = 1;
for (int i = s[y]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
if (sum[e[i]] < sum[y]) solve(e[i], sum[e[i]]);
else solve(e[i], p - sum[y]);
}
vis[y] = 0;
}
int main()
{
while (~scanf("%d", &n)&&n)
{
memset(s, -1, sizeof s);
mx[cnt = 0] = INF;
for (int i = 1; i <= n; i++)
{
while (scanf("%d", &x) && x)
{
scanf("%d", &w);
nt[cnt] = s[x], s[x] = cnt, e[cnt] = i, val[cnt++] = w;
nt[cnt] = s[i], s[i] = cnt, e[cnt] = x, val[cnt++] = w;
}
}
sz = 0;
while (scanf("%d", &a[sz]) && a[sz]) flag[sz++] = 0;
solve(1, n);
for (int i = 0; i < sz; i++)
{
if (flag[i]) printf("AYE\n");
else printf("NAY\n");
}
printf(".\n");
}
return 0;
}
相关文章推荐
- POJ 2114 Boatherds【Tree,点分治】
- poj 2114 Boatherds (点分治)
- POJ 2114 Boatherds 树分治
- POJ 2114 Boatherds 划分树
- POJ 2114 Boatherds 树的分治
- 【poj2114】 Boatherds
- 【POJ】2114 Boatherds 点分治
- poj 2114 Boatherds 树的分治
- POJ 2114 Boatherds (树上点分治)
- POJ2114 Boatherds 点分治
- POJ 2114 Boatherds 树分治
- POJ 2114 Boatherds
- POJ 2114 Boatherds 点分治
- poj 2114 Boatherds 树的分治
- poj 2114 Boatherds
- POJ 2114 Boatherds 笔记
- POJ 2114 Boatherds【Tree,点分治】
- POJ 2114 Boatherds
- POJ 题目2114 Boatherds(树的点分治)
- [POJ2114]Boatherds(点分治+二分)