ACdream - 1015 - Double Kings (DFS)

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.
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)
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


题意:一个生成树,每个点代表一个城市,其中有两个城市分别属于大王子和二王子作为首都,每个城市归属于离它较近的首都,若相同则归属给大王子的首都

哪个首都归属城市多哪个王子成为国王,现要求大王子成为国王时有多少个城市归属于它


注意n=1的情况

因为图是个生成树,所以二王子一定是选在大王子的旁边,这样可以抢占最多的城市归属,就是dfs遍历一下大王子选取的城市

先以任意一点作为大王子第一个选取的城市,然后dfs下去求出以它为最高点其他点向下含有的点的总数之和,就是每个点所连接的城市

然后再dfs一次求出答案


#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N = 1e5+10;
int n,x,y,v[N],vis[N],ans;
int ft[N], nt[N], u[N], sz;
void dfs(int now){
    v[now] = vis[now] = 1;
    if(ft[now]==-1) return;
    for(int i=ft[now];;i=nt[i]){
        if(!vis[u[i]]) {
            dfs(u[i]);
            v[now] += v[u[i]];
        }
        if(nt[i]==-1) break;
    }
}
void dfs2(int now){
    vis[now] = 1;
    int mx = 0;
    if(ans) return;
    for(int i=ft[now];;i=nt[i]){
        mx = max(mx, v[u[i]]);
        if(nt[i]==-1) break;
    }
    if(v[now]-mx>=mx){
        ans = v[now] - mx;
        return;
    }
    for(int i=ft[now];;i=nt[i]){
        if(!vis[u[i]]){
            v[now] = v[now] - v[u[i]];
            v[u[i]] = v[now] + v[u[i]];
            dfs2(u[i]);
            v[u[i]] = v[u[i]] - v[now];
            v[now] = v[now] + v[u[i]];
        }
        if(nt[i]==-1) break;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        if(n==1){
            printf("1\n");
            continue;
        }
        sz = ans = 0;
        memset(ft,-1,sizeof ft);
        memset(vis,0,sizeof vis);
        memset(v,0,sizeof v);
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++;
            u[sz] = x; nt[sz] = ft[y]; ft[y] = sz++;
        }
        dfs(1);
        memset(vis,0,sizeof vis);
        dfs2(1);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值