답안 #946770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946770 2024-03-15T04:31:43 Z phoenix0423 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1187 ms 117052 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long 
const int maxn = 2e5 + 5;
set<int> st[maxn], in[maxn], out[maxn], sgin[maxn], sgout[maxn];
int sz[maxn];
int par[maxn];
int root(int x){ return x == par[x] ? x : par[x] = root(par[x]);}
int cur = 0;
queue<pll> q;

void merge(int a, int b){
    int fa = root(a), fb = root(b);
    if(fa == fb) return;
    if(out[fb].find(fa) == out[fb].end()){
        if(sgin[fb].find(a) == sgin[fb].end()){
            cur += st[fb].size();
            sgin[fb].insert(a), sgout[a].insert(fb);
            in[fb].insert(fa), out[fa].insert(fb);
        }
        return;
    }
    cur -= 1LL * st[fa].size() * (st[fa].size() - 1);
    cur -= 1LL * st[fb].size() * (st[fb].size() - 1);
    in[fa].erase(fb), out[fb].erase(fa);
    if(st[fa].size() > st[fb].size()) swap(fa, fb);
    for(auto x : st[fa]){
        for(auto y : sgout[x]){
            cur -= st[y].size();
            sgin[y].erase(x);
            q.push({x, y});
        }
        sgout[x].clear();
    }
    for(auto x : sgin[fa]){
        cur -= st[fa].size();
        sgout[x].erase(fa);
        q.push({x, fa});
    }
    sgin[fa].clear();
    for(auto x : out[fa]) in[x].erase(fa);
    for(auto x : in[fa]) out[x].erase(fa);
    for(auto x : st[fa]) st[fb].insert(x);
    par[fa] = fb;
    cur += 1LL * st[fb].size() * (st[fb].size() - 1);
    cur += 1LL * sgin[fb].size() * st[fa].size();
    st[fa].clear();
}

signed main(void){
    fastio;
    int n, m;
    cin>>n>>m;
    for(int i = 0; i < n; i++) par[i] = i;
    for(int i = 0; i < n; i++) st[i].insert(i);
    for(int i = 0; i < m; i++){
        int a, b;
        cin>>a>>b;
        a--, b--;
        q.push({a, b});
        while(!q.empty()){
            auto [a, b] = q.front(); q.pop();
            merge(a, b);
        }
        cout<<cur<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 48220 KB Output is correct
2 Correct 10 ms 48220 KB Output is correct
3 Correct 10 ms 48288 KB Output is correct
4 Correct 10 ms 48220 KB Output is correct
5 Correct 11 ms 48476 KB Output is correct
6 Correct 11 ms 48476 KB Output is correct
7 Correct 10 ms 48220 KB Output is correct
8 Correct 11 ms 48220 KB Output is correct
9 Correct 10 ms 48396 KB Output is correct
10 Correct 10 ms 48220 KB Output is correct
11 Correct 10 ms 48220 KB Output is correct
12 Correct 13 ms 48292 KB Output is correct
13 Correct 11 ms 48336 KB Output is correct
14 Correct 11 ms 48220 KB Output is correct
15 Correct 12 ms 48220 KB Output is correct
16 Correct 10 ms 48220 KB Output is correct
17 Correct 11 ms 48344 KB Output is correct
18 Correct 11 ms 48248 KB Output is correct
19 Correct 11 ms 48248 KB Output is correct
20 Correct 10 ms 48220 KB Output is correct
21 Correct 20 ms 48216 KB Output is correct
22 Correct 11 ms 48220 KB Output is correct
23 Correct 11 ms 48220 KB Output is correct
24 Correct 13 ms 48220 KB Output is correct
25 Correct 14 ms 48220 KB Output is correct
26 Correct 10 ms 48216 KB Output is correct
27 Correct 10 ms 48220 KB Output is correct
28 Correct 11 ms 48248 KB Output is correct
29 Correct 11 ms 48220 KB Output is correct
30 Correct 15 ms 48324 KB Output is correct
31 Correct 15 ms 48220 KB Output is correct
32 Correct 12 ms 48220 KB Output is correct
33 Correct 11 ms 48220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 48220 KB Output is correct
2 Correct 10 ms 48220 KB Output is correct
3 Correct 10 ms 48288 KB Output is correct
4 Correct 10 ms 48220 KB Output is correct
5 Correct 11 ms 48476 KB Output is correct
6 Correct 11 ms 48476 KB Output is correct
7 Correct 10 ms 48220 KB Output is correct
8 Correct 11 ms 48220 KB Output is correct
9 Correct 10 ms 48396 KB Output is correct
10 Correct 10 ms 48220 KB Output is correct
11 Correct 10 ms 48220 KB Output is correct
12 Correct 13 ms 48292 KB Output is correct
13 Correct 11 ms 48336 KB Output is correct
14 Correct 11 ms 48220 KB Output is correct
15 Correct 12 ms 48220 KB Output is correct
16 Correct 10 ms 48220 KB Output is correct
17 Correct 11 ms 48344 KB Output is correct
18 Correct 11 ms 48248 KB Output is correct
19 Correct 11 ms 48248 KB Output is correct
20 Correct 10 ms 48220 KB Output is correct
21 Correct 20 ms 48216 KB Output is correct
22 Correct 11 ms 48220 KB Output is correct
23 Correct 11 ms 48220 KB Output is correct
24 Correct 13 ms 48220 KB Output is correct
25 Correct 14 ms 48220 KB Output is correct
26 Correct 10 ms 48216 KB Output is correct
27 Correct 10 ms 48220 KB Output is correct
28 Correct 11 ms 48248 KB Output is correct
29 Correct 11 ms 48220 KB Output is correct
30 Correct 15 ms 48324 KB Output is correct
31 Correct 15 ms 48220 KB Output is correct
32 Correct 12 ms 48220 KB Output is correct
33 Correct 11 ms 48220 KB Output is correct
34 Correct 12 ms 48472 KB Output is correct
35 Correct 79 ms 54616 KB Output is correct
36 Correct 111 ms 58012 KB Output is correct
37 Correct 105 ms 58252 KB Output is correct
38 Correct 99 ms 57632 KB Output is correct
39 Correct 12 ms 48472 KB Output is correct
40 Correct 13 ms 48472 KB Output is correct
41 Correct 13 ms 48476 KB Output is correct
42 Correct 13 ms 48472 KB Output is correct
43 Correct 13 ms 48476 KB Output is correct
44 Correct 13 ms 48476 KB Output is correct
45 Correct 13 ms 48472 KB Output is correct
46 Correct 12 ms 48476 KB Output is correct
47 Correct 13 ms 48476 KB Output is correct
48 Correct 14 ms 48476 KB Output is correct
49 Correct 23 ms 49500 KB Output is correct
50 Correct 97 ms 58204 KB Output is correct
51 Correct 16 ms 48988 KB Output is correct
52 Correct 85 ms 55916 KB Output is correct
53 Correct 19 ms 49496 KB Output is correct
54 Correct 94 ms 56916 KB Output is correct
55 Correct 18 ms 49240 KB Output is correct
56 Correct 15 ms 49272 KB Output is correct
57 Correct 17 ms 49240 KB Output is correct
58 Correct 16 ms 49188 KB Output is correct
59 Correct 12 ms 48728 KB Output is correct
60 Correct 72 ms 53460 KB Output is correct
61 Correct 14 ms 48732 KB Output is correct
62 Correct 91 ms 57468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 48220 KB Output is correct
2 Correct 10 ms 48220 KB Output is correct
3 Correct 10 ms 48288 KB Output is correct
4 Correct 10 ms 48220 KB Output is correct
5 Correct 11 ms 48476 KB Output is correct
6 Correct 11 ms 48476 KB Output is correct
7 Correct 10 ms 48220 KB Output is correct
8 Correct 11 ms 48220 KB Output is correct
9 Correct 10 ms 48396 KB Output is correct
10 Correct 10 ms 48220 KB Output is correct
11 Correct 10 ms 48220 KB Output is correct
12 Correct 13 ms 48292 KB Output is correct
13 Correct 11 ms 48336 KB Output is correct
14 Correct 11 ms 48220 KB Output is correct
15 Correct 12 ms 48220 KB Output is correct
16 Correct 10 ms 48220 KB Output is correct
17 Correct 11 ms 48344 KB Output is correct
18 Correct 11 ms 48248 KB Output is correct
19 Correct 11 ms 48248 KB Output is correct
20 Correct 10 ms 48220 KB Output is correct
21 Correct 20 ms 48216 KB Output is correct
22 Correct 11 ms 48220 KB Output is correct
23 Correct 11 ms 48220 KB Output is correct
24 Correct 13 ms 48220 KB Output is correct
25 Correct 14 ms 48220 KB Output is correct
26 Correct 10 ms 48216 KB Output is correct
27 Correct 10 ms 48220 KB Output is correct
28 Correct 11 ms 48248 KB Output is correct
29 Correct 11 ms 48220 KB Output is correct
30 Correct 15 ms 48324 KB Output is correct
31 Correct 15 ms 48220 KB Output is correct
32 Correct 12 ms 48220 KB Output is correct
33 Correct 11 ms 48220 KB Output is correct
34 Correct 12 ms 48472 KB Output is correct
35 Correct 79 ms 54616 KB Output is correct
36 Correct 111 ms 58012 KB Output is correct
37 Correct 105 ms 58252 KB Output is correct
38 Correct 99 ms 57632 KB Output is correct
39 Correct 12 ms 48472 KB Output is correct
40 Correct 13 ms 48472 KB Output is correct
41 Correct 13 ms 48476 KB Output is correct
42 Correct 13 ms 48472 KB Output is correct
43 Correct 13 ms 48476 KB Output is correct
44 Correct 13 ms 48476 KB Output is correct
45 Correct 13 ms 48472 KB Output is correct
46 Correct 12 ms 48476 KB Output is correct
47 Correct 13 ms 48476 KB Output is correct
48 Correct 14 ms 48476 KB Output is correct
49 Correct 23 ms 49500 KB Output is correct
50 Correct 97 ms 58204 KB Output is correct
51 Correct 16 ms 48988 KB Output is correct
52 Correct 85 ms 55916 KB Output is correct
53 Correct 19 ms 49496 KB Output is correct
54 Correct 94 ms 56916 KB Output is correct
55 Correct 18 ms 49240 KB Output is correct
56 Correct 15 ms 49272 KB Output is correct
57 Correct 17 ms 49240 KB Output is correct
58 Correct 16 ms 49188 KB Output is correct
59 Correct 12 ms 48728 KB Output is correct
60 Correct 72 ms 53460 KB Output is correct
61 Correct 14 ms 48732 KB Output is correct
62 Correct 91 ms 57468 KB Output is correct
63 Correct 522 ms 117052 KB Output is correct
64 Correct 512 ms 116904 KB Output is correct
65 Correct 545 ms 117036 KB Output is correct
66 Correct 139 ms 59532 KB Output is correct
67 Correct 526 ms 68948 KB Output is correct
68 Correct 178 ms 59816 KB Output is correct
69 Correct 409 ms 68928 KB Output is correct
70 Correct 180 ms 59492 KB Output is correct
71 Correct 138 ms 59416 KB Output is correct
72 Correct 460 ms 69748 KB Output is correct
73 Correct 471 ms 69564 KB Output is correct
74 Correct 1187 ms 93764 KB Output is correct
75 Correct 531 ms 76880 KB Output is correct
76 Correct 869 ms 86624 KB Output is correct
77 Correct 908 ms 86544 KB Output is correct
78 Correct 257 ms 71196 KB Output is correct
79 Correct 473 ms 74924 KB Output is correct
80 Correct 304 ms 71344 KB Output is correct
81 Correct 428 ms 74664 KB Output is correct
82 Correct 929 ms 97272 KB Output is correct
83 Correct 917 ms 96768 KB Output is correct
84 Correct 719 ms 96232 KB Output is correct
85 Correct 710 ms 96276 KB Output is correct
86 Correct 281 ms 60300 KB Output is correct
87 Correct 272 ms 62544 KB Output is correct
88 Correct 438 ms 69716 KB Output is correct
89 Correct 753 ms 85540 KB Output is correct