Submission #226192

# Submission time Handle Problem Language Result Execution time Memory
226192 2020-04-22T19:12:07 Z Minnakhmetov Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1601 ms 82556 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5;
int w[N];
set<int> *members[N], *ingoing_ver[N], *outgoing_gr[N], *ingoing_gr[N];
ll ans = 0;
int n, m;

int find_set(int x) {
    return w[x] < 0 ? x : w[x] = find_set(w[x]);
}

void mrg(int x, int y) {
    x = find_set(x);
    y = find_set(y);

    if (x == y)
        return;

    ans -= members[x]->size() * (ll)(members[x]->size() - 1);
    ans -= members[y]->size() * (ll)(members[y]->size() - 1);

    ans -= ingoing_ver[x]->size() * (ll)members[x]->size();
    ans -= ingoing_ver[y]->size() * (ll)members[y]->size();

    if (members[x]->size() + ingoing_ver[x]->size() < 
        members[y]->size() + ingoing_ver[y]->size()) {
        swap(members[x], members[y]);
        swap(ingoing_ver[x], ingoing_ver[y]);
    }

    for (const int &el : *members[y]) {
        members[x]->insert(el);
        ingoing_ver[x]->erase(el);
    }

    for (const int &el : *ingoing_ver[y]) {
        if (!members[x]->count(el)) {
            ingoing_ver[x]->insert(el);
        }
    }

    ans += ingoing_ver[x]->size() * (ll)members[x]->size();
    ans += members[x]->size() * (ll)(members[x]->size() - 1);

    if (ingoing_gr[x]->size() + outgoing_gr[x]->size() < 
        ingoing_gr[y]->size() + outgoing_gr[y]->size()) {
        swap(x, y);
        swap(members[x], members[y]);
        swap(ingoing_ver[x], ingoing_ver[y]);
    }

    ingoing_gr[y]->erase(x);
    ingoing_gr[x]->erase(y);

    vector<int> to_mrg;

    for (const int &el : *ingoing_gr[y]) {
        ingoing_gr[x]->insert(el);
        outgoing_gr[el]->erase(y);
        outgoing_gr[el]->insert(x);
        if (ingoing_gr[el]->count(x) &&
            outgoing_gr[el]->count(x)) {
            to_mrg.push_back(el);
        }
    }

    outgoing_gr[y]->erase(x);
    outgoing_gr[x]->erase(y);

    for (const int &el : *outgoing_gr[y]) {
        outgoing_gr[x]->insert(el);
        ingoing_gr[el]->erase(y);
        ingoing_gr[el]->insert(x);
        if (ingoing_gr[el]->count(x) &&
            outgoing_gr[el]->count(x)) {
            to_mrg.push_back(el);
        }
    }

    // cout << "!\n";
    // for (int i : *outgoing_gr[4]) {
    //     cout << i + 1 << " ";
    // }
    // cout << "\n";
    // for (int i : *ingoing_gr[4]) {
    //     cout << i + 1 << " ";
    // }
    // cout << "\n";

    w[y] = x;

    for (const int &el : to_mrg) {
        mrg(el, x);
    }
}

void add_edge(int x, int y) {
    y = find_set(y);

    if (find_set(x) == y)
        return;

    if (!ingoing_ver[y]->count(x)) {
        ans += members[y]->size();
        ingoing_ver[y]->insert(x);
    }

    x = find_set(x);

    ingoing_gr[y]->insert(x);
    outgoing_gr[x]->insert(y);

    if (outgoing_gr[y]->count(x)) {
        mrg(x, y);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    for (int i = 0; i < n; i++) {
        w[i] = -1;
        ingoing_gr[i] = new set<int>();
        outgoing_gr[i] = new set<int>();
        ingoing_ver[i] = new set<int>();
        members[i] = new set<int>();
        members[i]->insert(i);
    }

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

        add_edge(x, y);

        cout << ans << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 400 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 416 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 7 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 400 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 416 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 7 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 8 ms 512 KB Output is correct
35 Correct 100 ms 6520 KB Output is correct
36 Correct 118 ms 8824 KB Output is correct
37 Correct 124 ms 8952 KB Output is correct
38 Correct 113 ms 8568 KB Output is correct
39 Correct 8 ms 1280 KB Output is correct
40 Correct 9 ms 1664 KB Output is correct
41 Correct 9 ms 1664 KB Output is correct
42 Correct 8 ms 1280 KB Output is correct
43 Correct 10 ms 1280 KB Output is correct
44 Correct 9 ms 1280 KB Output is correct
45 Correct 8 ms 1280 KB Output is correct
46 Correct 8 ms 1280 KB Output is correct
47 Correct 9 ms 1536 KB Output is correct
48 Correct 10 ms 1536 KB Output is correct
49 Correct 16 ms 2304 KB Output is correct
50 Correct 124 ms 9080 KB Output is correct
51 Correct 15 ms 1664 KB Output is correct
52 Correct 116 ms 7288 KB Output is correct
53 Correct 16 ms 2176 KB Output is correct
54 Correct 125 ms 8056 KB Output is correct
55 Correct 13 ms 1664 KB Output is correct
56 Correct 15 ms 1664 KB Output is correct
57 Correct 11 ms 1920 KB Output is correct
58 Correct 11 ms 1920 KB Output is correct
59 Correct 9 ms 1792 KB Output is correct
60 Correct 89 ms 5752 KB Output is correct
61 Correct 10 ms 1536 KB Output is correct
62 Correct 120 ms 8440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 400 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 416 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 7 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 8 ms 512 KB Output is correct
35 Correct 100 ms 6520 KB Output is correct
36 Correct 118 ms 8824 KB Output is correct
37 Correct 124 ms 8952 KB Output is correct
38 Correct 113 ms 8568 KB Output is correct
39 Correct 8 ms 1280 KB Output is correct
40 Correct 9 ms 1664 KB Output is correct
41 Correct 9 ms 1664 KB Output is correct
42 Correct 8 ms 1280 KB Output is correct
43 Correct 10 ms 1280 KB Output is correct
44 Correct 9 ms 1280 KB Output is correct
45 Correct 8 ms 1280 KB Output is correct
46 Correct 8 ms 1280 KB Output is correct
47 Correct 9 ms 1536 KB Output is correct
48 Correct 10 ms 1536 KB Output is correct
49 Correct 16 ms 2304 KB Output is correct
50 Correct 124 ms 9080 KB Output is correct
51 Correct 15 ms 1664 KB Output is correct
52 Correct 116 ms 7288 KB Output is correct
53 Correct 16 ms 2176 KB Output is correct
54 Correct 125 ms 8056 KB Output is correct
55 Correct 13 ms 1664 KB Output is correct
56 Correct 15 ms 1664 KB Output is correct
57 Correct 11 ms 1920 KB Output is correct
58 Correct 11 ms 1920 KB Output is correct
59 Correct 9 ms 1792 KB Output is correct
60 Correct 89 ms 5752 KB Output is correct
61 Correct 10 ms 1536 KB Output is correct
62 Correct 120 ms 8440 KB Output is correct
63 Correct 709 ms 81272 KB Output is correct
64 Correct 488 ms 81528 KB Output is correct
65 Correct 754 ms 81272 KB Output is correct
66 Correct 308 ms 47480 KB Output is correct
67 Correct 459 ms 74104 KB Output is correct
68 Correct 262 ms 47480 KB Output is correct
69 Correct 704 ms 48504 KB Output is correct
70 Correct 379 ms 47456 KB Output is correct
71 Correct 330 ms 47480 KB Output is correct
72 Correct 546 ms 58360 KB Output is correct
73 Correct 713 ms 60024 KB Output is correct
74 Correct 1601 ms 82556 KB Output is correct
75 Correct 966 ms 55288 KB Output is correct
76 Correct 900 ms 67940 KB Output is correct
77 Correct 1204 ms 67916 KB Output is correct
78 Correct 250 ms 51040 KB Output is correct
79 Correct 453 ms 55520 KB Output is correct
80 Correct 264 ms 49912 KB Output is correct
81 Correct 475 ms 53624 KB Output is correct
82 Correct 815 ms 67296 KB Output is correct
83 Correct 819 ms 67364 KB Output is correct
84 Correct 769 ms 78752 KB Output is correct
85 Correct 791 ms 78712 KB Output is correct
86 Correct 376 ms 80248 KB Output is correct
87 Correct 367 ms 82552 KB Output is correct
88 Correct 571 ms 59260 KB Output is correct
89 Correct 1140 ms 65400 KB Output is correct