답안 #1074807

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1074807 2024-08-25T14:19:23 Z underwaterkillerwhale 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1921 ms 159104 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 3e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;

int n, m;
map<pii,int> cnt, dd;
queue<pii> pq;
set<int> inS[N], outS[N], in[N];
ll numin[N];
ll res = 0;

struct Disjoin_set {
    ll lab[N], sz[N];

    void init (int n) {
        rep (i, 1, n) lab[i] = i, sz[i] = 1;
    }

    int Find (int u) {
        return u == lab[u] ? u : lab[u] = Find(lab[u]);
    }

    void add_edge (int u, int v) {
        int uu = Find(u), vv = Find(v);
        if (uu == vv) return;
        if (dd[{u, vv}] == 0) {
            dd[{u, vv}] = 1;
            cnt[{uu, vv}]++;
            numin[vv]++;
            res += sz[vv];
        }
        if (inS[uu].find(vv) != inS[uu].end()) pq.push({uu, vv});
        inS[vv].insert(uu);
        outS[uu].insert(vv);
        in[vv].insert(u);
    }

    void Join (int u, int v) {
        u = Find(u), v = Find(v);
        if (u == v) return;
        if (SZ(in[u]) + SZ(outS[u]) + SZ(inS[u]) < SZ(in[v]) + SZ(outS[v]) + SZ(inS[v])) swap(u, v);
//        cout << u <<" "<<v<<" hihi\n";
        numin[u] -= cnt[{v, u}];
        numin[v] -= cnt[{u, v}];
        res -= sz[u] * (sz[u] - 1) + sz[v] * (sz[v] - 1);
        res -= sz[v] * cnt[{u, v}] + sz[u] * cnt[{v, u}];
//        cout << res<<" "<<cnt[{3, 1}]<<" "<<cnt[{1, 3}]<<"\n";
        res += (sz[u] + sz[v]) * (sz[u] + sz[v] - 1);
        res += numin[v] * sz[u] + numin[u] * sz[v];
        numin[u] += numin[v];
        iter (&id, in[v]) {
            if (Find(id) == v || Find(id) == u) continue;
            if (in[u].find(id) != in[u].end()) {
                res -= sz[u] + sz[v];
                numin[u] --;
            }
            if (dd[{id, u}] == 0) {
                dd[{id, u}] = 1;
                cnt[{Find(id), u}]++;
            }
        }
        iter (&id, inS[v]) {
            if (id == u) continue;
            if (outS[u].find(id) != outS[u].end()) pq.push({id, u});
            outS[id].erase(v), outS[id].insert(u);
            inS[u].insert(id);
        }
        iter (&id, outS[v]) {
            if (id == u) continue;
            if (inS[u].find(id) != inS[u].end()) pq.push({id, u});
            cnt[{u, id}] += cnt[{v, id}];
            inS[id].erase(v), inS[id].insert(u);
            outS[u].insert(id);
        }
        iter (&id, in[v]) {
            if (Find(id) == v || Find(id) == u) continue;
            dd[{id, u}] = 1;
            in[u].insert(id);
        }
        set<int> ().swap(inS[v]);
        set<int> ().swap(outS[v]);

        sz[u] += sz[v];
        lab[v] = u;
    }

    void simulate () {
        while (!pq.empty()) {
            pii u = pq.front();
            pq.pop();
            Join(u.fs, u.se);
        }
    }

}DSU;

void solution () {
    cin >> n >> m;
    DSU.init(n);
    rep (i, 1, m) {
        int u, v;
        cin >> u >> v;
        DSU.add_edge(u, v);
        DSU.simulate();
        cout << res <<"\n";
    }
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +4
mình có muốn dùng mảng này không?
4 5
1 3
4 1
1 2
3 2
2 1
return->break
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 42588 KB Output is correct
2 Correct 11 ms 42588 KB Output is correct
3 Correct 14 ms 42540 KB Output is correct
4 Correct 11 ms 42756 KB Output is correct
5 Correct 13 ms 42588 KB Output is correct
6 Correct 11 ms 42588 KB Output is correct
7 Correct 14 ms 42584 KB Output is correct
8 Correct 14 ms 42588 KB Output is correct
9 Correct 14 ms 42780 KB Output is correct
10 Correct 13 ms 42616 KB Output is correct
11 Correct 13 ms 42588 KB Output is correct
12 Correct 19 ms 42588 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 11 ms 42584 KB Output is correct
15 Correct 11 ms 42588 KB Output is correct
16 Correct 12 ms 42700 KB Output is correct
17 Correct 11 ms 42588 KB Output is correct
18 Correct 15 ms 42784 KB Output is correct
19 Correct 12 ms 42588 KB Output is correct
20 Correct 11 ms 42588 KB Output is correct
21 Correct 12 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 16 ms 42588 KB Output is correct
24 Correct 11 ms 42588 KB Output is correct
25 Correct 11 ms 42588 KB Output is correct
26 Correct 11 ms 42588 KB Output is correct
27 Correct 12 ms 42588 KB Output is correct
28 Correct 11 ms 42724 KB Output is correct
29 Correct 13 ms 42584 KB Output is correct
30 Correct 11 ms 42588 KB Output is correct
31 Correct 12 ms 42756 KB Output is correct
32 Correct 14 ms 42588 KB Output is correct
33 Correct 12 ms 42684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 42588 KB Output is correct
2 Correct 11 ms 42588 KB Output is correct
3 Correct 14 ms 42540 KB Output is correct
4 Correct 11 ms 42756 KB Output is correct
5 Correct 13 ms 42588 KB Output is correct
6 Correct 11 ms 42588 KB Output is correct
7 Correct 14 ms 42584 KB Output is correct
8 Correct 14 ms 42588 KB Output is correct
9 Correct 14 ms 42780 KB Output is correct
10 Correct 13 ms 42616 KB Output is correct
11 Correct 13 ms 42588 KB Output is correct
12 Correct 19 ms 42588 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 11 ms 42584 KB Output is correct
15 Correct 11 ms 42588 KB Output is correct
16 Correct 12 ms 42700 KB Output is correct
17 Correct 11 ms 42588 KB Output is correct
18 Correct 15 ms 42784 KB Output is correct
19 Correct 12 ms 42588 KB Output is correct
20 Correct 11 ms 42588 KB Output is correct
21 Correct 12 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 16 ms 42588 KB Output is correct
24 Correct 11 ms 42588 KB Output is correct
25 Correct 11 ms 42588 KB Output is correct
26 Correct 11 ms 42588 KB Output is correct
27 Correct 12 ms 42588 KB Output is correct
28 Correct 11 ms 42724 KB Output is correct
29 Correct 13 ms 42584 KB Output is correct
30 Correct 11 ms 42588 KB Output is correct
31 Correct 12 ms 42756 KB Output is correct
32 Correct 14 ms 42588 KB Output is correct
33 Correct 12 ms 42684 KB Output is correct
34 Correct 14 ms 42952 KB Output is correct
35 Correct 71 ms 48640 KB Output is correct
36 Correct 105 ms 53484 KB Output is correct
37 Correct 108 ms 53588 KB Output is correct
38 Correct 110 ms 52820 KB Output is correct
39 Correct 16 ms 43608 KB Output is correct
40 Correct 21 ms 44380 KB Output is correct
41 Correct 21 ms 44428 KB Output is correct
42 Correct 15 ms 43612 KB Output is correct
43 Correct 17 ms 43516 KB Output is correct
44 Correct 16 ms 43608 KB Output is correct
45 Correct 19 ms 43612 KB Output is correct
46 Correct 17 ms 43512 KB Output is correct
47 Correct 19 ms 44124 KB Output is correct
48 Correct 22 ms 44124 KB Output is correct
49 Correct 28 ms 45144 KB Output is correct
50 Correct 110 ms 53840 KB Output is correct
51 Correct 20 ms 44120 KB Output is correct
52 Correct 83 ms 50632 KB Output is correct
53 Correct 25 ms 44880 KB Output is correct
54 Correct 91 ms 52048 KB Output is correct
55 Correct 20 ms 44120 KB Output is correct
56 Correct 19 ms 44288 KB Output is correct
57 Correct 19 ms 44124 KB Output is correct
58 Correct 21 ms 44144 KB Output is correct
59 Correct 20 ms 44584 KB Output is correct
60 Correct 63 ms 48424 KB Output is correct
61 Correct 20 ms 44120 KB Output is correct
62 Correct 95 ms 52564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 42588 KB Output is correct
2 Correct 11 ms 42588 KB Output is correct
3 Correct 14 ms 42540 KB Output is correct
4 Correct 11 ms 42756 KB Output is correct
5 Correct 13 ms 42588 KB Output is correct
6 Correct 11 ms 42588 KB Output is correct
7 Correct 14 ms 42584 KB Output is correct
8 Correct 14 ms 42588 KB Output is correct
9 Correct 14 ms 42780 KB Output is correct
10 Correct 13 ms 42616 KB Output is correct
11 Correct 13 ms 42588 KB Output is correct
12 Correct 19 ms 42588 KB Output is correct
13 Correct 12 ms 42736 KB Output is correct
14 Correct 11 ms 42584 KB Output is correct
15 Correct 11 ms 42588 KB Output is correct
16 Correct 12 ms 42700 KB Output is correct
17 Correct 11 ms 42588 KB Output is correct
18 Correct 15 ms 42784 KB Output is correct
19 Correct 12 ms 42588 KB Output is correct
20 Correct 11 ms 42588 KB Output is correct
21 Correct 12 ms 42844 KB Output is correct
22 Correct 12 ms 42588 KB Output is correct
23 Correct 16 ms 42588 KB Output is correct
24 Correct 11 ms 42588 KB Output is correct
25 Correct 11 ms 42588 KB Output is correct
26 Correct 11 ms 42588 KB Output is correct
27 Correct 12 ms 42588 KB Output is correct
28 Correct 11 ms 42724 KB Output is correct
29 Correct 13 ms 42584 KB Output is correct
30 Correct 11 ms 42588 KB Output is correct
31 Correct 12 ms 42756 KB Output is correct
32 Correct 14 ms 42588 KB Output is correct
33 Correct 12 ms 42684 KB Output is correct
34 Correct 14 ms 42952 KB Output is correct
35 Correct 71 ms 48640 KB Output is correct
36 Correct 105 ms 53484 KB Output is correct
37 Correct 108 ms 53588 KB Output is correct
38 Correct 110 ms 52820 KB Output is correct
39 Correct 16 ms 43608 KB Output is correct
40 Correct 21 ms 44380 KB Output is correct
41 Correct 21 ms 44428 KB Output is correct
42 Correct 15 ms 43612 KB Output is correct
43 Correct 17 ms 43516 KB Output is correct
44 Correct 16 ms 43608 KB Output is correct
45 Correct 19 ms 43612 KB Output is correct
46 Correct 17 ms 43512 KB Output is correct
47 Correct 19 ms 44124 KB Output is correct
48 Correct 22 ms 44124 KB Output is correct
49 Correct 28 ms 45144 KB Output is correct
50 Correct 110 ms 53840 KB Output is correct
51 Correct 20 ms 44120 KB Output is correct
52 Correct 83 ms 50632 KB Output is correct
53 Correct 25 ms 44880 KB Output is correct
54 Correct 91 ms 52048 KB Output is correct
55 Correct 20 ms 44120 KB Output is correct
56 Correct 19 ms 44288 KB Output is correct
57 Correct 19 ms 44124 KB Output is correct
58 Correct 21 ms 44144 KB Output is correct
59 Correct 20 ms 44584 KB Output is correct
60 Correct 63 ms 48424 KB Output is correct
61 Correct 20 ms 44120 KB Output is correct
62 Correct 95 ms 52564 KB Output is correct
63 Correct 691 ms 128892 KB Output is correct
64 Correct 633 ms 128848 KB Output is correct
65 Correct 667 ms 128924 KB Output is correct
66 Correct 412 ms 92240 KB Output is correct
67 Correct 1332 ms 143076 KB Output is correct
68 Correct 416 ms 93520 KB Output is correct
69 Correct 468 ms 93152 KB Output is correct
70 Correct 484 ms 93012 KB Output is correct
71 Correct 492 ms 93084 KB Output is correct
72 Correct 953 ms 118084 KB Output is correct
73 Correct 1041 ms 120284 KB Output is correct
74 Correct 1921 ms 159104 KB Output is correct
75 Correct 787 ms 102224 KB Output is correct
76 Correct 1353 ms 127172 KB Output is correct
77 Correct 1437 ms 127548 KB Output is correct
78 Correct 409 ms 88656 KB Output is correct
79 Correct 712 ms 103372 KB Output is correct
80 Correct 359 ms 85548 KB Output is correct
81 Correct 615 ms 96592 KB Output is correct
82 Correct 1174 ms 119728 KB Output is correct
83 Correct 1234 ms 120144 KB Output is correct
84 Correct 1076 ms 119528 KB Output is correct
85 Correct 1055 ms 119752 KB Output is correct
86 Correct 1343 ms 150132 KB Output is correct
87 Correct 1392 ms 152360 KB Output is correct
88 Correct 1077 ms 119376 KB Output is correct
89 Correct 1393 ms 124528 KB Output is correct