ACdream 1015
来源:互联网 发布:ovid数据库的网址 编辑:程序博客网 时间:2024/06/06 19:42
Double Kings
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem
Problem Description
Our country is tree-like structure, that is to say that N cities is connected by exactly N - 1 roads.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
Input
There are multiple test cases.
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city aand city b. (1 ≤ a, b ≤ N)
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city aand city b. (1 ≤ a, b ≤ N)
Output
For each test case, output an integer indicating the number of cities who will choose elder son as their king.
Sample Input
4 1 22 33 441 21 31 4
Sample Output
23
Source
dut200901102
题意:
大儿子先选一个作为首都,小儿子后选,每个点选择离自己近的首都作为king。
大小儿子都很聪明,求出大儿子最多可以为king的点个数。
POINT:
分析可知,小儿子肯定是要选在大儿子的旁边最优。
我们遍历大儿子的选择,在在周围遍历小儿子的选择。用记忆化搜索就行了。
#include <iostream>#include <string.h>#include <math.h>#include <algorithm>#include <stdio.h>using namespace std;#define LL long longconst int maxn = 50000*2+43;const int inf = 0x3f3f3f3f;int n,m,sz=0;int head[maxn],to[maxn],nxt[maxn];int flag[maxn];void add(int u,int v){ to[sz]=v; nxt[sz]=head[u]; head[u]=sz++;}int ans;int dfs(int now,int pre){ int num=1; for(int i=head[now];~i;i=nxt[i]){ int v=to[i]; if(v==pre) continue; if(flag[i]!=0) { num+=flag[i]; continue; } int a=dfs(v,now); num+=a; flag[i]=a; } return num;}void doit(int x){ int sum=1; int num[maxn]; int cnt=0; for(int i=head[x];~i;i=nxt[i]){ int v=to[i]; int a=dfs(v,x); flag[i]=a; num[++cnt]=a; sum+=a; } int Min=inf; if(cnt==0) Min=0; for(int i=1;i<=cnt;i++){ Min=min(Min,sum-num[i]); } ans=max(ans,Min);}int main(){ while(~scanf("%d",&n)){ sz=0; for(int i=1;i<=n;i++) head[i]=-1; memset(flag,0,sizeof flag); for(int i=2;i<=n;i++){ int u,v; scanf("%d %d",&u,&v); add(u,v); add(v,u); } ans=1; for(int i=1;i<=n;i++){ doit(i); } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- ACdream 1015
- ACdream 1015Double Kings
- ACdream 1015 A
- ACdream 1015Double Kings
- ACdream
- Acdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- acdream 1015Double Kings 树形结构 贪心
- 【教程】html+css零基础入门教程之 伪类(三十)
- OpenSSL自签发配置有多域名或ip地址的证书
- matplotlib例子
- 理解RxJava(三)线程调度原理分析
- 乱搞向——二维坐标系曼哈顿距离和切比雪夫距离转换的简要数学证明
- ACdream 1015
- 进程、线程
- cmd中的%~dp0
- redis学习记录
- Codeforces Round #448 (Div. 2)D. String Mark(组合数学)
- 程序员午夜随笔
- MySQL简单语法(2)
- Spring Cloud(三)服务提供者 Eureka + 服务消费者(rest + Ribbon)
- javaweb 后台和前台合作处理跨域问题