Submission #937081

# Submission time Handle Problem Language Result Execution time Memory
937081 2024-03-03T12:36:07 Z Pring Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
810 ms 90072 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
#define debug(x...) cout << '[' << #x << "] : ", dout(x)
void dout() { cout << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;

const int MXN = 100005;
int n, m;

struct DSU {
    int p[MXN];
    void init(int n) {
        fill(p + 1, p + n + 1, -1);
    }
    int find(int x) {
        return (p[x] < 0 ? x : p[x] = find(p[x]));
    }
    bool onion(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        p[y] += p[x];
        p[x] = y;
        return true;
    }
};

namespace DS {
    DSU dsu;
    int deg_in[MXN], deg_out[MXN];
    queue<pii> q;
    set<pii> out[MXN];
    unordered_map<int, vector<int>> M[MXN];
    set<int> S[MXN];
    int ansE = 0, ansG = 0;

    int tri(int x) {
        return x * (x - 1);
    }

    int SIZE(int X) {
        return -dsu.p[X];
    }

    void init(int n) {
        dsu.init(n);
    }

    void ADD_EDGE(int x, int X, int Y) {
        deg_in[Y]++;
        deg_out[X]++;
        out[X].insert(mp(x, Y));
        M[Y][X].push_back(x);
        S[x].insert(Y);
        ansE += SIZE(Y);
        // debug('+', x, Y, ansE);
    }

    void ERASE_EDGE(int x, int X, int Y) {
        deg_out[X]--;
        deg_in[Y]--;
        out[X].erase(mp(x, Y));
        S[x].erase(Y);
        ansE -= SIZE(Y);
        // debug('-', x, Y, ansE);
    }

    void MERGE(int X, int Y) {
        ansG -= tri(SIZE(X));
        ansG -= tri(SIZE(Y));
        ansG += tri(SIZE(X) + SIZE(Y));
        for (auto &i : M[X][Y]) ERASE_EDGE(i, Y, X);
        M[X].erase(Y);
        for (auto &i : M[Y][X]) ERASE_EDGE(i, X, Y);
        M[Y].erase(X);
        if (deg_in[X] + deg_out[X] >= deg_in[Y] + deg_out[Y]) swap(X, Y);
        // debug("MERGE", X, Y);
        {
            for (auto it = M[X].begin(); it != M[X].end(); it++) {
                for (auto &i : it -> sc) {
                    ERASE_EDGE(i, dsu.find(i), X);
                    q.push(mp(i, Y));
                }
            }
            M[X].clear();
        }
        {
            vector<pii> v;
            for (auto i : out[X]) v.push_back(i);
            for (auto i : v) {
                ERASE_EDGE(i.fs, X, i.sc);
                q.push(mp(i.fs, i.sc));
            }
            for (auto i : v) {
                M[i.sc][X].clear();
            }
            out[X].clear();
            // debug("CLEAR", X);
        }
        ansE -= SIZE(Y) * deg_in[Y];
        dsu.onion(X, Y);
        ansE += SIZE(Y) * deg_in[Y];
    }

    void TEST(int x, int Y) {
        if (S[x].find(Y) != S[x].end()) return;
        int X = dsu.find(x);
        auto it = M[X].find(Y);
        if (it == M[X].end() || it -> sc.empty()) {
            ADD_EDGE(x, X, Y);
            return;
        }
        MERGE(X, Y);
    }

    void clear_queue() {
        while (q.size()) {
            auto [x, y] = q.front();
            q.pop();
            if (dsu.find(x) == dsu.find(y)) continue;
            TEST(x, dsu.find(y));
        }
    }

    void COUT() {
        cout << endl;
        FOR(i, 1, n + 1) {
            cout << i << ": ";
            // for (auto j : S[i]) cout << j << ' ';
            for (auto it = M[i].begin(); it != M[i].end(); it++) {
                cout << '[' << it -> fs << ":";
                for (auto &j : it -> sc) cout << ' ' << j;
                cout << "] ";
            }
            cout << endl;
        }
        debug(ansE, ansG);
        cout << endl;
    }
}

void miku() {
    int a, b;
    cin >> n >> m;
    // cin >> n;
    DS::init(n);
    while (m--) {
        cin >> a >> b;
    // while (cin >> a >> b) {
        DS::q.push(mp(a, b));
        DS::clear_queue();
        // DS::COUT();
        cout << DS::ansE + DS::ansG << '\n';
        // for (auto &i : DS::out[5]) cout << '<' << i.fs << ' ' << i.sc << "> ";
        // cout << endl;
    }
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    // cin.exceptions(iostream::failbit);
    miku();
    return 0;
}

Compilation message

joitter2.cpp: In function 'void DS::COUT()':
joitter2.cpp:10:20: warning: statement has no effect [-Wunused-value]
   10 | #define debug(...) 39
      |                    ^~
joitter2.cpp:150:9: note: in expansion of macro 'debug'
  150 |         debug(ansE, ansG);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 4 ms 17036 KB Output is correct
3 Correct 7 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 5 ms 16996 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 5 ms 16988 KB Output is correct
8 Correct 5 ms 16988 KB Output is correct
9 Correct 4 ms 17108 KB Output is correct
10 Correct 4 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 4 ms 16808 KB Output is correct
14 Correct 5 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 5 ms 17040 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 5 ms 16988 KB Output is correct
21 Correct 4 ms 17052 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 5 ms 17076 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 17048 KB Output is correct
26 Correct 4 ms 17004 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 4 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 17080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 4 ms 17036 KB Output is correct
3 Correct 7 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 5 ms 16996 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 5 ms 16988 KB Output is correct
8 Correct 5 ms 16988 KB Output is correct
9 Correct 4 ms 17108 KB Output is correct
10 Correct 4 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 4 ms 16808 KB Output is correct
14 Correct 5 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 5 ms 17040 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 5 ms 16988 KB Output is correct
21 Correct 4 ms 17052 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 5 ms 17076 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 17048 KB Output is correct
26 Correct 4 ms 17004 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 4 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 17080 KB Output is correct
34 Correct 6 ms 17172 KB Output is correct
35 Correct 69 ms 23380 KB Output is correct
36 Correct 81 ms 26704 KB Output is correct
37 Correct 83 ms 26964 KB Output is correct
38 Correct 86 ms 26388 KB Output is correct
39 Correct 5 ms 17244 KB Output is correct
40 Correct 7 ms 17500 KB Output is correct
41 Correct 6 ms 17500 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 6 ms 17260 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 7 ms 17244 KB Output is correct
46 Correct 6 ms 17244 KB Output is correct
47 Correct 6 ms 17500 KB Output is correct
48 Correct 6 ms 17304 KB Output is correct
49 Correct 13 ms 18356 KB Output is correct
50 Correct 83 ms 26964 KB Output is correct
51 Correct 9 ms 17756 KB Output is correct
52 Correct 75 ms 24624 KB Output is correct
53 Correct 12 ms 18268 KB Output is correct
54 Correct 84 ms 26136 KB Output is correct
55 Correct 8 ms 18012 KB Output is correct
56 Correct 7 ms 18012 KB Output is correct
57 Correct 7 ms 18048 KB Output is correct
58 Correct 8 ms 18012 KB Output is correct
59 Correct 6 ms 17244 KB Output is correct
60 Correct 82 ms 22096 KB Output is correct
61 Correct 7 ms 17500 KB Output is correct
62 Correct 84 ms 26252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 4 ms 17036 KB Output is correct
3 Correct 7 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 5 ms 16996 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 5 ms 16988 KB Output is correct
8 Correct 5 ms 16988 KB Output is correct
9 Correct 4 ms 17108 KB Output is correct
10 Correct 4 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 4 ms 16808 KB Output is correct
14 Correct 5 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 5 ms 17040 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 5 ms 16988 KB Output is correct
21 Correct 4 ms 17052 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 5 ms 17076 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 17048 KB Output is correct
26 Correct 4 ms 17004 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 4 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 17080 KB Output is correct
34 Correct 6 ms 17172 KB Output is correct
35 Correct 69 ms 23380 KB Output is correct
36 Correct 81 ms 26704 KB Output is correct
37 Correct 83 ms 26964 KB Output is correct
38 Correct 86 ms 26388 KB Output is correct
39 Correct 5 ms 17244 KB Output is correct
40 Correct 7 ms 17500 KB Output is correct
41 Correct 6 ms 17500 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 6 ms 17260 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 7 ms 17244 KB Output is correct
46 Correct 6 ms 17244 KB Output is correct
47 Correct 6 ms 17500 KB Output is correct
48 Correct 6 ms 17304 KB Output is correct
49 Correct 13 ms 18356 KB Output is correct
50 Correct 83 ms 26964 KB Output is correct
51 Correct 9 ms 17756 KB Output is correct
52 Correct 75 ms 24624 KB Output is correct
53 Correct 12 ms 18268 KB Output is correct
54 Correct 84 ms 26136 KB Output is correct
55 Correct 8 ms 18012 KB Output is correct
56 Correct 7 ms 18012 KB Output is correct
57 Correct 7 ms 18048 KB Output is correct
58 Correct 8 ms 18012 KB Output is correct
59 Correct 6 ms 17244 KB Output is correct
60 Correct 82 ms 22096 KB Output is correct
61 Correct 7 ms 17500 KB Output is correct
62 Correct 84 ms 26252 KB Output is correct
63 Correct 450 ms 89800 KB Output is correct
64 Correct 469 ms 90072 KB Output is correct
65 Correct 432 ms 89888 KB Output is correct
66 Correct 101 ms 32852 KB Output is correct
67 Correct 268 ms 40572 KB Output is correct
68 Correct 126 ms 33176 KB Output is correct
69 Correct 227 ms 38648 KB Output is correct
70 Correct 116 ms 32928 KB Output is correct
71 Correct 129 ms 32852 KB Output is correct
72 Correct 269 ms 40016 KB Output is correct
73 Correct 315 ms 40208 KB Output is correct
74 Correct 810 ms 61012 KB Output is correct
75 Correct 407 ms 46768 KB Output is correct
76 Correct 666 ms 57188 KB Output is correct
77 Correct 617 ms 57196 KB Output is correct
78 Correct 173 ms 41116 KB Output is correct
79 Correct 282 ms 44884 KB Output is correct
80 Correct 144 ms 36452 KB Output is correct
81 Correct 195 ms 41160 KB Output is correct
82 Correct 576 ms 70192 KB Output is correct
83 Correct 634 ms 70144 KB Output is correct
84 Correct 446 ms 69972 KB Output is correct
85 Correct 393 ms 70032 KB Output is correct
86 Correct 128 ms 32148 KB Output is correct
87 Correct 129 ms 34372 KB Output is correct
88 Correct 266 ms 40020 KB Output is correct
89 Correct 581 ms 55952 KB Output is correct