Home AtCoder Beginner Contest 264
Post
Cancel

AtCoder Beginner Contest 264

E. Blackout 2 (DSU)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>

using namespace std;

struct DSU {
    vector<int> a, b;
    vector<int> p;

    DSU(int n, int m)
    {
        a.resize(n + m, 0);
        b.resize(n + m, 0);
        p.resize(n + m, -1);

        for (int i = 0; i < n; i++)
        {
            a[i] = 1;
        }
        for (int i = n; i < n + m; i++)
        {
            b[i] = 1;
        }
    }

    int root(int x)
    {
        if (p[x] == -1)
        {
            return x;
        } else {
            p[x] = root(p[x]);
            return p[x];
        }
    }

    bool connect(int x, int y)
    {
        return root(x) == root(y);
    }

    int get_a(int x)
    {
        return a[root(x)];
    }

    int get_b(int x)
    {
        return b[root(x)];
    }

    void to_union(int x, int y)
    {
        int px = root(x);
        int py = root(y);

        p[px] = py;
        a[py] += a[px];
        b[py] += b[px];
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m, e;
    cin >> n >> m >> e;

    vector<int> u(e), v(e);
    for (int i = 0; i < e; i++)
    {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
    }

    int q;
    cin >> q;
    vector<int> x(q);
    vector<bool> ok(e, true);
    for (int i = 0; i < q; i++)
    {
        cin >> x[i];
        x[i]--;

        ok[x[i]] = false;
    }

    DSU dsu(n, m);
    int cnt = 0;

    for (int i = 0; i < e; i++)
    {
        if (ok[i])
        {
            if (!dsu.connect(u[i], v[i]))
            {
                int ua = dsu.get_a(u[i]);
                int va = dsu.get_a(v[i]);

                int ub = dsu.get_b(u[i]);
                int vb = dsu.get_b(v[i]);

                if (ub > 0 && vb == 0)
                {
                    cnt += va;
                }

                if (ub == 0 && vb > 0)
                {
                    cnt += ua;
                }

                dsu.to_union(u[i], v[i]);
            }
        }
    }

    vector<int> ans(q);
    for (int i = q - 1; i >= 0; i--)
    {
        ans[i] = cnt;

        if (!dsu.connect(u[x[i]], v[x[i]]))
        {
            int ua = dsu.get_a(u[x[i]]);
            int va = dsu.get_a(v[x[i]]);

            int ub = dsu.get_b(u[x[i]]);
            int vb = dsu.get_b(v[x[i]]);

            if (ub > 0 && vb == 0){
                cnt += va;
            }
            if (vb > 0 && ub == 0){
                cnt += ua;
            }

            dsu.to_union(u[x[i]], v[x[i]]);
        }
    }

    for (auto & num : ans)
    {
        cout << num << endl;
    }

    return 0;
}
This post is licensed under CC BY 4.0 by the author.

牛客小白月赛54 A(排序+贪心) B(线段树/差分) C(区间合并+二分查找) D(BFS) E(DP)

Codeforces Round 815 (Div. 2) D1(DP) D2(字典树+DP)