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

· · 题解

题意

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

思路

把边分 3 类:

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

时间复杂度

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

代码实现

AC link

#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;
}