Submission #770943

# Submission time Handle Problem Language Result Execution time Memory
770943 2023-07-02T07:30:20 Z PurpleCrayon Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1402 ms 88044 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;

int n, m, par[N];
vector<int> st[N];
set<ar<int, 2>> ed;
set<int> in[N];
vector<ar<int, 2>> use[N];

ll ans = 0;
ll f(int x) {
    assert(x == par[x]);
    return (long long) sz(st[x]) * sz(in[x]);
};

ll g(int x) {
    assert(x == par[x]);
    return (long long) sz(st[x]) * (sz(st[x]) - 1);
}


void merge(int a, int b) {
    a = par[a], b = par[b];
    if (a == b) return;
    if (sz(st[a]) < sz(st[b])) swap(a, b);

    vector<ar<int, 2>> change;

    ans -= g(a), ans -= g(b);
    ans -= f(a), ans -= f(b);
    for (int x : st[b]) {
        par[x] = a;
        st[a].push_back(x);
        in[a].erase(x);
    }

    for (auto e : use[b]) {
        change.push_back({e[0], e[1]});
    }
    use[b].clear();

    st[b].clear();

    for (int x : in[b]) {
        if (par[x] != a) {
            in[a].insert(x);
        }
    }
    in[b].clear();

    vector<ar<int, 2>> to_merge;
    for (auto e : change) {
        ar<int, 2> ne{par[e[0]], par[e[1]]};
        if (ne[0] == ne[1]) continue;

        ed.insert(ne);
        if (ed.count({ne[1], ne[0]})) {
            to_merge.push_back(ne);
        }
        use[ne[0]].push_back(ne);
        use[ne[1]].push_back(ne);
    }

    ans += f(a);
    ans += g(a);

    for (auto e : to_merge) {
        if (par[e[0]] != par[e[1]]) {
            merge(e[0], e[1]);
        }
    }
}

void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        st[i].push_back(i);
        par[i] = i;
    }

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b, --a, --b;

        ans -= f(par[b]);
        if (par[a] != par[b]) in[par[b]].insert(a);
        ans += f(par[b]);

        ed.insert({par[a], par[b]});
        use[par[a]].push_back({par[a], par[b]});
        use[par[b]].push_back({par[a], par[b]});
        if (ed.count({par[b], par[a]})) {
            merge(a, b);
        }

        cout << ans << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9716 KB Output is correct
4 Correct 5 ms 9716 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9720 KB Output is correct
12 Correct 5 ms 9744 KB Output is correct
13 Correct 6 ms 9720 KB Output is correct
14 Correct 5 ms 9704 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9720 KB Output is correct
18 Correct 6 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9724 KB Output is correct
24 Correct 5 ms 9716 KB Output is correct
25 Correct 5 ms 9724 KB Output is correct
26 Correct 5 ms 9720 KB Output is correct
27 Correct 4 ms 9720 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 5 ms 9812 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9716 KB Output is correct
4 Correct 5 ms 9716 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9720 KB Output is correct
12 Correct 5 ms 9744 KB Output is correct
13 Correct 6 ms 9720 KB Output is correct
14 Correct 5 ms 9704 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9720 KB Output is correct
18 Correct 6 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9724 KB Output is correct
24 Correct 5 ms 9716 KB Output is correct
25 Correct 5 ms 9724 KB Output is correct
26 Correct 5 ms 9720 KB Output is correct
27 Correct 4 ms 9720 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 5 ms 9812 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9812 KB Output is correct
34 Correct 7 ms 10196 KB Output is correct
35 Correct 98 ms 23872 KB Output is correct
36 Correct 129 ms 27244 KB Output is correct
37 Correct 122 ms 27440 KB Output is correct
38 Correct 137 ms 26888 KB Output is correct
39 Correct 7 ms 10068 KB Output is correct
40 Correct 9 ms 10324 KB Output is correct
41 Correct 8 ms 10324 KB Output is correct
42 Correct 9 ms 10112 KB Output is correct
43 Correct 7 ms 10068 KB Output is correct
44 Correct 7 ms 10116 KB Output is correct
45 Correct 9 ms 10068 KB Output is correct
46 Correct 7 ms 10112 KB Output is correct
47 Correct 8 ms 10196 KB Output is correct
48 Correct 9 ms 10252 KB Output is correct
49 Correct 18 ms 11568 KB Output is correct
50 Correct 129 ms 27572 KB Output is correct
51 Correct 11 ms 10580 KB Output is correct
52 Correct 133 ms 25376 KB Output is correct
53 Correct 14 ms 11348 KB Output is correct
54 Correct 120 ms 26416 KB Output is correct
55 Correct 10 ms 10452 KB Output is correct
56 Correct 9 ms 10468 KB Output is correct
57 Correct 13 ms 11244 KB Output is correct
58 Correct 10 ms 11176 KB Output is correct
59 Correct 8 ms 10196 KB Output is correct
60 Correct 87 ms 23088 KB Output is correct
61 Correct 11 ms 10372 KB Output is correct
62 Correct 118 ms 26684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9716 KB Output is correct
4 Correct 5 ms 9716 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9720 KB Output is correct
12 Correct 5 ms 9744 KB Output is correct
13 Correct 6 ms 9720 KB Output is correct
14 Correct 5 ms 9704 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9720 KB Output is correct
18 Correct 6 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9724 KB Output is correct
24 Correct 5 ms 9716 KB Output is correct
25 Correct 5 ms 9724 KB Output is correct
26 Correct 5 ms 9720 KB Output is correct
27 Correct 4 ms 9720 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 5 ms 9812 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9812 KB Output is correct
34 Correct 7 ms 10196 KB Output is correct
35 Correct 98 ms 23872 KB Output is correct
36 Correct 129 ms 27244 KB Output is correct
37 Correct 122 ms 27440 KB Output is correct
38 Correct 137 ms 26888 KB Output is correct
39 Correct 7 ms 10068 KB Output is correct
40 Correct 9 ms 10324 KB Output is correct
41 Correct 8 ms 10324 KB Output is correct
42 Correct 9 ms 10112 KB Output is correct
43 Correct 7 ms 10068 KB Output is correct
44 Correct 7 ms 10116 KB Output is correct
45 Correct 9 ms 10068 KB Output is correct
46 Correct 7 ms 10112 KB Output is correct
47 Correct 8 ms 10196 KB Output is correct
48 Correct 9 ms 10252 KB Output is correct
49 Correct 18 ms 11568 KB Output is correct
50 Correct 129 ms 27572 KB Output is correct
51 Correct 11 ms 10580 KB Output is correct
52 Correct 133 ms 25376 KB Output is correct
53 Correct 14 ms 11348 KB Output is correct
54 Correct 120 ms 26416 KB Output is correct
55 Correct 10 ms 10452 KB Output is correct
56 Correct 9 ms 10468 KB Output is correct
57 Correct 13 ms 11244 KB Output is correct
58 Correct 10 ms 11176 KB Output is correct
59 Correct 8 ms 10196 KB Output is correct
60 Correct 87 ms 23088 KB Output is correct
61 Correct 11 ms 10372 KB Output is correct
62 Correct 118 ms 26684 KB Output is correct
63 Correct 632 ms 55524 KB Output is correct
64 Correct 686 ms 55500 KB Output is correct
65 Correct 663 ms 55532 KB Output is correct
66 Correct 231 ms 32112 KB Output is correct
67 Correct 512 ms 43064 KB Output is correct
68 Correct 226 ms 32044 KB Output is correct
69 Correct 339 ms 32036 KB Output is correct
70 Correct 217 ms 32104 KB Output is correct
71 Correct 221 ms 32124 KB Output is correct
72 Correct 563 ms 39892 KB Output is correct
73 Correct 574 ms 39968 KB Output is correct
74 Correct 1402 ms 88044 KB Output is correct
75 Correct 616 ms 40372 KB Output is correct
76 Correct 1010 ms 56492 KB Output is correct
77 Correct 1010 ms 56704 KB Output is correct
78 Correct 206 ms 32440 KB Output is correct
79 Correct 423 ms 40624 KB Output is correct
80 Correct 221 ms 32444 KB Output is correct
81 Correct 431 ms 39832 KB Output is correct
82 Correct 789 ms 48148 KB Output is correct
83 Correct 867 ms 48168 KB Output is correct
84 Correct 785 ms 82104 KB Output is correct
85 Correct 802 ms 82060 KB Output is correct
86 Correct 296 ms 37748 KB Output is correct
87 Correct 325 ms 41880 KB Output is correct
88 Correct 522 ms 40132 KB Output is correct
89 Correct 893 ms 52896 KB Output is correct