Submission #770930

# Submission time Handle Problem Language Result Execution time Memory
770930 2023-07-02T07:10:00 Z PurpleCrayon Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
4939 ms 1048576 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[x]) {
            change.push_back({e[0], e[1]});
        }
        use[x].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]]};
        ed.insert(ne);
        if (par[ne[0]] != par[ne[1]] && 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 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9716 KB Output is correct
6 Correct 4 ms 9680 KB Output is correct
7 Correct 5 ms 9824 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 6 ms 9644 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9724 KB Output is correct
21 Correct 5 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 4 ms 9812 KB Output is correct
24 Correct 5 ms 9720 KB Output is correct
25 Correct 5 ms 9824 KB Output is correct
26 Correct 4 ms 9684 KB Output is correct
27 Correct 5 ms 9684 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9736 KB Output is correct
30 Correct 5 ms 9720 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 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9716 KB Output is correct
6 Correct 4 ms 9680 KB Output is correct
7 Correct 5 ms 9824 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 6 ms 9644 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9724 KB Output is correct
21 Correct 5 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 4 ms 9812 KB Output is correct
24 Correct 5 ms 9720 KB Output is correct
25 Correct 5 ms 9824 KB Output is correct
26 Correct 4 ms 9684 KB Output is correct
27 Correct 5 ms 9684 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9736 KB Output is correct
30 Correct 5 ms 9720 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 8 ms 10244 KB Output is correct
35 Correct 100 ms 23524 KB Output is correct
36 Correct 132 ms 26480 KB Output is correct
37 Correct 126 ms 26596 KB Output is correct
38 Correct 126 ms 26204 KB Output is correct
39 Correct 7 ms 10196 KB Output is correct
40 Correct 18 ms 15024 KB Output is correct
41 Correct 17 ms 14068 KB Output is correct
42 Correct 7 ms 10196 KB Output is correct
43 Correct 8 ms 10260 KB Output is correct
44 Correct 8 ms 10196 KB Output is correct
45 Correct 7 ms 10220 KB Output is correct
46 Correct 7 ms 10196 KB Output is correct
47 Correct 11 ms 11196 KB Output is correct
48 Correct 14 ms 11276 KB Output is correct
49 Correct 17 ms 11944 KB Output is correct
50 Correct 128 ms 26684 KB Output is correct
51 Correct 11 ms 10652 KB Output is correct
52 Correct 112 ms 24884 KB Output is correct
53 Correct 15 ms 11688 KB Output is correct
54 Correct 114 ms 25712 KB Output is correct
55 Correct 12 ms 10792 KB Output is correct
56 Correct 9 ms 10732 KB Output is correct
57 Correct 9 ms 11224 KB Output is correct
58 Correct 11 ms 11264 KB Output is correct
59 Correct 36 ms 22600 KB Output is correct
60 Correct 111 ms 38720 KB Output is correct
61 Correct 11 ms 10960 KB Output is correct
62 Correct 117 ms 25956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9716 KB Output is correct
6 Correct 4 ms 9680 KB Output is correct
7 Correct 5 ms 9824 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 6 ms 9644 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9724 KB Output is correct
21 Correct 5 ms 9812 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 4 ms 9812 KB Output is correct
24 Correct 5 ms 9720 KB Output is correct
25 Correct 5 ms 9824 KB Output is correct
26 Correct 4 ms 9684 KB Output is correct
27 Correct 5 ms 9684 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 5 ms 9736 KB Output is correct
30 Correct 5 ms 9720 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 8 ms 10244 KB Output is correct
35 Correct 100 ms 23524 KB Output is correct
36 Correct 132 ms 26480 KB Output is correct
37 Correct 126 ms 26596 KB Output is correct
38 Correct 126 ms 26204 KB Output is correct
39 Correct 7 ms 10196 KB Output is correct
40 Correct 18 ms 15024 KB Output is correct
41 Correct 17 ms 14068 KB Output is correct
42 Correct 7 ms 10196 KB Output is correct
43 Correct 8 ms 10260 KB Output is correct
44 Correct 8 ms 10196 KB Output is correct
45 Correct 7 ms 10220 KB Output is correct
46 Correct 7 ms 10196 KB Output is correct
47 Correct 11 ms 11196 KB Output is correct
48 Correct 14 ms 11276 KB Output is correct
49 Correct 17 ms 11944 KB Output is correct
50 Correct 128 ms 26684 KB Output is correct
51 Correct 11 ms 10652 KB Output is correct
52 Correct 112 ms 24884 KB Output is correct
53 Correct 15 ms 11688 KB Output is correct
54 Correct 114 ms 25712 KB Output is correct
55 Correct 12 ms 10792 KB Output is correct
56 Correct 9 ms 10732 KB Output is correct
57 Correct 9 ms 11224 KB Output is correct
58 Correct 11 ms 11264 KB Output is correct
59 Correct 36 ms 22600 KB Output is correct
60 Correct 111 ms 38720 KB Output is correct
61 Correct 11 ms 10960 KB Output is correct
62 Correct 117 ms 25956 KB Output is correct
63 Correct 603 ms 55536 KB Output is correct
64 Correct 709 ms 55468 KB Output is correct
65 Correct 645 ms 55548 KB Output is correct
66 Correct 221 ms 36656 KB Output is correct
67 Correct 3657 ms 689652 KB Output is correct
68 Correct 269 ms 36964 KB Output is correct
69 Correct 430 ms 38356 KB Output is correct
70 Correct 255 ms 36824 KB Output is correct
71 Correct 242 ms 36740 KB Output is correct
72 Correct 832 ms 92600 KB Output is correct
73 Correct 918 ms 116540 KB Output is correct
74 Correct 1788 ms 135368 KB Output is correct
75 Correct 653 ms 44676 KB Output is correct
76 Correct 1117 ms 73040 KB Output is correct
77 Correct 1113 ms 73220 KB Output is correct
78 Correct 229 ms 34376 KB Output is correct
79 Correct 491 ms 44144 KB Output is correct
80 Correct 256 ms 34448 KB Output is correct
81 Correct 478 ms 43440 KB Output is correct
82 Correct 959 ms 54488 KB Output is correct
83 Correct 953 ms 54512 KB Output is correct
84 Correct 927 ms 86824 KB Output is correct
85 Correct 930 ms 86892 KB Output is correct
86 Runtime error 4939 ms 1048576 KB Execution killed with signal 9
87 Halted 0 ms 0 KB -