跳到正文

题解

题解:AT_agc064_b [AGC064B] Red and Blue Spanning Tree

cbcyh约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年2月24日
收录
2026年9月30日
更新
2026年11月10日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

题意

给定一个无向连通图,每个点是红色或蓝色的,每条边也是红色或蓝色的。每种颜色的点都得有一条相应颜色的边连向它,要求在这种限制下构造出一棵生成树。

思路

把边分 33 类:

  • 第 11 类:这条边的颜色和这条边两个端点的颜色都相同。
  • 第 22 类:这条边的颜色和这条边其中一个端点的颜色相同。
  • 第 33 类:这条边的颜色和这条边两个端点的颜色都不相同。

首先,我们把第 11 类边尽可能地选,要保证不会构成环。
其次,我们来选第 22 类边,设与这条边颜色相同地那个端点为 xx 点,颜色不同的点为 yy 点,只有当 yy 之前满足了条件且 xx 之前没有满足条件时,我们才会选这条边。这里的满足条件指有一条和该点颜色相同的边被选择了。还要注意一下选边的顺序,我们应以一种 bfs 的顺序来枚举点,然后枚举这条点连出去的边。
具体实现:我们用一个队列来记录已经满足条件了的点,每次把队首的点出队,枚举它连出去的 22 类边,如果这条边的另一个端点之前没有满足条件且不在一个连通块,就把那个点标记为满足条件,加入队尾。实际上只需要不在一个连通块就可以加入队尾,因为加了肯定不劣。
证明:如果我们选了一条 22 类边,它两个端点都还没有满足条件,那么我们肯定会留下一个顶点仍然没满足条件。我们是按 bfs 的顺序来枚举边的,所以之前的 11 类边肯定不可能帮上忙,如果能帮上忙我们应该是一路 bfs 过来的,肯定有一个端点满足条件。这样地话,仅靠 22 类边的话,除非练成环,否则不可能凭空让一个连通块的点全部满足条件。这样就证明成功了。
最后,我们把所有之前没选的边都尽可能地选以保证连通性。当然,不能构成环。

时间复杂度

因为用到并查集,所以时间复杂度要带 α(n)\alpha(n)。
时间复杂度:(n+m)⋅α(n)(n+m) \cdot \alpha(n)

代码实现

AC link

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define unll unsigned long long
#define bint __int128
#define set_q priority_queue
#define unmp unordered_map
mt19937_64 mrand((unll) std::chrono::steady_clock::now().time_since_epoch().count());

inline ll in () {
	ll x = 0; bool f = false; char ch = getchar();
	while (ch < '0' || ch > '9') {f |= ch == '-'; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return f ? -x : x;
}

const ll p = 1000000007, inf = 1ll << 60ll, infless = -(1ll << 60ll);

inline ll quickpow (const ll &a, const ll &n) {
	ll res = 1ll, t = a;
	for (ll y = n; y > 0; y >>= 1) {
		if (y & 1) {
			res *= t;
			res %= p;
		}
		t *= t;
		t %= p;
	}
	return res;
}

const int N = 200100, M = 520;

int test, n, m, a[N], len1, len2, fa[N];
char s[N], t[M];
bool b[N];
vector<array<int, 2> > edges[N];
vector<int> e;
array<int, 4> edges1[N], edges2[N], edge[N];
ll ans = 0ll;

inline int findset (int x) {
	return x == fa[x] ? x : fa[x] = findset(fa[x]);
}

inline void bfs () {
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (b[i]) {
			q.push(i);
		}
	}
	while (!q.empty()) {
		auto x = q.front();
		q.pop();
		for (auto [y, id] : edges[x]) {
			int fx = findset(x), fy = findset(y);
			if (fx != fy) {
				fa[fy] = fx;
				if (!b[y]) {
					b[y] = true;
					q.push(y);
				}
				e.push_back(id);
			}
		}
	}
}

inline void solve () {
	ans = 0ll;
	//solve clear !
	n = in(), m = in();
	for (int i = 1; i <= m; i++) {
		int x = in(), y = in();
		scanf("%s", t + 1);
		int ty = 0;
		if (t[1] == 'B') {
			ty = 1;
		}
		edge[i] = {x, y, ty, i};
	}
	scanf("%s", s + 1);
	for (int i = 1; i <= n; i++) {
		a[i] = s[i] == 'B' ? 1 : 0;
	}
	for (int i = 1; i <= m; i++) {
		auto [x, y, ty, id] = edge[i];
		if (ty == a[x] && ty == a[y]) {
			edges1[++len1] = edge[i];
		} else {
			if (ty != a[x] && ty != a[y]) {
				edges2[++len2] = edge[i];
			} else {
				if (ty == a[x]) {
					edges[y].push_back({x, id});
				} else {
					edges[x].push_back({y, id});
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
	}
	for (int i = 1; i <= len1; i++) {
		auto [x, y, ty, id] = edges1[i];
		int fx = findset(x), fy = findset(y);
		if (fx != fy) {
			fa[fx] = fy;
			b[x] = b[y] = true;
			e.push_back(id);
		}
	}
	bfs();
	for (int i = 1; i <= m; i++) {
		auto [x, y, ty, id] = edge[i];
		int fx = findset(x), fy = findset(y);
		if (fx != fy) {
			e.push_back(id);
			fa[fx] = fy;
		}
	}
	for (int i = 1; i <= n; i++) {
		int fx = findset(i);
		if (!b[i] || fx != findset(1)) {
			puts("No");
			return;
		}
	}
	sort(e.begin(), e.end());
	e.erase(unique(e.begin(), e.end()), e.end());
	puts("Yes");
	for (auto x : e) {
		printf("%d ", x);
	}
	puts("");
	return;
}

int main() {
	for (test = 1; test--; solve());
	return 0;
}
原文