답안 #720058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720058 2023-04-07T10:52:16 Z walterw 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
17 / 100
418 ms 53508 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;

int sz[100005], par[100005];

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

int ans = 0;
set<int> in[100005], out[100005];
queue<pair<int, int>> 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";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9616 KB Output is correct
3 Correct 5 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9732 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9668 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9716 KB Output is correct
14 Correct 6 ms 9716 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 8 ms 9736 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 8 ms 9728 KB Output is correct
26 Correct 5 ms 9684 KB Output is correct
27 Correct 5 ms 9720 KB Output is correct
28 Correct 6 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 6 ms 9764 KB Output is correct
31 Correct 6 ms 9684 KB Output is correct
32 Correct 6 ms 9720 KB Output is correct
33 Correct 6 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9616 KB Output is correct
3 Correct 5 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9732 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9668 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9716 KB Output is correct
14 Correct 6 ms 9716 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 8 ms 9736 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 8 ms 9728 KB Output is correct
26 Correct 5 ms 9684 KB Output is correct
27 Correct 5 ms 9720 KB Output is correct
28 Correct 6 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 6 ms 9764 KB Output is correct
31 Correct 6 ms 9684 KB Output is correct
32 Correct 6 ms 9720 KB Output is correct
33 Correct 6 ms 9728 KB Output is correct
34 Correct 9 ms 9856 KB Output is correct
35 Correct 113 ms 15208 KB Output is correct
36 Correct 118 ms 17628 KB Output is correct
37 Correct 133 ms 17540 KB Output is correct
38 Correct 118 ms 17352 KB Output is correct
39 Correct 7 ms 10196 KB Output is correct
40 Correct 10 ms 10936 KB Output is correct
41 Correct 12 ms 10884 KB Output is correct
42 Correct 8 ms 10168 KB Output is correct
43 Correct 9 ms 10240 KB Output is correct
44 Correct 8 ms 10244 KB Output is correct
45 Correct 8 ms 10196 KB Output is correct
46 Correct 8 ms 10196 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 10 ms 10628 KB Output is correct
49 Correct 16 ms 11128 KB Output is correct
50 Correct 118 ms 17688 KB Output is correct
51 Correct 16 ms 10568 KB Output is correct
52 Correct 114 ms 16452 KB Output is correct
53 Correct 14 ms 10964 KB Output is correct
54 Correct 125 ms 17020 KB Output is correct
55 Correct 9 ms 10580 KB Output is correct
56 Correct 9 ms 10628 KB Output is correct
57 Correct 9 ms 10580 KB Output is correct
58 Correct 9 ms 10624 KB Output is correct
59 Correct 12 ms 11092 KB Output is correct
60 Correct 103 ms 15944 KB Output is correct
61 Correct 12 ms 10580 KB Output is correct
62 Correct 135 ms 17268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9616 KB Output is correct
3 Correct 5 ms 9724 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9732 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9668 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9716 KB Output is correct
14 Correct 6 ms 9716 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 8 ms 9736 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 8 ms 9728 KB Output is correct
26 Correct 5 ms 9684 KB Output is correct
27 Correct 5 ms 9720 KB Output is correct
28 Correct 6 ms 9684 KB Output is correct
29 Correct 6 ms 9684 KB Output is correct
30 Correct 6 ms 9764 KB Output is correct
31 Correct 6 ms 9684 KB Output is correct
32 Correct 6 ms 9720 KB Output is correct
33 Correct 6 ms 9728 KB Output is correct
34 Correct 9 ms 9856 KB Output is correct
35 Correct 113 ms 15208 KB Output is correct
36 Correct 118 ms 17628 KB Output is correct
37 Correct 133 ms 17540 KB Output is correct
38 Correct 118 ms 17352 KB Output is correct
39 Correct 7 ms 10196 KB Output is correct
40 Correct 10 ms 10936 KB Output is correct
41 Correct 12 ms 10884 KB Output is correct
42 Correct 8 ms 10168 KB Output is correct
43 Correct 9 ms 10240 KB Output is correct
44 Correct 8 ms 10244 KB Output is correct
45 Correct 8 ms 10196 KB Output is correct
46 Correct 8 ms 10196 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 10 ms 10628 KB Output is correct
49 Correct 16 ms 11128 KB Output is correct
50 Correct 118 ms 17688 KB Output is correct
51 Correct 16 ms 10568 KB Output is correct
52 Correct 114 ms 16452 KB Output is correct
53 Correct 14 ms 10964 KB Output is correct
54 Correct 125 ms 17020 KB Output is correct
55 Correct 9 ms 10580 KB Output is correct
56 Correct 9 ms 10628 KB Output is correct
57 Correct 9 ms 10580 KB Output is correct
58 Correct 9 ms 10624 KB Output is correct
59 Correct 12 ms 11092 KB Output is correct
60 Correct 103 ms 15944 KB Output is correct
61 Correct 12 ms 10580 KB Output is correct
62 Correct 135 ms 17268 KB Output is correct
63 Correct 418 ms 53508 KB Output is correct
64 Correct 391 ms 53484 KB Output is correct
65 Correct 412 ms 53508 KB Output is correct
66 Incorrect 267 ms 38376 KB Output isn't correct
67 Halted 0 ms 0 KB -