답안 #1074812

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1074812 2024-08-25T14:21:45 Z underwaterkillerwhale 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1974 ms 127756 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 = 1e5 + 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 3 ms 16732 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 2 ms 16728 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16808 KB Output is correct
10 Correct 3 ms 16764 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 3 ms 16732 KB Output is correct
14 Correct 4 ms 16732 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16728 KB Output is correct
19 Correct 3 ms 16984 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16772 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 16728 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 4 ms 16732 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 2 ms 16728 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16808 KB Output is correct
10 Correct 3 ms 16764 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 3 ms 16732 KB Output is correct
14 Correct 4 ms 16732 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16728 KB Output is correct
19 Correct 3 ms 16984 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16772 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 16728 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 4 ms 16732 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
34 Correct 5 ms 16920 KB Output is correct
35 Correct 61 ms 21368 KB Output is correct
36 Correct 100 ms 25680 KB Output is correct
37 Correct 99 ms 25940 KB Output is correct
38 Correct 93 ms 25168 KB Output is correct
39 Correct 7 ms 17500 KB Output is correct
40 Correct 15 ms 18344 KB Output is correct
41 Correct 12 ms 18264 KB Output is correct
42 Correct 7 ms 17500 KB Output is correct
43 Correct 8 ms 17500 KB Output is correct
44 Correct 7 ms 17500 KB Output is correct
45 Correct 7 ms 17472 KB Output is correct
46 Correct 8 ms 17500 KB Output is correct
47 Correct 12 ms 18012 KB Output is correct
48 Correct 12 ms 18008 KB Output is correct
49 Correct 21 ms 18996 KB Output is correct
50 Correct 102 ms 26192 KB Output is correct
51 Correct 13 ms 18012 KB Output is correct
52 Correct 74 ms 22824 KB Output is correct
53 Correct 19 ms 18780 KB Output is correct
54 Correct 90 ms 24364 KB Output is correct
55 Correct 11 ms 18012 KB Output is correct
56 Correct 10 ms 18264 KB Output is correct
57 Correct 10 ms 18012 KB Output is correct
58 Correct 11 ms 18112 KB Output is correct
59 Correct 13 ms 18268 KB Output is correct
60 Correct 55 ms 20816 KB Output is correct
61 Correct 13 ms 18008 KB Output is correct
62 Correct 93 ms 24912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 2 ms 16728 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16808 KB Output is correct
10 Correct 3 ms 16764 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 3 ms 16732 KB Output is correct
14 Correct 4 ms 16732 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16728 KB Output is correct
19 Correct 3 ms 16984 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16772 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 16728 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 4 ms 16732 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
34 Correct 5 ms 16920 KB Output is correct
35 Correct 61 ms 21368 KB Output is correct
36 Correct 100 ms 25680 KB Output is correct
37 Correct 99 ms 25940 KB Output is correct
38 Correct 93 ms 25168 KB Output is correct
39 Correct 7 ms 17500 KB Output is correct
40 Correct 15 ms 18344 KB Output is correct
41 Correct 12 ms 18264 KB Output is correct
42 Correct 7 ms 17500 KB Output is correct
43 Correct 8 ms 17500 KB Output is correct
44 Correct 7 ms 17500 KB Output is correct
45 Correct 7 ms 17472 KB Output is correct
46 Correct 8 ms 17500 KB Output is correct
47 Correct 12 ms 18012 KB Output is correct
48 Correct 12 ms 18008 KB Output is correct
49 Correct 21 ms 18996 KB Output is correct
50 Correct 102 ms 26192 KB Output is correct
51 Correct 13 ms 18012 KB Output is correct
52 Correct 74 ms 22824 KB Output is correct
53 Correct 19 ms 18780 KB Output is correct
54 Correct 90 ms 24364 KB Output is correct
55 Correct 11 ms 18012 KB Output is correct
56 Correct 10 ms 18264 KB Output is correct
57 Correct 10 ms 18012 KB Output is correct
58 Correct 11 ms 18112 KB Output is correct
59 Correct 13 ms 18268 KB Output is correct
60 Correct 55 ms 20816 KB Output is correct
61 Correct 13 ms 18008 KB Output is correct
62 Correct 93 ms 24912 KB Output is correct
63 Correct 666 ms 98640 KB Output is correct
64 Correct 663 ms 98644 KB Output is correct
65 Correct 689 ms 98644 KB Output is correct
66 Correct 407 ms 62728 KB Output is correct
67 Correct 1358 ms 112824 KB Output is correct
68 Correct 393 ms 62800 KB Output is correct
69 Correct 518 ms 62804 KB Output is correct
70 Correct 420 ms 62712 KB Output is correct
71 Correct 449 ms 62800 KB Output is correct
72 Correct 963 ms 87632 KB Output is correct
73 Correct 974 ms 90420 KB Output is correct
74 Correct 1974 ms 127756 KB Output is correct
75 Correct 748 ms 71028 KB Output is correct
76 Correct 1305 ms 95580 KB Output is correct
77 Correct 1243 ms 95876 KB Output is correct
78 Correct 344 ms 58444 KB Output is correct
79 Correct 614 ms 71760 KB Output is correct
80 Correct 299 ms 55380 KB Output is correct
81 Correct 515 ms 65060 KB Output is correct
82 Correct 1058 ms 89172 KB Output is correct
83 Correct 1065 ms 89680 KB Output is correct
84 Correct 1002 ms 88964 KB Output is correct
85 Correct 995 ms 89168 KB Output is correct
86 Correct 1377 ms 119632 KB Output is correct
87 Correct 1341 ms 120904 KB Output is correct
88 Correct 998 ms 88824 KB Output is correct
89 Correct 1233 ms 92980 KB Output is correct