POJ 1987Distance Statistics
来源:互联网 发布:笔记本强力卸载软件 编辑:程序博客网 时间:2024/04/28 03:40
Description
Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K <= 1,000,000,000) and wants to know how many pairs of farms lie at a distance at most K from each other (distance is measured in terms of the length of road required to travel from one farm to another). Please only count pairs of distinct farms (i.e. do not count pairs such as (farm #5, farm #5) in your answer).
Input
* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"
* Line M+2: A single integer, K.
* Line M+2: A single integer, K.
Output
* Line 1: The number of pairs of farms that are at a distance of at most K from each-other.
Sample Input
7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S10
Sample Output
5
Hint
There are 5 roads with length smaller or equal than 10, namely 1-4 (3), 4-7 (2), 1-7 (5), 3-5 (7) and 3-6 (9).
树分治,同POJ1741,这次又重新写了一遍,一A,感觉理解有深刻了一点。
#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const double pi = acos(-1.0);const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int n, m, x, y, z, K;char ch[5];struct Tree{const static int maxn = 1e5 + 10;int ft[maxn], nt[maxn], u[maxn], v[maxn], sz, tot;int dis[maxn], vis[maxn], cnt[maxn], mx[maxn];void clear(int n){mx[sz = 0] = INF;for (int i = 1; i <= n; i++){ft[i] = -1;vis[i] = 0;}}void AddEdge(int x, int y, int z){u[sz] = y;v[sz] = z;nt[sz] = ft[x];ft[x] = sz++;u[sz] = x;v[sz] = z;nt[sz] = ft[y];ft[y] = sz++;}int dfs(int x, int fa, int sum){int z = mx[x] = 0;cnt[x] = 1;for (int i = ft[x]; i != -1; i = nt[i]){if (u[i] == fa || vis[u[i]]) continue;int y = dfs(u[i], x, sum);cnt[x] += cnt[u[i]];mx[x] = max(mx[x], cnt[u[i]]);if (mx[z] > mx[y]) z = y; }mx[x] = max(mx[x], sum - cnt[x]);return mx[x] < mx[z] ? x : z;}void dfsdis(int x, int fa, int len){dis[tot++] = len;for (int i = ft[x]; i != -1; i = nt[i]){if (u[i] == fa || vis[u[i]]) continue;dfsdis(u[i], x, len + v[i]);}}LL find(int x, int d){tot = 0;dfsdis(x, -1, d);sort(dis, dis + tot);LL ans = 0;for (int i = 0, j = tot - 1; i < j; i++){while (j > i&&dis[i] + dis[j] > K) j--;ans += j - i;}return ans;}LL work(int x, int sum){int y = dfs(x, -1, sum);LL ans = find(y, 0);vis[y] = 1;for (int i = ft[y]; i != -1; i = nt[i]){if (vis[u[i]]) continue;ans -= find(u[i], v[i]);ans += work(u[i], cnt[u[i]] > cnt[y] ? sum - cnt[y] : cnt[u[i]]);}return ans;}}solve;int main(){while (scanf("%d%d", &n, &m) != EOF){solve.clear(n);while (m--){scanf("%d%d%d%s", &x, &y, &z, ch);solve.AddEdge(x, y, z);}scanf("%d", &K);printf("%d\n", solve.work(1, n));}return 0;}
0 0
- POJ 1987Distance Statistics
- POJ 1987 Distance Statistics 笔记
- POJ 1987 Distance Statistics【Tree,点分治】
- 【POJ 1987】Distance Statistics【树的分治】
- 【POJ】1987 Distance Statistics 点分治
- POJ - 1987 Distance Statistics 树上的分治
- POJ 1987 Distance Statistics 树上点分治
- POJ 1987 Distance Statistics (树上点分治)
- POJ 1987 Distance Statistics (树的分治)
- POJ 1987Distance Statistics 树的分治
- poj 1987 Distance Statistics 点分治
- POJ 1987 Distance Statistics(树的点分治)
- POJ 题目1987 Distance Statistics(树的点分治)
- 【POJ 1987】树的点分治 Distance Statistics
- POJ 1741 Tree + POJ 1987 Distance Statistics【树的点分治】
- POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)
- POJ 1987 BZOJ 3365 USACO 2004 Feb Distance Statistics 路程统计 点分治
- 【poj1987】Distance Statistics
- Java基于Socket的简单推送
- Git各种撤销操作
- 第十周第十一周阅读程序-5-c
- Cygwin/shell 常用命令
- 开发工具大全
- POJ 1987Distance Statistics
- 【LeetCode】Patching Array
- kickstart无人值守,PXE+TFTP+FTP+DHCP
- Poj1503(高精度加法)
- [Other]1/0.0f Error?
- 【JAVA】算法排序(一)——选择排序法
- codeforces 653B Bear and Compressing (dfs)
- Ubuntu搭建Openstack平台(kilo)(七.Dashboard)
- HDU 2043 密码(判断字符串存在性)