CSU 1811 Tree Intersection
来源:互联网 发布:mac屏保设置不生效 编辑:程序博客网 时间:2024/05/22 16:47
题意:
一棵树,每个节点有一种颜色,现在删去一条边,求这个树分成两部分,这两部分的共有颜色个数。
分析:
枚举每一棵子树所有存在的颜色的节点个数,看看有没有超过这种颜色再整个树中的个数,就是答案。
就是一个子树的合并操作,合并并不需要每一个子树都建立一个节点,只需要找出不同颜色最多的那个子树,在这个子树上进行合并就可以了。这个样子每一次最多暴力了
代码:
#include <bits/stdc++.h>using namespace std;typedef pair <int,int> PII;typedef vector <int> VI;#define pb push_back#define mp make_pair#define fi first#define se secondconst int maxn = 100010;int n,c[maxn],rt[maxn],dp[maxn],ans[maxn],cnt[maxn];map <int,int> hs[maxn],edge[maxn];VI g[maxn];void dfs(int u,int fa){ int id = u,sz = -1; for(int i = 0;i < (int)g[u].size();++ i){ int v = g[u][i]; if(v == fa) continue; dfs(v,u); if((int)hs[rt[v]].size() > sz) id = v,sz = (int)hs[rt[v]].size(); } rt[u] = rt[id]; dp[u] = dp[id]; id = rt[u]; if(hs[id][c[u]] == 0) ++ dp[u]; hs[id][c[u]] ++; if(hs[id][c[u]] == cnt[c[u]]) -- dp[u]; for(int i = 0;i < (int)g[u].size();++ i){ int v = g[u][i]; if(v == fa || rt[v] == id) continue; map <int,int> :: iterator it; for(it = hs[rt[v]].begin();it != hs[rt[v]].end();++ it){ if(hs[id][it->fi] == 0) ++ dp[u]; hs[id][it->fi] += it->se; if(hs[id][it->fi] == cnt[it->fi]) -- dp[u]; } } //cout << u << " " << rt[u] << " " << dp[u] << endl; if(fa != -1) ans[edge[u][fa]] = dp[u];}int main(){ //freopen("test.in","r",stdin); while(~scanf("%d",&n)){ memset(cnt,0,sizeof(cnt)); memset(dp,0,sizeof(dp)); for(int i = 1;i <= n;++ i) scanf("%d",c+i),++ cnt[c[i]]; for(int i = 1;i <= n;++ i) rt[i] = i; for(int i = 1;i <= n;++ i) hs[i].clear(),edge[i].clear(),g[i].clear(); for(int i = 1;i < n;++ i){ int u,v; scanf("%d%d",&u,&v); g[u].pb(v); g[v].pb(u); edge[u][v] = i; edge[v][u] = i; } dfs(1,-1); for(int i = 1;i < n;++ i) printf("%d\n",ans[i]); } return 0;}
0 0
- CSU 1811 Tree Intersection
- CSU 1811 Tree Intersection
- CSU 1811 Tree Intersection(启发式合并)
- CSU 1811 Tree Intersection(启发式合并)
- CSU 1811 Tree Intersection(莫队算法)
- CSU 1811 Tree Intersection(dfs序+莫队算法)
- csu 1811 Tree Intersection 2016湖南省赛 I
- CSU 1811 Tree Intersection 平衡树启发式合并
- CSU 1811 Tree Intersection(线段树+启发式合并 解法)
- CSU 1811 Tree Intersection(Treap启发式合并)
- CSU 1811 Tree Intersection 处理树问题的一个重要思路!!!
- 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
- [CSU - 1811 (湖南省赛16)] Tree Intersection (启发式合并)
- 1811: Tree Intersection
- [CSU - 1811 (湖南省赛16)] Tree Intersection (dfs序维护子树+离线询问+树状数组)
- CSU 1663: Tree(树链剖分)
- csu 1837: Tree Count
- [dsu] CSU1811: Tree Intersection
- GreenDao查询,有时会查不到数据,解决方法。
- spring Bean的实例化
- Android中当一行显示两个TextView时,第一个可“…”,而第二个必须显示全部
- leetCode_Substring with Concatenation of All Words
- extjs中如何监听textfield的改变
- CSU 1811 Tree Intersection
- 求素数算法(C语言)
- 面试题34:丑数
- undefined reference to `clock_gettime' 链接错误问题解决
- Dubbo架构设计详解
- 无法访问项目中jsp解决方法
- HTML前端开发上乘"武学"-nodeJS
- Serializable 接口
- QNX system architecture 11 - Character I/O