POJ 1987 Distance Statistics 树分治
2016-07-21 19:43
405 查看
[b]Distance Statistics[/b]
[b]Description[/b]
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).
[b]Input[/b]
* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"
* Line M+2: A single integer, K.
[b]Output[/b]
* Line 1: The number of pairs of farms that are at a distance of at most K from each-other.
[b]Sample Input[/b]
[b]Sample Output[/b]
[b]Hint[/b]
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).
[b]题解:[/b]
POJ 1741
http://www.cnblogs.com/zxhl/p/5692688.html
[b]Description[/b]
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).
[b]Input[/b]
* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"
* Line M+2: A single integer, K.
[b]Output[/b]
* Line 1: The number of pairs of farms that are at a distance of at most K from each-other.
[b]Sample Input[/b]
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 10
[b]Sample Output[/b]
5
[b]Hint[/b]
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).
[b]题解:[/b]
POJ 1741
http://www.cnblogs.com/zxhl/p/5692688.html
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 4e4+20, M = 1e2+10, mod = 1e9+7, inf = 1e9+1000; typedef long long ll; int ans, n,m,root , t = 1,K,siz ,head ,f ,deep ,d ,allnode,vis ; struct edg{int to,next,v,w;}e[N * 4]; void add(int u,int v,int w) {e[t].to=v;e[t].v=w;e[t].next=head[u];head[u]=t++;} void getroot(int x,int fa) { siz[x] = 1; f[x] = 0; for(int i=head[x];i;i=e[i].next) { int to = e[i].to; if(to == fa || vis[to]) continue; getroot(to,x); siz[x] += siz[to]; f[x] = max(f[x] , siz[to]); } f[x] = max(f[x] , allnode - siz[x]); if(f[x] < f[root]) root = x; } void getdeep(int x,int fa) { if(d[x] <= K) deep[++deep[0]]=d[x]; for(int i=head[x];i;i=e[i].next) { int to = e[i].to; if(to == fa || vis[to]) continue; d[to] = d[x] + e[i].v; getdeep(to,x); } } int cal(int x,int now) { d[x]=now;deep[0] = 0; getdeep(x,0); sort(deep+1,deep+deep[0]+1); int all = 0; for(int l=1,r=deep[0];l<r;) { if(deep[l]+deep[r] <= K) {all+=r-l;l++;} else r--; } return all; } void work(int x) { ans+=cal(x,0); vis[x] = 1; for(int i=head[x];i;i=e[i].next) { int to = e[i].to; if(vis[to]) continue; ans-=cal(to,e[i].v); allnode = siz[to]; root = 0; getroot(to,root); work(root); } } void init() { memset(head,0,sizeof(head)); t = 1; ans = root = 0; memset(vis,0,sizeof(vis)); } int main() { while(~scanf("%d%d",&n,&m)) { init(); for(int i=1;i<n;i++) { int a,b,c;char ch[2]; scanf("%d%d%d%s",&a,&b,&c,ch); add(a,b,c) , add(b,a,c); } scanf("%d",&K); allnode=n;f[0]=inf; getroot(1,0); work(root); printf("%d\n",ans); } }
相关文章推荐
- 开头的是什么编码呢。浏览器可以解释它。如中国等同与中文"中国"?
- OTG
- 继承、实现、依赖、关联、聚合、组合
- 浅谈C语言中结构体的初始化
- var tensquared=(function(x) {return x*x;}(10)); //这是一个函数表达式 问:为什么花括号后面还有一个(10)的?而且整个句子用()扩起来?这个是什么表
- Java 用户登录系统
- 空心三角形
- C - Sacrament of the sum
- 《OpenCV2计算机视觉编程手册》2-8定义感兴趣区域(打水印)
- 腾讯信鸽自定义推送通知
- hdu5744 Keep On Movin(水)
- KafkaStreams介绍(二)
- Remove Duplicates from Sorted Array II
- 【大话Hibernate】Hibernate的核心接口和类
- Notepad++ 快捷键整理
- Linux Wireless Networking: a short walk
- AsyncTask-异步任务
- IOS 9.3.3更后打电话没声音解决方法
- 循环队列java实现
- Linux命令