首页 > 代码库 > HDU 5029 Relief grain
HDU 5029 Relief grain
Relief grain
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 867 Accepted Submission(s): 221
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 4
1 2
1 1 1
1 2 2
2 2 2
2 2 1
5 3
1 2
3 1
3 4
5 3
2 3 3
1 5 2
3 3 3
0 0
Sample Output
1
2
2
3
3
0
2
Hint
For 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
这条题目是我做过的一条很特别 。。
给了一棵树 , 有 10W个点 ,10W次查询 。 每次查询在一条边上涂上一种颜色 , 然后又10W种颜色。
然后就是操作最后问你某个点涂过的最多次数的颜色是什么。
假若说这是在一维线段上面操作的话 , 我们可以开一个邻接矩阵, 然后在某个点i 标记 +X , 表示 在这个点上面涂上X颜色, -X表示抹去 X 颜色。
那么,一个操作 l r z 就可以在 l 处 标记一个 +X , 在 r + 1 处标记一个 -X 。
然后,再要维护一颗颜色线段树,更新的时候顺带维护任意区间涂色次数最多的颜色是什么。
最后,从左到右扫一次操作区间,把其在邻接矩阵上面的操作全部完成以后 (操作的时候标志为 +X 的话在就线段树 X 位置 +1 ,-X 在 X位置 -1 ),
在颜色线段树上查询哪个颜色出现最多就可以了。
题目是在数上面操作的, 然后要写一个树链剖分。
注意 , 建线段树的时候 , 每次都要建区间长度为 1~10W
写树链剖分要增栈。
操作的邻接矩阵空间要足够大~
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <vector>using namespace std;typedef long long LL;const int N = 100011;const int M = 2000011;#define lr rt<<1#define rr rt<<1|1#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define root 1,100000,1int tot , eh[N], et[N<<1] , nxt[N<<1] ;int fa[N] , dep[N] , p[N] , siz[N] , son[N] , top[N] , rnk[N] , pos ;int n , q ;void addedge(int u , int v ){ et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++; et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++;}//------------------------int eh2[N],nxt2[M],et2[M],tot2;void add(int u,int v){ et2[tot2] = v , nxt2[tot2] = eh2[u] , eh2[u] = tot2 ++;}void init(){ pos = 1 ; tot = tot2 = 0 ; memset( eh , -1 , sizeof eh ); memset( eh2 , -1 , sizeof eh2 ); memset( son , -1 ,sizeof son );}void dfs1( int u , int father , int deep){ fa[u] = father ; dep[u] = deep ; siz[u] = 1 ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i] ; if( v == fa[u] ) continue ; dfs1( v , u , deep + 1 ); siz[u] += siz[v] ; if( son[u] == -1 || siz[v] > siz[son[u]] ) son[u] = v; }}void dfs2( int u , int tp ){ top[u] = tp ; p[u] = pos++ ; rnk[ p[u] ] = u ; if( son[u] == -1 ) return ; dfs2( son[u] , tp ); for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i] ; if( v == fa[u] || v == son[u] ) continue ; dfs2(v,v); }}// --------------------void change( int u , int v , int val){ int f1 = top[u] , f2 = top[v] ; while( f1 != f2 ){ if( dep[f1] < dep[f2] ){ swap(f1,f2); swap(u,v); } add( p[f1] , val ); add( p[u] + 1 , -val ); u = fa[f1]; f1 = top[u]; } if( dep[u] > dep[v] ) swap(u,v); add( p[u] , val ); add( p[v] + 1 , - val ) ;}//--------------------------------------int mz[N<<2],col[N<<2];void Up(int rt){ if( mz[lr] >= mz[rr]){ mz[rt] = mz[lr]; col[rt] = col[lr]; } else{ mz[rt] = mz[rr]; col[rt] = col[rr]; }}void build(int l,int r,int rt){ if(l == r){ col[rt] = l; mz[rt] = 0; return; } int mid = (l + r)/2; build(lson); build(rson); Up(rt);}void update( int l, int r , int rt,int k,int val){ if( l == r ){ mz[rt] += val; return; } int mid = ( l + r ) / 2; if(k <= mid) update(lson,k,val); else update(rson ,k,val); Up(rt);}int ans[N];void run(){ int u , v , z ; init(); for( int i = 1 ; i < n ; ++i ){ scanf("%d%d",&u,&v); addedge(u,v); } build(root); dfs1(1,0,0); dfs2(1,1); for( int i = 0 ; i < q ; ++i ){ scanf("%d%d%d",&u,&v,&z); change(u,v,z); } for( int i = 1 ; i <= n ; ++i ){ for( int j = eh2[i] ; ~j ; j = nxt2[j] ){ v = et2[j]; if( v > 0 ) update( root, v ,1); else update( root, -v , -1 ); } ans[ rnk[i] ] = ( mz[1] == 0 ? 0 : col[1] ); } for( int i = 1; i <= n ; ++i ){ printf("%d\n",ans[i]); }}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(0); while( ~scanf("%d%d",&n,&q ) ) { if( !n && !q )break; run(); }}
HDU 5029 Relief grain
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。