HDU Hilarity (dfs序+线段树)

Problem Description

After June 1st, elementary students of Ted Land are still celebrating "The Sacred Day of Elementary Students”. They go to the streets and do some elementary students stuff. So we call them "elementary students". There are N cities in Ted Land. But for simplicity,
the mayor Matt only built N - 1 roads so that all cities can reach each other. Some of the roads are occupied by the "elementary students". They will put an celebration hat on everyone who goes through the road without one. But if someone goes through the
road with a celebration hat on his head, "elementary students" will steal the hat for no reason. Since Matt doesn’t have a celebration hat, he wants to know how many different paths in his land that he can end up with a hat. Two paths are considered to be
different if and only if they have different start city or end city. As the counsellor of the mayor Matt, you have to answer this question for him. The celebration elementary students are not stable: sometimes a new crowd of elementary students go to an empty
road; sometimes the elementary students on a road will go back home and remain the road empty. Matt will send you the monitor about the change of elementary students on road and ask you the question above. You will be fired if you answer wrong.

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

For each test case, the first line contains N (1<=N<=30000) describing the number of cities.

Then N lines follow. The ith line of these contains the name of the ith city, it’s a string containing only letters and will not be longer than 10.

The following N - 1 lines indicate the N - 1 edges between cities. Each of these lines will contain two strings for the cities’ name and a integer for the initial status of the edge (0 for empty, 1 for crowded).

Then an integer M (1<=M<=60000) describes the number of queries. There are two kinds of query:

● "M i" means the status changing of the ith (starting from 1) road (0 to 1, 1 to 0);

● "Q" means that Matt asks you the number of different paths.

Output

For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).

Then for each “Q” in input, output a line with the answer.

Sample Input

1
5
a
b
c
d
e
a b 1
b c 0
c d 1
d e 1
7
Q
M 1
Q
M 3
Q
M 4
Q

Sample Output

Case #1:
12
8
8
0

Source

2014 ACM/ICPC Asia Regional Beijing Online

题意:给定一棵边权为 0/1 的树,要求支持两类操作:1.
询问有多少条路径 xor 和为 1。2. 修改一条边的权值。

思路:把树形转换成线性的,又涨知识啦,参考官方提供的题解:因为只在乎奇偶性,可以看做求有多少条路径的xor值为1。
随便找个根dfs出根到每个节点路径的xor值d[x],那么很明显路径的xor值就是两个端点的d[]的xor值。 这样这个问题就比较喜闻乐见了,而修改仅仅是翻转在dfs序上的一个区间,线段树就可以做了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
typedef long long ll;
using namespace std;
const int maxn = 30005;

map<string, int> mp;
struct Edge {
	int u, v, c;
	Edge() {}
	Edge(int u, int v, int c) {
		this->u = u;
		this->v = v;
		this->c = c;
	}
};
vector<Edge> edges;
vector<int> g[maxn];
int n, m, q, cnt;
int le[maxn], ri[maxn];
string sa, sb;
int cover[maxn<<2], val[maxn<<2], dep[maxn], ans[maxn<<2];

int id(string s) {
	if (mp.count(s) == 0)
		mp[s] = ++cnt;
	return mp[s];
}

void add(int u, int v, int c) {
	edges.push_back(Edge(u, v, c));
	edges.push_back(Edge(v, u, c));
	m = edges.size();
	g[u].push_back(m-2);
	g[v].push_back(m-1);
}

void dfs(int u, int fa, int va) {
	dep[u] = dep[fa] + 1;
	le[u] = ++cnt;
	val[cnt] = va;
	for (int i = 0; i < g[u].size(); i++) {
		Edge e = edges[g[u][i]];
		int v = e.v;
		int c = e.c;
		if (v == fa) continue;
		dfs(v, u, c^va);
	}
	ri[u] = cnt;
}

void pushup(int pos) {
	ans[pos] = ans[lson(pos)] + ans[rson(pos)];
}

void pushdown(int l, int r, int pos) {
	int m = l + r >> 1;
	if (cover[pos]) {
		cover[lson(pos)] ^= 1;
		cover[rson(pos)] ^= 1;
		ans[lson(pos)] = m - l + 1 - ans[lson(pos)];
		ans[rson(pos)] = r - m - ans[rson(pos)];
		cover[pos] = 0;
	}
}

void build(int l, int r, int pos) {
	if (l == r) {
		cover[pos] = 0;
		ans[pos] = val[l];
		return;
	}
	cover[pos] = 0;
	int m = l + r >> 1;
	build(l, m, lson(pos));
	build(m+1, r, rson(pos));
	pushup(pos);
}

void update(int l, int r, int pos, int L, int R) {
	if (L <= l && R >= r) {
		cover[pos] ^= 1;
		ans[pos] = r - l + 1 - ans[pos];
		return;
	}

	int m = l + r >> 1;
	pushdown(l, r, pos);
	if (L <= m)
		update(l, m, lson(pos), L, R);
	if (R > m)
		update(m+1, r, rson(pos), L, R);
	pushup(pos);
}

int main() {
	int t, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		mp.clear();
		memset(dep, 0, sizeof(dep));
		cnt = 0;
		edges.clear();
		for (int i = 1; i <= n; i++) {
			g[i].clear();
			cin >> sa;
			id(sa);
		}

		for (int i = 0; i < n-1; i++) {
			int u, v, c;
			cin >> sa >> sb;
			scanf("%d", &c);
			u = id(sa);
			v = id(sb);
			add(u, v, c);
		}

		scanf("%d", &q);
		printf("Case #%d:\n", cas++);
		cnt = 0;
		dfs(1, 0, 0);

		build(1, n, 1);
		ll res = 0;
		while (q--) {
			cin >> sa;
			if (sa[0] == 'Q') {
				res = (ll) (n - ans[1]) * ans[1] * 2;
				printf("%lld\n", res);
			}
			else {
				int x;
				scanf("%d", &x);
				x--;
				x <<= 1;
				int u = edges[x].u;
				int v = edges[x].v;
				if (dep[u] < dep[v])
					swap(u, v);
				update(1, n, 1, le[u], ri[u]);
			}
		}
	}
	return 0;
}
时间: 2024-10-11 11:28:41

HDU Hilarity (dfs序+线段树)的相关文章

POJ 3321 DFS序+线段树

单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include <cstdio> 6: #include <cstring> 7: #include <cctype> 8: #include <algorithm> 9: #include <vector> 10: #include <iostream> 1

codevs1228 (dfs序+线段树)

总结: 第一次遇到dfs序的问题,对于一颗树,记录节点 i 开始搜索的序号 Left[i] 和结束搜索的序号 Righti[i],那么序号在 Left[i] ~ Right[i] 之间的都是节点 i 子树上的节点. 并且此序号与线段树中 L~R 区间对应,在纸上模拟了几遍确实如此,但暂时还未理解为何对应. 此题就是dfs序+线段树的裸题 代码: #include<iostream> #include<vector> #include<cstring> #include&

[BZOJ 3306]树(dfs序+线段树+倍增)

Description 给定一棵大小为 n 的有根点权树,支持以下操作: • 换根 • 修改点权 • 查询子树最小值 Solution 单点修改子树查询的话可以想到用dfs序+线段树来处理,换根的处理画一画图应该可以明白: 如果查询的x是当前的根rt,直接返回整棵树的min 如果rt在x的子树中,用倍增的方法找到离x最近的rt的祖先t,整棵树除t的子树以外的部分就是x当前根下的子树 如果rt不在x的子树中,查询x原来的子树的min值 #include<iostream> #include<

Educational Codeforces Round 6 E dfs序+线段树

题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili上电子科技大学发的视频学习的 将一颗树通过dfs编号的方式 使每个点的子树的编号连在一起作为相连的区间 就可以配合线段树搞子树 因为以前好像听说过 线段树可以解决一种区间修改和查询区间中不同的xx个数...所以一下子就想到了... 但是我不会写线段树..只会最简单的单点修改区间查询...不会用延迟标

【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某些场景可以通过不同的选择支到达其他场景.所有场景和选择支构成树状结构:开始游戏时在根节点(共通线)

【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\),问你答案是多少. \(n\leq {10}^5,k\leq {10}^9\) 题解 设\(l\)为这棵树的叶子个数,显然当\(k>\)树的深度时答案都是\(l\). 下面要证明:答案是\(O(l+\frac{n-l}{k})\)的. 我们从下往上贪心,每次选择一个未被覆盖的深度最深的点,覆盖这个点网

codeforces 343D Water Tree 树链剖分 dfs序 线段树 set

题目链接 这道题主要是要考虑到同一棵子树中dfs序是连续的 然后我就直接上树剖了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=600005; 4 5 struct Node 6 { 7 int l,r; 8 int value; 9 void init() 10 { 11 l=r=value=0; 12 } 13 }tree[4*MAXN]; 14 vector<int>nei[MAXN]

Manthan, Codefest 16(G. Yash And Trees(dfs序+线段树))

题目链接:点击打开链接 题意:给你一棵树, 根结点为1, q组操作, 每组操作有两种, 一种是对一个结点的所有子树结点的值全部+1, 另一种是查询一个结点的子树结点上值%m的余数为素数的个数. 思路:对于第一个操作, 我们可以想到用dfs序给树重新标号, 使得一个结点的子树结点为相邻的一条线段, 这样,就可以很容易的用线段树进行处理了.  对于第二个操作, 为了维护一个区间内的值, 我们可以用bitset作为结点信息.  我们可以开一个m位的bitset, 对于每个位, 1表示这个数在此区间中,

HDU 4366 Successor( DFS序+ 线段树 )

Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2631    Accepted Submission(s): 634 Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st