其他分享
首页 > 其他分享> > 【CodeForces 613D】Kingdom and its Cities

【CodeForces 613D】Kingdom and its Cities

作者:互联网

链接

洛谷

题目大意

给定一棵树,多组询问,每组询问给定 \(k\) 个点,你可以删掉不同于那 \(k\) 个点的 \(m\) 个点,使得这 \(k\) 个点两两不连通,要求最小化 \(m\),如果不可能输出 \(-1\)。询问之间独立。

思路

虚树板题。

虚树

针对一些有多组特殊点的树上问题。

如果只有一组特殊点,就是一道简单树上 DP。发现有一些点是多余的,于是考虑把多余的点去掉。具体做法是:特殊点作为关键点,将关键点按\(\mathrm{dfn}\) 排序,求出相邻点的最近公共祖先也作关键点,按照原树祖先后代关系建树即可。

代码:

const int N = 1e5 + 10;

inline ll Read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

int n, m;
int head[N], tot;
struct edge {
	int to, nxt;
}e[N << 1];

void add(int u, int v) {
	e[++tot] = (edge) {v, head[u]}, head[u] = tot;
}

int dfn[N], dep[N], fa[N][25], cnt, son[N];
void dfs(int u, int fat) {
	dfn[u] = ++cnt;
	fa[u][0] = fat;
	dep[u] = dep[fat] + 1;
	for (int i = head[u]; i; i = e[i].nxt) {
		if (e[i].to == fat) continue;
		dfs(e[i].to, u);
	}
	son[u] = cnt;
}

int LCA(int u, int v) {
	if (dep[u] > dep[v]) swap(u, v);
	for (int j = 21; j >= 0; j--) 
		if (dep[fa[v][j]] >= dep[u]) 
			v = fa[v][j];
	if (u == v) return u;
	for (int j = 21; j >= 0; j--) 
		if (fa[u][j] != fa[v][j]) 
			u = fa[u][j], v = fa[v][j];
	return fa[u][0];
}

int a[N << 1];
bool vis[N];
namespace VirtualTree {
	int head[N], tot;
	struct edge {
		int to, nxt;
	}e[N << 1];

	void add(int u, int v) {
		e[++tot] = (edge) {v, head[u]}, head[u] = tot;
	}
	
	bool cmp (int a, int b) {
		return dfn[a] < dfn[b];
	} 

	int stack[N];
	void Build () {
		memset (head, 0, sizeof head);
		tot = 0;
		sort (a + 1, a + 1 + m, cmp);
		for (int i = 1, n = m; i < n; i++)
			a[++m] = LCA(a[i], a[i + 1]);
		sort (a + 1, a + 1 + m, cmp);
		m = unique(a + 1, a + 1 + m) - a - 1;
		int top = 0;
		stack[++top] = a[1];
		for (int i = 2; i <= m; i++) {
			for (; top && son[stack[top]] < dfn[a[i]]; top--);
			if (top) add(stack[top], a[i]);
			stack[++top] = a[i];
		}
	}

	int f[N], siz[N];
	void dfs(int u) {
		f[u] = 0;
		if (vis[u]) {
			siz[u] = 1;
			for (int i = head[u]; i; i = e[i].nxt) {
				int v = e[i].to;
				dfs (v);
				f[u] += f[v];
				if (siz[v]) f[u] ++;
			}
		} else {
			siz[u] = 0;
			for (int i = head[u]; i; i = e[i].nxt) {
				int v = e[i].to;
				dfs (v);
				f[u] += f[v];
				siz[u] += siz[v];
			}
			if (siz[u] > 1) {
				siz[u] = 0;
				f[u]++;
			}
		}
	}
}


int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	n = Read();
	for (int i = 1; i < n; i++) {
		int u = Read(), v = Read();
		add(u, v), add(v, u);
	}
	dfs (1, 0);
	for (int j = 1; j <= 21; j++)
		for (int i = 1; i <= n; i++) 
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
	for (int t = Read(); t--; ) {
		m = Read();
		memset (vis, 0, sizeof vis);
		for (int i = 1; i <= m; i++) 
			a[i] = Read(), vis[a[i]] = 1;
		bool flag = 0;
		for (int i = 1; i <= m; i++) 
			if (vis[fa[a[i]][0]]) {flag = 1; break;}
		if (flag) {puts("-1"); continue;}
		VirtualTree::Build();
		VirtualTree::dfs(a[1]);
		printf("%d\n", VirtualTree::f[a[1]]);
	}
	return 0;
}

标签:Kingdom,dep,个点,int,CodeForces,fa,Read,Cities,getchar
来源: https://www.cnblogs.com/GJY-JURUO/p/16253152.html