Double Kings
Time Limit: 2000/1000MS (Java/Others)
Memory Limit: 128000/64000KB (Java/Others)
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 2 2 3 3 4 4 1 2 1 3 1 4
Sample Output
2 3
Source
dut200901102
题意:
大儿子先选一个作为首都,小儿子后选,每个点选择离自己近的首都作为king。
大小儿子都很聪明,求出大儿子最多可以为king的点个数。
POINT:
分析可知,小儿子肯定是要选在大儿子的旁边最优。
我们遍历大儿子的选择,在在周围遍历小儿子的选择。用记忆化搜索就行了。
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define LL long long
const 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;
}