Submission #720063

# Submission time Handle Problem Language Result Execution time Memory
720063 2023-04-07T10:53:57 Z walterw Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1060 ms 107692 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

ll sz[100005], par[100005];

int findset(int i) {
    return (par[i] == i? i : par[i] = findset(par[i]));
}

ll ans = 0;
set<ll> in[100005], out[100005];
queue<pair<ll, ll>> q;

void join(int a, int b) {
    a = findset(a);
    b = findset(b);

    if (a == b) return;

    if (sz[a] > sz[b]) swap(a, b);
    ans -= (sz[a] * in[a].size() + sz[b] * in[b].size());

    par[a] = b;
    sz[b] += sz[a];

    for (auto a : in[a]) {
        in[b].insert(a);
        out[findset(a)].insert(b);

        if (findset(a) != b && out[b].find(findset(a)) != out[b].end()) {
            q.push({a, b});
        }
    }

    for (auto cur : out[a]) {
        out[b].insert(cur);
        if (findset(cur) != b && out[findset(cur)].find(b) != out[findset(cur)].end()) {
            q.push({cur, b});
        }
    }

    ans += sz[b] * in[b].size();
}


void add(int a, int b) {
    b = findset(b);

    if (in[b].find(a) != in[b].end() || findset(a) == b) return;

    if (out[b].find(findset(a)) != out[b].end()) {
        // join them
        q.push({a, b});
        while (!q.empty()) {
            auto [c, d] = q.front();
            q.pop();
            join(c, d);
        }
        return;
    }

    in[b].insert(a);
    out[findset(a)].insert(b);
    ans += sz[b];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, m; cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        sz[i] = 1; par[i] = i;
        in[i].insert(i);
        out[i].insert(i);
    }

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        add(a, b);
        cout << ans << "\n";
    }
}
# 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 5 ms 9684 KB Output is correct
4 Correct 5 ms 9732 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9736 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9628 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 5 ms 9744 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 6 ms 9684 KB Output is correct
25 Correct 6 ms 9684 KB Output is correct
26 Correct 7 ms 9684 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 7 ms 9640 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9684 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 5 ms 9684 KB Output is correct
4 Correct 5 ms 9732 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9736 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9628 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 5 ms 9744 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 6 ms 9684 KB Output is correct
25 Correct 6 ms 9684 KB Output is correct
26 Correct 7 ms 9684 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 7 ms 9640 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 8 ms 9812 KB Output is correct
35 Correct 91 ms 12928 KB Output is correct
36 Correct 140 ms 15128 KB Output is correct
37 Correct 114 ms 15248 KB Output is correct
38 Correct 111 ms 14892 KB Output is correct
39 Correct 8 ms 10196 KB Output is correct
40 Correct 11 ms 10916 KB Output is correct
41 Correct 10 ms 10916 KB Output is correct
42 Correct 7 ms 10196 KB Output is correct
43 Correct 8 ms 10196 KB Output is correct
44 Correct 7 ms 10204 KB Output is correct
45 Correct 7 ms 10268 KB Output is correct
46 Correct 9 ms 10184 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 9 ms 10580 KB Output is correct
49 Correct 14 ms 11092 KB Output is correct
50 Correct 126 ms 15228 KB Output is correct
51 Correct 11 ms 10452 KB Output is correct
52 Correct 107 ms 13888 KB Output is correct
53 Correct 13 ms 11000 KB Output is correct
54 Correct 123 ms 14472 KB Output is correct
55 Correct 10 ms 10580 KB Output is correct
56 Correct 14 ms 10564 KB Output is correct
57 Correct 9 ms 10580 KB Output is correct
58 Correct 12 ms 10580 KB Output is correct
59 Correct 10 ms 11088 KB Output is correct
60 Correct 94 ms 13408 KB Output is correct
61 Correct 10 ms 10580 KB Output is correct
62 Correct 125 ms 14664 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 5 ms 9684 KB Output is correct
4 Correct 5 ms 9732 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9736 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9628 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 5 ms 9744 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 6 ms 9684 KB Output is correct
25 Correct 6 ms 9684 KB Output is correct
26 Correct 7 ms 9684 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 7 ms 9640 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 8 ms 9812 KB Output is correct
35 Correct 91 ms 12928 KB Output is correct
36 Correct 140 ms 15128 KB Output is correct
37 Correct 114 ms 15248 KB Output is correct
38 Correct 111 ms 14892 KB Output is correct
39 Correct 8 ms 10196 KB Output is correct
40 Correct 11 ms 10916 KB Output is correct
41 Correct 10 ms 10916 KB Output is correct
42 Correct 7 ms 10196 KB Output is correct
43 Correct 8 ms 10196 KB Output is correct
44 Correct 7 ms 10204 KB Output is correct
45 Correct 7 ms 10268 KB Output is correct
46 Correct 9 ms 10184 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 9 ms 10580 KB Output is correct
49 Correct 14 ms 11092 KB Output is correct
50 Correct 126 ms 15228 KB Output is correct
51 Correct 11 ms 10452 KB Output is correct
52 Correct 107 ms 13888 KB Output is correct
53 Correct 13 ms 11000 KB Output is correct
54 Correct 123 ms 14472 KB Output is correct
55 Correct 10 ms 10580 KB Output is correct
56 Correct 14 ms 10564 KB Output is correct
57 Correct 9 ms 10580 KB Output is correct
58 Correct 12 ms 10580 KB Output is correct
59 Correct 10 ms 11088 KB Output is correct
60 Correct 94 ms 13408 KB Output is correct
61 Correct 10 ms 10580 KB Output is correct
62 Correct 125 ms 14664 KB Output is correct
63 Correct 381 ms 50836 KB Output is correct
64 Correct 405 ms 50892 KB Output is correct
65 Correct 419 ms 50892 KB Output is correct
66 Correct 252 ms 36852 KB Output is correct
67 Correct 548 ms 90392 KB Output is correct
68 Correct 278 ms 39112 KB Output is correct
69 Correct 375 ms 39168 KB Output is correct
70 Correct 275 ms 39060 KB Output is correct
71 Correct 357 ms 39076 KB Output is correct
72 Correct 583 ms 58956 KB Output is correct
73 Correct 571 ms 62240 KB Output is correct
74 Correct 1060 ms 84300 KB Output is correct
75 Correct 697 ms 46232 KB Output is correct
76 Correct 885 ms 63360 KB Output is correct
77 Correct 717 ms 63584 KB Output is correct
78 Correct 281 ms 41676 KB Output is correct
79 Correct 463 ms 49492 KB Output is correct
80 Correct 239 ms 42756 KB Output is correct
81 Correct 509 ms 49088 KB Output is correct
82 Correct 674 ms 57020 KB Output is correct
83 Correct 642 ms 57132 KB Output is correct
84 Correct 560 ms 57112 KB Output is correct
85 Correct 585 ms 57040 KB Output is correct
86 Correct 446 ms 105440 KB Output is correct
87 Correct 502 ms 107692 KB Output is correct
88 Correct 565 ms 60584 KB Output is correct
89 Correct 740 ms 61476 KB Output is correct