树分治(hdu5016-2014西安现场赛)
来源:互联网 发布:易安卓播放器源码 编辑:程序博客网 时间:2024/04/30 07:23
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?
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 bi, ei and wi, (1 ≤ bi,ei ≤ n,1 ≤ wi ≤ 10000), indicates that there’s one road connecting city bi and ei, and its length is wi.
Last line : n(1 ≤ n ≤ 105) 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.
In each test case:
First line: an integer n indicating the number of districts.
Next n - 1 lines: each contains three numbers bi, ei and wi, (1 ≤ bi,ei ≤ n,1 ≤ wi ≤ 10000), indicates that there’s one road connecting city bi and ei, and its length is wi.
Last line : n(1 ≤ n ≤ 105) 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
51 2 12 3 13 4 14 5 11 0 0 0 151 2 12 3 13 4 14 5 11 0 0 0 01110
Sample Output
2401
Source
2014 ACM/ICPC Asia Regional Xi'an Online
题意:有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;}
0 0
- 树分治(hdu5016-2014西安现场赛)
- 2014西安网络预选赛1009(点分治)HDU5016
- hdu5073 -- 贪心(2014西安现场赛)
- 小记2014西安现场赛
- gym 100548题解(2014年西安现场赛(回文树,01字典树,容斥))
- 2014西安现场赛I题 International Collegiate Routing Contest(字典树)
- 2014 icpc 西安现场赛 I
- 【2014acm西安现场赛】A
- 【2014acm西安现场赛】K
- 【2014acm西安现场赛】F
- 鞍山&西安现场赛小结
- 2014 ACM区域赛 西安赛区现场赛总结
- 2014ACM/ICPC亚洲区西安站现场赛 F color(转换角度+容斥)
- CF GYM 100548 Last Defence(2014ACM西安现场赛Problem K)
- CF GYM 100548 International Collegiate Routing Contest(2014ACM西安现场赛Problem I)
- CF GYM 100548 Color(2014ACM西安现场赛Problem F)
- CF GYM 100548 Color(2014ACM西安现场赛Problem F)
- HDU5016 Mart Master II【点分治】
- OC中简单内存开辟 初始化
- 第八周【项目1-实现复数类中的运算符重载】
- 疯狂JAVA之学习笔记(5)----------注释
- 基于protobuf的RPC实现
- shell条件判断之test命令实例详解
- 树分治(hdu5016-2014西安现场赛)
- UITableView---初始化
- 深入剖析jsp的工作原理
- SQL with as 的用法 以及递归函数的写法
- 神经网络与机器学习导言笔记——网络结构与知识表示
- 清除float常用方法(:after和clear:both)
- Unity3D修改脚本模板
- String,StringBuilder,String Buffer的一点小结
- GRE、PPTP、L2TP隧道协议