树分治(hdu5016-2014西安现场赛)

Mart Master II

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 328    Accepted Submission(s): 108


Problem Description
Trader Dogy lives in city S, which consists of n districts. There are n - 1 bidirectional roads in city S, each connects a pair of districts. Indeed, city S is connected, i.e. people can travel between every pair of districts by roads.

In some districts there are marts founded by Dogy’s competitors. when people go to marts, they’ll choose the nearest one. In cases there are more than one nearest marts, they’ll choose the one with minimal city number.

Dogy’s money could support him to build only one new marts, he wants to attract as many people as possible, that is, to build his marts in some way that maximize the number of people who will choose his mart as favorite. Could you help him?
 

Input
There are multiple test cases. Please process till EOF.

In each test case:

First line: an integer n indicating the number of districts.

Next n - 1 lines: each contains three numbers b i, e i and w i, (1 ≤ b i,e i ≤ n,1 ≤ w i ≤ 10000), indicates that there’s one road connecting city b i and e i, and its length is w i.

Last line : n(1 ≤ n ≤ 10 5) numbers, each number is either 0 or 1, i-th number is 1 indicates that the i-th district has mart in the beginning and vice versa.
 

Output
For each test case, output one number, denotes the number of people you can attract, taking district as a unit.
 

Sample Input
  
  
5 1 2 1 2 3 1 3 4 1 4 5 1 1 0 0 0 1 5 1 2 1 2 3 1 3 4 1 4 5 1 1 0 0 0 0 1 1 1 0
 

Sample Output
  
  
2 4 0 1
 

Source


题意:有n个城市,有些城市被设定成集市 ,每个城市只能到离他最近的并且编号最小的集市,现在如果再建一个集市,那么最多有多少个城市可以到这个集市

思路:首先用最短路求出每个城市,离他最近的集市的距离和集市的编号,然后进行树分治,假设当前的重心为rt,对于树上的节点u,v,如果dis[u]+dis[v]<near[v](dis表示节点到rt的距离,near表示接地离最近集市的距离),那么如果在节点u建立集市,那么节点v肯定会到u,把式子变形,得到dis[u]<near[v]-dis[v];那么只需要二分就可以求出有多少个节点到达u

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=1000000000;
int N;
int ans[maxn];
int type[maxn];
int head[maxn],tot;
int vis[maxn];
int size[maxn],maxv[maxn];
pair<int,int> np[maxn];
pair<int,int> pp[maxn];
int Max,root;
struct node
{
    int v,next,w;
}edge[maxn*2];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add_edge(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void SPFA()
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=N;i++)
    {
        if(type[i])
        {
            q.push(i);
            vis[i]=1;
            np[i]=make_pair(0,i);
        }
        else np[i]=make_pair(INF,0);
    }
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(np[v].first>np[u].first+edge[i].w)
            {
                np[v]=make_pair(np[u].first+edge[i].w,np[u].second);
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
void dfssize(int u,int fa)
{
    size[u]=1;
    maxv[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa||vis[v])continue;
        dfssize(v,u);
        size[u]+=size[v];
        if(size[v]>maxv[u])maxv[u]=size[v];
    }
}
void dfsroot(int r,int u,int f)
{
    if(size[r]-size[u]>maxv[u])
        maxv[u]=size[r]-size[u];
    if(maxv[u]<Max)Max=maxv[u],root=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f||vis[v])continue;
        dfsroot(r,v,u);
    }
}
int ff[maxn];
int cnt;
int dis[maxn];
void dfsdis(int u,int fa,int d)
{
    ff[cnt++]=u;
    dis[u]=d;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v]||v==fa)continue;
        dfsdis(v,u,d+edge[i].w);
    }
}
void calc(int u,int d)
{
    cnt=0;
    dfsdis(u,0,d);
    for(int i=0;i<cnt;i++)
        pp[i].first=np[ff[i]].first-dis[ff[i]],pp[i].second=np[ff[i]].second;
    sort(pp,pp+cnt);
    for(int i=0;i<cnt;i++)
    {
        if(!type[ff[i]])
        {
            pair<int,int> tmp;
            tmp.first=dis[ff[i]],tmp.second=ff[i];
            int p=lower_bound(pp,pp+cnt,tmp)-pp;
            ans[ff[i]]+=(d>0?p-cnt:cnt-p);
        }
    }
}
void dfs(int u)
{
    Max=INF;
    dfssize(u,0);
    dfsroot(u,u,0);

    calc(root,0);
    vis[root]=1;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])continue;
        calc(v,edge[i].w);
        dfs(v);
    }
}
void solve()
{
    memset(ans,0,sizeof(ans));
    memset(vis,0,sizeof(vis));
    dfs(1);
    int maxv=0;
    for(int i=1;i<=N;i++)
        maxv=max(ans[i],maxv);
    printf("%d\n",maxv);
}
int main()
{
    int u,v,w;
    while(scanf("%d",&N)!=EOF)
    {
        init();
        for(int i=1;i<N;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w);
            add_edge(v,u,w);
        }
        for(int i=1;i<=N;i++)scanf("%d",&type[i]);
        SPFA();
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值