CSU 1811 Tree Intersection 处理树问题的一个重要思路!!!

http://acm.hust.edu.cn/vjudge/contest/131505#problem/I

给下链接 题意自取

错误思路不谈,避免强化

对题意简单理解后转化为

①求出每棵子树中独有的颜色 和 没出现过的颜色的数量


1. 比赛一打玩问 lcy 韬神 他们怎么做的,n个map 存该点下子树中需要存的颜色

据说复杂度可以证明为nlogn,不太懂 暂且不管

update: 证明就是启发式合并的证明//还没弄懂

map::swap的复杂度是常数的


2. 百度了一个做法超级神奇

先用dfs序将树转化为线段,然后莫队算法求

关键点一:将树转化为线段是想过,但懵逼选手只想到了树剖和LCA那个转化方式,显然两个都不适用就放弃了

然而dfs序转化树的题目做过两道了啊,居然没有想起来,给自己跪了

关键点二:将①转化为求

②每棵字数中独有的颜色 和 出现过的颜色的数量

关键点三:在区间上这个问题又如何做呢,因为拆成区间了,第一反应是线段树搞一下,然后就不知道如何合并了又GG

然后莫队出现了!


总结一些思路

将树拆为区间->三种,分别树剖,LCA, dfs序, 分别解决三种问题

区间查询问题->线段树/st/BIT 以及 莫队

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cctype>
#include <cmath>
#include <vector>
#include <sstream>
#include <bitset>
#include <deque>
#include <iomanip>
using namespace std;
#define pr(x) cout << "x = " << x << endl;
#define bug cout << "bugbug" << endl;
#define ppr(x, y) printf("(%d, %d)\n", x, y);
#define pfun(a, b) printf("x = %d   f(x) = %d\n", a, b);

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;
const int MOD = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e5 + 4;
const int maxm = 1e3 + 4;
int n, col[maxn], c[maxn], u, v, S[maxn], T[maxn], Index, belong[maxn], cnt[maxn], in[maxn];
bool vis[maxn];
int a[maxn], b[maxn], fa[maxn];
vector<int> G[maxn];
void dfs(int u){
	c[Index] = col[u];
	S[u] = Index++;
	vis[u] = true;
	for (int i = 0; i < G[u].size(); ++i){
		int v = G[u][i];
		if (vis[v]) continue;
		fa[v] = u;
		dfs(v);
	}
	T[u] = Index - 1;
}
struct Query{
	int l, r, id;
	bool operator < (const Query &rhs) const{
		if (belong[l] == belong[rhs.l]) return r < rhs.r;
		return belong[l] < belong[rhs.l];
	}
}q[maxn];
int ans[maxn], tot, full;
inline void add(int pos){
	if (in[c[pos]] == 0) tot++;
	if (++in[c[pos]] == cnt[c[pos]]) full++;
	return;
}
inline void sub(int pos){
	if (in[c[pos]] == cnt[c[pos]]) full--;
	if (--in[c[pos]] == 0) tot--;
	return;
}
int main(){
//必须编译过才能交 
//	ios::sync_with_stdio(false);
	int ik, i, j, k, kase;
	while(~scanf("%d", &n)){
		memset(in, 0, sizeof in);
		memset(cnt, 0, sizeof cnt);
		for (i = 1; i <= n; ++i){
			scanf("%d", col + i);
			cnt[col[i]]++;
		}
		for (i = 1; i <= n; ++i) G[i].clear();
		for (i = 1; i < n; ++i){
			scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
			a[i] = u;
			b[i] = v;
		}
		fa[1] = -1;
		memset(vis, false, sizeof vis);
		Index = 1;
		dfs(1);
		int bk = sqrt(n + 1);
		for (i = 1; i <= n; ++i){
			belong[i] = i / bk;
			q[i].l = S[i];
			q[i].r = T[i];
			q[i].id = i;
		}
		sort(q+1, q+1+n);
		int lft = 1, rght = 0; tot = 0, full = 0;
		for (i = 1; i <= n; ++i){
			Query& qu = q[i];
			while(rght < qu.r) add(++rght);
			while(lft > qu.l) add(--lft);
			while(rght > qu.r) sub(rght--);
			while(lft < qu.l) sub(lft++);
			ans[qu.id] = tot - full;
//			cout << qu.id << ' ' << tot << ' ' << full << endl; 
		}
		for (i = 1; i <= n - 1; ++i){
//			printf("%d: ", fa[a[i]] == b[i] ? a[i] : b[i]);
			printf("%d\n", fa[a[i]] == b[i] ? ans[a[i]] : ans[b[i]]);
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值