hdu 5029树链剖分+线段树
来源:互联网 发布:手机定时提醒软件 编辑:程序博客网 时间:2024/06/05 09:06
Relief grain
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 437 Accepted Submission(s): 99
Problem Description
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
Input
The input consists of at most 25 test cases.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
Output
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
Sample Input
2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
Sample Output
1223302HintFor the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}.
Source
2014 ACM/ICPC Asia Regional Guangzhou Online
#pragma comment(linker, "/STACK:102400000,102400000")#include<stdio.h>#include<algorithm>#include<string.h>#define N 100010#include<vector>using namespace std;vector<int>add[N],del[N];struct node{ int u,v,next;}bian[N*2];struct pp{ int x,y; int col;//区间当中数目最多的那个数 int num;//区间当中最大的重复次数}a[N*4];int e,head[N],sz[N],dep[N],son[N],father[N],ti[N],top[N],id,dui[N],ans[N];void adge(int u,int v){ bian[e].u=u; bian[e].v=v; bian[e].next=head[u]; head[u]=e++;}int max(int a,int b){ return a>b?a:b;}void dfs1(int u,int fa){ int i,v; sz[u]=1; dep[u]=dep[fa]+1; son[u]=0; father[u]=fa; for(i=head[u];i!=-1;i=bian[i].next) { v=bian[i].v; if(v==fa) continue; dfs1(v,u); sz[u]+=sz[v]; if(sz[son[u]]<sz[v]) son[u]=v; }}void dfs2(int u,int fa){ int i,v; ti[u]=id++; top[u]=fa; if(son[u]!=0) dfs2(son[u],fa); for(i=head[u];i!=-1;i=bian[i].next) { v=bian[i].v; if(v==father[u]||v==son[u]) continue; dfs2(v,v); }}void cal(int u,int v,int z){ while(top[u]!=top[v]) { if(dep[top[u]]>dep[top[v]]) swap(u,v); add[ti[top[v]]].push_back(z); del[ti[v]].push_back(z); v=father[top[v]]; } if(ti[u]>ti[v]) swap(u,v); add[ti[u]].push_back(z); del[ti[v]].push_back(z);}void pushdown(int t){ int temp=t<<1; if(a[temp].num>=a[temp+1].num) { a[t].col=a[temp].col; a[t].num=a[temp].num; } else { a[t].col=a[temp+1].col; a[t].num=a[temp+1].num; }}void update(int k,int t,int p){ if(a[t].x==a[t].y) { a[t].num+=p; return ; } int mid=(a[t].x+a[t].y)>>1,temp=t<<1; if(k<=mid) update(k,temp,p); else update(k,temp+1,p); pushdown(t);}void build(int x,int y,int t){ a[t].x=x; a[t].y=y; if(x==y) { a[t].col=x; a[t].num=0; return ; } int mid=(x+y)>>1,temp=t<<1; build(x,mid,temp); build(mid+1,y,temp+1); pushdown(t);}int main(){ int n,m,i,u,v,z,cnt,j,sx; //freopen("a.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(head,-1,sizeof(head)); e=0; for(i=1;i<n;i++) { scanf("%d%d",&u,&v); adge(u,v); adge(v,u); } dep[1]=0; id=1; sz[0]=0; dfs1(1,1); dfs2(1,1); /*for(i=1;i<=n;i++) { printf("i=%d ",i); printf("dep=%d ",dep[i]); printf("sz=%d ",sz[i]); printf("son=%d ",son[i]); printf("father=%d ",father[i]); printf("ti=%d ",ti[i]); printf("top=%d ",top[i]); printf("\n"); }*/ for(i=1;i<=n;i++) { dui[ti[i]]=i; } cnt=0; for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&z); cal(u,v,z); cnt=max(cnt,z); } build(0,cnt,1); for(i=1;i<=n;i++) { sx=add[i].size(); for(j=0;j<sx;j++) { update(add[i][j],1,1); } ans[dui[i]]=a[1].col; sx=del[i].size(); for(j=0;j<sx;j++) { update(del[i][j],1,-1); } add[i].clear(); del[i].clear(); } for(i=1;i<=n;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- hdu 5029树链剖分+线段树
- hdu 5029 Relief grain (树链剖分+线段树)
- hdu 5029 Relief grain(树链剖分+线段树)
- HDU 5029Relief grain(树链剖分+线段树)
- hdu 3966 (树链剖分+线段树)
- hdu 3804(树链剖分+线段树)
- hdu 3804 树链剖分+线段树
- hdu 3966 树链剖分+线段树
- 2014网络赛 hdu 5029 树链剖分 + 线段树
- HDU 5029 Relief grain 树链剖分+线段树离线维护
- HDU 5029 Relief grain 树链剖分 离线 线段树
- HDU 5029 Relief grain (树链剖分 + 线段树)
- [HDU 5029] Relief grain (树链剖分+线段树)
- HDU 5029 Relief grain(恶心的树链剖分 + 线段树)
- HDU 5029Relief grain-树链剖分+线段树+离线
- HDU-3966 (树链剖分+线段树)
- hdu-3966(树链剖分+线段树)
- HDU-5242 Game (贪心&&树链剖分&&线段树)
- 游戏开发标准流程
- vim常用命令
- C++_类、对象
- 解决安卓SDK更新dl-ssl.google.com无法连接
- hdu2073 模拟规律
- hdu 5029树链剖分+线段树
- Aix 下打开超过2G的文件
- java数组的高效排序
- Android得到状态栏的高度
- android.grapthics.Bitmap类的详解
- Ubuntu 12.04中文输入法的安装
- 线程的同步和异步
- hdu2035(快速幂取余)人见人爱A^B
- HDU 1058 Humble Numbers