csu oj 1811: Tree Intersection (启发式合并)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811
给你一棵树,每个节点有一个颜色。问删除一条边形成两棵子树,两棵子树有多少种颜色是有相同的。
启发式合并,小的合并到大的中。类似的题目有http://codeforces.com/contest/600/problem/E
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
struct Edge {
int next, to, index;
}edge[N << ];
int color[N], head[N], tot;
int sum[N], ans[N], res[N]; //sum[color]:颜色color节点个数, ans[u]表示u点及字节点的答案, res[edge]表示边的答案
map <int, int> cnt[N]; //cnt[u][color] 表示u点子树color颜色有多少个节点 void init(int n) {
for(int i = ; i <= n; ++i) {
head[i] = -;
sum[i] = ;
cnt[i].clear();
}
tot = ;
} inline void add_edge(int u, int v, int id) {
edge[tot].next = head[u];
edge[tot].to = v;
edge[tot].index = id;
head[u] = tot++;
} void dfs(int u, int pre, int id) {
cnt[u][color[u]] = ;
ans[u] = cnt[u][color[u]] < sum[color[u]] ? : ;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == pre)
continue;
dfs(v, u, edge[i].index);
if(cnt[u].size() < cnt[v].size()) {
swap(cnt[u], cnt[v]);
swap(ans[u], ans[v]);
}
for(auto it : cnt[v]) {
int &num = cnt[u][it.first];
if(num == && num + it.second < sum[it.first]) {
++ans[u];
} else if(num + it.second == sum[it.first] && num) { //说明此子树的it.first颜色节点个数已满
--ans[u];
}
num += it.second;
}
}
res[id] = ans[u];
} int main()
{
int n, u, v;
while(scanf("%d", &n) != EOF) {
init(n);
for(int i = ; i <= n; ++i) {
scanf("%d", color + i);
++sum[color[i]];
}
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
add_edge(u, v, i);
add_edge(v, u, i);
}
dfs(, -, );
for(int i = ; i < n; ++i) {
printf("%d\n", res[i]);
}
}
return ;
}
csu oj 1811: Tree Intersection (启发式合并)的更多相关文章
- CSU 1811: Tree Intersection(线段树启发式合并||map启发式合并)
		
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1811 题意:给出一棵树,每一个结点有一个颜色,然后依次删除树边,问每次删除树边之后,分开的两个 ...
 - dsu on tree 树上启发式合并 学习笔记
		
近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...
 - dsu on tree[树上启发式合并学习笔记]
		
dsu on tree 本质上是一个 启发式合并 复杂度 \(O(n\log n)\) 不支持修改 只能支持子树统计 不能支持链上统计- 先跑一遍树剖的dfs1 搞出来轻重儿子- 求每个节点的子树上有 ...
 - dsu on tree (树上启发式合并) 详解
		
一直都没出过算法详解,昨天心血来潮想写一篇,于是 dsu on tree 它来了 1.前置技能 1.链式前向星(vector 建图) 2.dfs 建树 3.剖分轻重链,轻重儿子 重儿子 一个结点的所有 ...
 - AGC 014E.Blue and Red Tree(思路 启发式合并)
		
题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...
 - 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
		
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...
 - CSU 1811 Tree Intersection
		
莫队算法,$dfs$序. 题目要求计算将每一条边删除之后分成的两棵树的颜色的交集中元素个数. 例如删除$u->v$,我们只需知道以$v$为$root$的子树中有多少种不同的颜色(记为$qq$), ...
 - dsu on tree(树上启发式合并)
		
简介 对于一颗静态树,O(nlogn)时间内处理子树的统计问题.是一种优雅的暴力. 算法思想 很显然,朴素做法下,对于每颗子树对其进行统计的时间复杂度是平方级别的.考虑对树进行一个重链剖分.虽然都基于 ...
 - 启发式合并CSU - 1811
		
F - Tree Intersection CSU - 1811 Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges ...
 
随机推荐
- Calculate drive total/free/available space
			
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Consol ...
 - /bin/bash: [xxxx]: command not found
			
/******************************************************************************** * /bin/bash: [xxxx ...
 - Windows Tftpd32 DHCP服务器 使用
			
/********************************************************************* * Windows Tftpd32 DHCP服务器 使用 ...
 - IntelliJ IDEA使用总结篇
			
解决控制台中文乱码的问题: 1.windows下改intellij安装目录下bin\idea.exe.vmoptions文件 加上 -Dfile.encoding=UT 2.设置IDEA server ...
 - web前端调试工具
			
1.firebug入门指南 http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 2. Console命令详解,让调试js代码变得更 ...
 - eclipse 报错 import ... cannot be resolved 处理方法
			
项目上右键,properties, 找java build path,切到libraies标签,将爆红的jdk编辑一下,选用你需要的jdk版本,一般1..我看你类的httpServlet报错,也是这个 ...
 - 下载个jquery-easyui-1.3.0使用,把他导入到myeclipse10里,jquery-1.7.2.min.js报错。 错误如下, Syntax error on token "Invalid Regular Expression Options", no accurate correc
			
1.选中报错的jquery文件“jquery-1.2.6.min.js”.2.右键选择 MyEclipse-->Exclude From Validation .3.再右键选择 MyEclips ...
 - Word排版成树形结构技巧
			
初始文字 A A1 A2 B1 B1 B2 C C1 希望效果 关健设置
 - web app 页面旋转整体样式问题
			
$(window).bind("orientationchange", function (event) { if (event.orientation) { //portrait ...
 - Android Ant批量打包
			
一.配置Ant环境变量 JAVA_HOME=/software/jdk1.6.0_24 ANT_HOME=/software/apache-ant-1.9.2 Android_Home=/softwa ...