답안 #1000233

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1000233 2024-06-17T06:51:35 Z Unforgettablepl 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
355 ms 62544 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long

struct UFDS{
    vector<int> p,siz;
    vector<set<pair<int,int>>> incoming,outgoing;
    UFDS(int n):p(n+1),siz(n+1,1),incoming(n+1),outgoing(n+1){iota(p.begin(),p.end(),0);}
    int findRoot(int x){
        return p[x]==x ? x : p[x]=findRoot(p[x]);
    }
    int merge(int x,int y){
        if(findRoot(x)==findRoot(y))return 0;
        x = findRoot(x);
        y = findRoot(y);
        int ans = 0;
        int root_x = x;
        while(true){
            auto iter = incoming[root_x].lower_bound({y,0ll});
            if(iter==incoming[root_x].end() or iter->first!=y)break;
            outgoing[y].erase({root_x,iter->second});
            incoming[root_x].erase(iter);
            ans-=siz[root_x];
        }
        while(true){
            auto iter = incoming[y].lower_bound({root_x,0ll});
            if(iter==incoming[y].end() or iter->first!=root_x)break;
            outgoing[root_x].erase({y,iter->second});
            incoming[y].erase(iter);
            ans-=siz[y];
        }
        ans-=siz[root_x]*incoming[root_x].size();
        ans-=siz[y]*incoming[y].size();
        ans-=siz[root_x]*(siz[root_x]-1ll);
        ans-=siz[y]*(siz[y]-1ll);
        if(incoming[root_x].size()+outgoing[root_x].size()<incoming[y].size()+outgoing[y].size())swap(root_x,y);
        p[y] = root_x;
        siz[root_x]+=siz[y];
        vector<int> qu;
        for(auto[a,b]:incoming[y]){
            incoming[root_x].insert({a,b});
            auto iter = outgoing[root_x].lower_bound({a,0});
            if(iter!=outgoing[root_x].end() and iter->first==a)qu.emplace_back(a);
            outgoing[a].erase({y,b});
            outgoing[a].insert({root_x,b});
        }
        for(auto[a,b]:outgoing[y]){
            outgoing[root_x].insert({a,b});
            auto iter = incoming[root_x].lower_bound({a,0});
            if(iter!=incoming[root_x].end() and iter->first==a)qu.emplace_back(a);
            incoming[a].erase({y,b});
            incoming[a].insert({root_x,b});
        }
        ans+=siz[root_x]*incoming[root_x].size();
        ans+=siz[root_x]*(siz[root_x]-1);
        for(int&i:qu)ans+=merge(root_x,i);
        return ans;
    }
    int unite(int x,int y){
        int root_x = findRoot(x);
        y = findRoot(y);
        if(y==root_x)return 0;
        if(outgoing[root_x].count({y,x}))return 0;
        auto iter = incoming[root_x].lower_bound({y,0ll});
        if(iter!=incoming[root_x].end() and iter->first==y){
            return merge(root_x,y);
        }
        outgoing[root_x].insert({y,x});
        incoming[y].insert({root_x,x});
        return siz[y];
    }
};

UFDS dsu(100000);

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,m;
    cin >> n >> m;
    int ans = 0;
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        ans+=dsu.unite(a,b);
        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11352 KB Output is correct
2 Correct 4 ms 11356 KB Output is correct
3 Correct 4 ms 11356 KB Output is correct
4 Correct 4 ms 11396 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11352 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 5 ms 11356 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11352 KB Output is correct
12 Correct 4 ms 11296 KB Output is correct
13 Correct 5 ms 11608 KB Output is correct
14 Correct 5 ms 11356 KB Output is correct
15 Correct 4 ms 11356 KB Output is correct
16 Correct 4 ms 11352 KB Output is correct
17 Correct 5 ms 11352 KB Output is correct
18 Correct 4 ms 11356 KB Output is correct
19 Correct 5 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 5 ms 11356 KB Output is correct
22 Correct 5 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 5 ms 11388 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11428 KB Output is correct
28 Correct 5 ms 11356 KB Output is correct
29 Correct 4 ms 11352 KB Output is correct
30 Correct 4 ms 11244 KB Output is correct
31 Correct 5 ms 11352 KB Output is correct
32 Correct 4 ms 11356 KB Output is correct
33 Correct 7 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11352 KB Output is correct
2 Correct 4 ms 11356 KB Output is correct
3 Correct 4 ms 11356 KB Output is correct
4 Correct 4 ms 11396 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11352 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 5 ms 11356 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11352 KB Output is correct
12 Correct 4 ms 11296 KB Output is correct
13 Correct 5 ms 11608 KB Output is correct
14 Correct 5 ms 11356 KB Output is correct
15 Correct 4 ms 11356 KB Output is correct
16 Correct 4 ms 11352 KB Output is correct
17 Correct 5 ms 11352 KB Output is correct
18 Correct 4 ms 11356 KB Output is correct
19 Correct 5 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 5 ms 11356 KB Output is correct
22 Correct 5 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 5 ms 11388 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11428 KB Output is correct
28 Correct 5 ms 11356 KB Output is correct
29 Correct 4 ms 11352 KB Output is correct
30 Correct 4 ms 11244 KB Output is correct
31 Correct 5 ms 11352 KB Output is correct
32 Correct 4 ms 11356 KB Output is correct
33 Correct 7 ms 11356 KB Output is correct
34 Correct 8 ms 11356 KB Output is correct
35 Correct 52 ms 17008 KB Output is correct
36 Correct 78 ms 19616 KB Output is correct
37 Correct 70 ms 19796 KB Output is correct
38 Correct 73 ms 19536 KB Output is correct
39 Correct 5 ms 11608 KB Output is correct
40 Correct 6 ms 11612 KB Output is correct
41 Correct 5 ms 11612 KB Output is correct
42 Correct 5 ms 11356 KB Output is correct
43 Correct 6 ms 11612 KB Output is correct
44 Correct 6 ms 11440 KB Output is correct
45 Correct 6 ms 11356 KB Output is correct
46 Correct 6 ms 11356 KB Output is correct
47 Correct 6 ms 11448 KB Output is correct
48 Correct 6 ms 11612 KB Output is correct
49 Correct 11 ms 12380 KB Output is correct
50 Correct 70 ms 19928 KB Output is correct
51 Correct 8 ms 11864 KB Output is correct
52 Correct 60 ms 18196 KB Output is correct
53 Correct 11 ms 12632 KB Output is correct
54 Correct 64 ms 19100 KB Output is correct
55 Correct 8 ms 11868 KB Output is correct
56 Correct 7 ms 11868 KB Output is correct
57 Correct 7 ms 12380 KB Output is correct
58 Correct 7 ms 12468 KB Output is correct
59 Correct 5 ms 11356 KB Output is correct
60 Correct 48 ms 15948 KB Output is correct
61 Correct 6 ms 11612 KB Output is correct
62 Correct 67 ms 19284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11352 KB Output is correct
2 Correct 4 ms 11356 KB Output is correct
3 Correct 4 ms 11356 KB Output is correct
4 Correct 4 ms 11396 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11352 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 5 ms 11356 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11352 KB Output is correct
12 Correct 4 ms 11296 KB Output is correct
13 Correct 5 ms 11608 KB Output is correct
14 Correct 5 ms 11356 KB Output is correct
15 Correct 4 ms 11356 KB Output is correct
16 Correct 4 ms 11352 KB Output is correct
17 Correct 5 ms 11352 KB Output is correct
18 Correct 4 ms 11356 KB Output is correct
19 Correct 5 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 5 ms 11356 KB Output is correct
22 Correct 5 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 5 ms 11388 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11428 KB Output is correct
28 Correct 5 ms 11356 KB Output is correct
29 Correct 4 ms 11352 KB Output is correct
30 Correct 4 ms 11244 KB Output is correct
31 Correct 5 ms 11352 KB Output is correct
32 Correct 4 ms 11356 KB Output is correct
33 Correct 7 ms 11356 KB Output is correct
34 Correct 8 ms 11356 KB Output is correct
35 Correct 52 ms 17008 KB Output is correct
36 Correct 78 ms 19616 KB Output is correct
37 Correct 70 ms 19796 KB Output is correct
38 Correct 73 ms 19536 KB Output is correct
39 Correct 5 ms 11608 KB Output is correct
40 Correct 6 ms 11612 KB Output is correct
41 Correct 5 ms 11612 KB Output is correct
42 Correct 5 ms 11356 KB Output is correct
43 Correct 6 ms 11612 KB Output is correct
44 Correct 6 ms 11440 KB Output is correct
45 Correct 6 ms 11356 KB Output is correct
46 Correct 6 ms 11356 KB Output is correct
47 Correct 6 ms 11448 KB Output is correct
48 Correct 6 ms 11612 KB Output is correct
49 Correct 11 ms 12380 KB Output is correct
50 Correct 70 ms 19928 KB Output is correct
51 Correct 8 ms 11864 KB Output is correct
52 Correct 60 ms 18196 KB Output is correct
53 Correct 11 ms 12632 KB Output is correct
54 Correct 64 ms 19100 KB Output is correct
55 Correct 8 ms 11868 KB Output is correct
56 Correct 7 ms 11868 KB Output is correct
57 Correct 7 ms 12380 KB Output is correct
58 Correct 7 ms 12468 KB Output is correct
59 Correct 5 ms 11356 KB Output is correct
60 Correct 48 ms 15948 KB Output is correct
61 Correct 6 ms 11612 KB Output is correct
62 Correct 67 ms 19284 KB Output is correct
63 Correct 230 ms 53884 KB Output is correct
64 Correct 221 ms 53844 KB Output is correct
65 Correct 244 ms 53840 KB Output is correct
66 Correct 50 ms 15704 KB Output is correct
67 Correct 84 ms 21588 KB Output is correct
68 Correct 47 ms 15644 KB Output is correct
69 Correct 161 ms 21840 KB Output is correct
70 Correct 60 ms 15632 KB Output is correct
71 Correct 60 ms 15620 KB Output is correct
72 Correct 101 ms 22100 KB Output is correct
73 Correct 138 ms 22096 KB Output is correct
74 Correct 355 ms 48376 KB Output is correct
75 Correct 254 ms 28496 KB Output is correct
76 Correct 276 ms 37720 KB Output is correct
77 Correct 272 ms 37712 KB Output is correct
78 Correct 96 ms 22388 KB Output is correct
79 Correct 174 ms 26196 KB Output is correct
80 Correct 102 ms 22304 KB Output is correct
81 Correct 143 ms 25684 KB Output is correct
82 Correct 306 ms 40784 KB Output is correct
83 Correct 319 ms 40620 KB Output is correct
84 Correct 308 ms 62544 KB Output is correct
85 Correct 332 ms 62296 KB Output is correct
86 Correct 52 ms 14928 KB Output is correct
87 Correct 75 ms 17236 KB Output is correct
88 Correct 116 ms 22300 KB Output is correct
89 Correct 260 ms 35156 KB Output is correct