[CF600E]Lomsat gelral
来源:互联网 发布:vb页面跳转 编辑:程序博客网 时间:2024/06/06 04:41
题目大意
统计每个子树里出现次数最多的颜色编号和。
dsu on tree
裸题吧。
#include<cstdio>#include<algorithm>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;typedef long long ll;const int maxn=100000+10;int h[maxn],go[maxn*2],next[maxn*2],c[maxn],cnt[maxn],size[maxn],mx[maxn];ll ans[maxn];bool bz[maxn];int i,j,k,l,t,n,m,tot,num;ll now;void add(int x,int y){ go[++tot]=y; next[tot]=h[x]; h[x]=tot;}void travel(int x,int y){ int t=h[x]; size[x]=1; while (t){ if (go[t]!=y){ travel(go[t],x); size[x]+=size[go[t]]; } t=next[t]; } mx[x]=0; t=h[x]; while (t){ if (go[t]!=y&&(!mx[x]||size[go[t]]>size[mx[x]])) mx[x]=go[t]; t=next[t]; }}void dg(int x,int y,int f){ cnt[c[x]]+=f; if (cnt[c[x]]>num) num=cnt[c[x]],now=c[x]; else if (cnt[c[x]]==num) now+=(ll)c[x]; int t=h[x]; while (t){ if (go[t]!=y&&!bz[go[t]]) dg(go[t],x,f); t=next[t]; }}void dfs(int x,int y,bool keep){ int t=h[x]; while (t){ if (go[t]!=y&&go[t]!=mx[x]) dfs(go[t],x,0); t=next[t]; } if (mx[x]) dfs(mx[x],x,1),bz[mx[x]]=1; dg(x,y,1); ans[x]=now; if (mx[x]) bz[mx[x]]=0; if (!keep){ dg(x,y,-1); now=num=0; }}int main(){ scanf("%d",&n); fo(i,1,n) scanf("%d",&c[i]); fo(i,1,n-1){ scanf("%d%d",&j,&k); add(j,k);add(k,j); } travel(1,0); dfs(1,0,0); fo(i,1,n) printf("%I64d ",ans[i]);}
阅读全文
0 0
- [CF600E]Lomsat gelral
- [CF600E]Lomsat gelral
- Codeforces 600E Lomsat gelral
- codeforces 660E Lomsat gelral
- Codeforces 600E Lomsat gelral
- CF 600E(Lomsat gelral-dfs)
- CodeForces 600E Lomsat gelral 暴力
- CF 600E Lomsat gelral 学习内容
- Codeforces 600E Lomsat gelral(启发式合并)
- codeforces600E Lomsat gelral -- 树上启发式合并
- codeforces600E. Lomsat gelral(dsu on tree)
- 【dsu on tree】Codeforces600E[Lomsat gelral]题解
- cf600E
- codeforces 600E. Lomsat gelral(教育场 树形dp)
- Educational Codeforces Round 2 E.Lomsat gelral(树形dp)
- CodeForces 600E Lomsat gelral(树形dp+启发式合并)
- Educational Codeforces Round 2 E Lomsat gelral(启发式合并)
- Educational Codeforces Round 2 E. Lomsat gelral(启发式合并)
- js中onblur和onfocus事件(用于文本框)
- 余弦计算相似度度量【转】 此博文包含图片(2014-11-17 10:18:40)转载▼ 标签: 数学 基础 自然语言处理分类: 学习 http://blog.csdn.net/u0121606
- Spring 03
- Django 1.8.2 文档
- Spring 各个jar包的作用
- [CF600E]Lomsat gelral
- Ubuntu下用apt-get安装最新版本的Redis
- CreateProcess
- 通过MyEclipse 9.0对Liferay6.1.2进行编译与部署
- 安卓仿手机网易新闻app项目开发系列之(二)轮播图显示和RecyclerView适配器编写
- SQL简明实例教程
- Vlan使用--------2
- 学习笔记---高等数学前置知识---数列、排列组合、解不等式
- 接口interface