标签:des style color io os ar java for strong
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
1 2 2 3 3 0 2HintFor 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}.
路径染色,然后求每一个节点染色次数最大的,如果存在多种颜色次数相同,输出编号最小的。
轻重链剖分,对于每一条路径,按照重链划分,保存起来,然后枚举每一条重链,顺着重儿子向下统计,
/* *********************************************** Author :rabbit Created Time :2014/10/5 10:18:36 File Name :7.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <string> #include <time.h> #include <math.h> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; const int maxn=100100; int head[maxn],tol,n,m,dep[maxn],son[maxn],top[maxn],que[maxn],fa[maxn][19]; int sum[maxn],cnt[maxn],ans[maxn]; struct Edge{ int next,to; Edge(int _next=0,int _to=0){ next=_next;to=_to; } }edge[200010]; void addedge(int u,int v){ edge[tol]=Edge(head[u],v); head[u]=tol++; } void divide(){ memset(sum,0,sizeof(sum)); memset(son,0,sizeof(son)); memset(top,0,sizeof(top)); int front=1,rear=1; que[rear++]=1;dep[1]=0;fa[1][0]=0; while(front!=rear){ int now=que[front++]; for(int i=1;i<19;i++)fa[now][i]=fa[fa[now][i-1]][i-1]; for(int i=head[now];i!=-1;i=edge[i].next){ int v=edge[i].to; if(v==fa[now][0])continue; dep[v]=dep[now]+1; fa[v][0]=now; que[rear++]=v; } } for(int i=n;i>=1;i--)sum[fa[que[i]][0]]+=++sum[que[i]]; for(int i=1;i<=n;i++){ int u=que[i],best=0; if(top[u]==0)top[u]=u; for(int j=head[u];j!=-1;j=edge[j].next){ int v=edge[j].to; if(v==fa[u][0])continue; if(sum[v]>best)son[u]=v,best=sum[v]; } top[son[u]]=top[u]; } } int find(int x,int d){ for(int i=18;i>=0;i--)if(d&(1<<i))x=fa[x][i]; return x; } int LCA(int x,int y){ while(1){ if(dep[x]>dep[y])swap(x,y); if(top[x]==top[y])return x; if(dep[top[x]]<dep[top[y]])y=fa[top[y]][0]; else x=fa[top[x]][0]; } } int lca(int x,int y){ if(dep[x]<dep[y])swap(x,y); for(int i=18;i>=0;i--)if((dep[x]-dep[y])&(1<<i))x=fa[x][i]; if(x==y)return x; for(int i=18;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i]; return fa[x][0]; } vector<pair<int,int> > c[maxn]; void insert(int x,int y,int z){ while(top[x]!=top[y]){ c[top[y]].push_back(make_pair(z,0)); c[y].push_back(make_pair(z,1)); y=fa[top[y]][0]; } c[x].push_back(make_pair(z,0)); c[y].push_back(make_pair(z,1)); } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); while(~scanf("%d%d",&n,&m)){ if(n==0&&m==0)break; memset(head,-1,sizeof(head));tol=0; for(int i=1;i<n;i++){ int x,y; scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } for(int i=0;i<maxn;i++)c[i].clear(); divide(); for(int i=1;i<=m;i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); if(dep[x]>dep[y])swap(x,y); int A=lca(x,y); if(A!=x)insert(find(x,dep[x]-dep[A]-1),x,z); insert(A,y,z); } // cout<<"ddd "<<endl; priority_queue<pair<int,int> > Q; memset(cnt,0, sizeof(cnt)); memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++) if(top[i]==i){ while(!Q.empty())Q.pop(); for(int u=i;u;u=son[u]){ vector<pair<int,int> >::iterator it; for(it=c[u].begin();it!=c[u].end();it++) if(it->second==0)Q.push(make_pair(++cnt[it->first],-it->first)); while(!Q.empty()&&Q.top().first!=cnt[-Q.top().second])Q.pop(); if(!Q.empty())ans[u]=-Q.top().second; for(it=c[u].begin();it!=c[u].end();it++) if(it->second==1)if(--cnt[it->first])Q.push(make_pair(cnt[it->first],-it->first)); } } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); } return 0; }
非递归的树链剖分简直炫酷。
代码;
标签:des style color io os ar java for strong
原文地址:http://blog.csdn.net/xianxingwuguan1/article/details/39802467