답안 #212566

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212566 2020-03-23T14:38:17 Z ereonzis 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1810 ms 67512 KB
#include <bits/stdc++.h>
using namespace std;


using LL = long long int;
using uLL = unsigned long long int;
using uint = unsigned int;
using ld = long double;

const int N = 100007;

int parent[N];
set <int> in[N], out[N], vert[N];
set <pair<int,int> > S;
queue <pair<int,int> > Q;
LL ans;


void init_UF(int n){
    for(int i = 1; i <= n; i++){
        parent[i] = i;
        vert[i].insert(i);
    }
}

inline LL newton(int n){
    return LL(n)*(n-1);
}


int FIND(int x){
    if(x == parent[x]) return x;
    return parent[x] = FIND(parent[x]);
}

void add_edge(int a, int b){
    int pa = FIND(a), pb = FIND(b);
    if(pa == pb) return;
    if(in[pb].find(a) == in[pb].end()){
        in[pb].insert(a);
        ans += vert[pb].size();
    }
    out[pa].insert(b);
    S.insert({pa, pb});
    if(S.find({pb, pa}) != S.end()){
        Q.push({pa, pb});
    }
}



void UNION(int a, int b){
    if(a == b) return;
    if(vert[a].size() > vert[b].size()) swap(a, b);
    parent[a] = b;
    S.erase({a, b});
    S.erase({b, a});
    ans -= LL(vert[b].size()) * in[b].size();
    ans -= LL(vert[a].size()) * in[a].size();
    ans -= newton(vert[a].size());
    ans -= newton(vert[b].size());
    for(auto x: vert[a]){
        if(in[b].find(x) != in[b].end()){
            in[b].erase(x);
        }
        if(out[b].find(x) != out[b].end()){
            out[b].erase(x);
        }
        vert[b].insert(x);
    }
    vert[a].clear();
    for(auto x: in[a]){
        if(vert[b].find(x) != vert[b].end()) continue;
        int y = FIND(x);
        S.erase({y, a});
        S.insert({y, b});
        if(S.find({b, y}) != S.end()){
            Q.push({b, y});
        }
        in[b].insert(x);
    }
    in[a].clear();
    for(auto x: out[a]){
        if(vert[b].find(x) != vert[b].end()) continue;
        int y = FIND(x);
        S.erase({a, y});
        S.insert({b, y});
        if(S.find({y, b}) != S.end()){
            Q.push({b, y});
        }
        out[b].insert(x);
    }
    out[a].clear();
    /*cout << "LOL " << vert[b].size() << " " << in[b].size() << '\n';
    for(auto x: in[b]) cout << x << " ";
    cout << '\n';
    for(auto x: vert[b]) cout << x << " ";
    cout << '\n';*/
    ans += LL(vert[b].size()) * in[b].size();
    ans += newton(vert[b].size());
}



void update(){
    while(!Q.empty()){
        pair<int,int> x = Q.front();
        Q.pop();
        UNION(FIND(x.first), FIND(x.second));
    }
}



int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(0);
    int n, q;
    cin >> n >> q;
    init_UF(n);
    while(q--){
        int a, b;
        cin >> a >> b;
        add_edge(a, b);
        update();
        cout << ans << '\n';
    }
    
    
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14336 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 14 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 14 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 13 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 13 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 16 ms 14464 KB Output is correct
19 Correct 13 ms 14464 KB Output is correct
20 Correct 16 ms 14592 KB Output is correct
21 Correct 14 ms 14464 KB Output is correct
22 Correct 15 ms 14464 KB Output is correct
23 Correct 13 ms 14464 KB Output is correct
24 Correct 13 ms 14464 KB Output is correct
25 Correct 15 ms 14464 KB Output is correct
26 Correct 13 ms 14464 KB Output is correct
27 Correct 13 ms 14464 KB Output is correct
28 Correct 14 ms 14464 KB Output is correct
29 Correct 13 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 13 ms 14464 KB Output is correct
33 Correct 14 ms 14464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14336 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 14 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 14 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 13 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 13 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 16 ms 14464 KB Output is correct
19 Correct 13 ms 14464 KB Output is correct
20 Correct 16 ms 14592 KB Output is correct
21 Correct 14 ms 14464 KB Output is correct
22 Correct 15 ms 14464 KB Output is correct
23 Correct 13 ms 14464 KB Output is correct
24 Correct 13 ms 14464 KB Output is correct
25 Correct 15 ms 14464 KB Output is correct
26 Correct 13 ms 14464 KB Output is correct
27 Correct 13 ms 14464 KB Output is correct
28 Correct 14 ms 14464 KB Output is correct
29 Correct 13 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 13 ms 14464 KB Output is correct
33 Correct 14 ms 14464 KB Output is correct
34 Correct 16 ms 14592 KB Output is correct
35 Correct 105 ms 20216 KB Output is correct
36 Correct 148 ms 22904 KB Output is correct
37 Correct 149 ms 23032 KB Output is correct
38 Correct 153 ms 22820 KB Output is correct
39 Correct 16 ms 14592 KB Output is correct
40 Correct 19 ms 14720 KB Output is correct
41 Correct 19 ms 14720 KB Output is correct
42 Correct 16 ms 14632 KB Output is correct
43 Correct 18 ms 14720 KB Output is correct
44 Correct 20 ms 14720 KB Output is correct
45 Correct 16 ms 14592 KB Output is correct
46 Correct 15 ms 14592 KB Output is correct
47 Correct 19 ms 14720 KB Output is correct
48 Correct 19 ms 14720 KB Output is correct
49 Correct 30 ms 15488 KB Output is correct
50 Correct 147 ms 23080 KB Output is correct
51 Correct 23 ms 14976 KB Output is correct
52 Correct 123 ms 21372 KB Output is correct
53 Correct 29 ms 15360 KB Output is correct
54 Correct 136 ms 22264 KB Output is correct
55 Correct 22 ms 15232 KB Output is correct
56 Correct 22 ms 15104 KB Output is correct
57 Correct 21 ms 15104 KB Output is correct
58 Correct 22 ms 15104 KB Output is correct
59 Correct 16 ms 14592 KB Output is correct
60 Correct 98 ms 19576 KB Output is correct
61 Correct 21 ms 14848 KB Output is correct
62 Correct 135 ms 22520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14336 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 14 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 14 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 13 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 13 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 16 ms 14464 KB Output is correct
19 Correct 13 ms 14464 KB Output is correct
20 Correct 16 ms 14592 KB Output is correct
21 Correct 14 ms 14464 KB Output is correct
22 Correct 15 ms 14464 KB Output is correct
23 Correct 13 ms 14464 KB Output is correct
24 Correct 13 ms 14464 KB Output is correct
25 Correct 15 ms 14464 KB Output is correct
26 Correct 13 ms 14464 KB Output is correct
27 Correct 13 ms 14464 KB Output is correct
28 Correct 14 ms 14464 KB Output is correct
29 Correct 13 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 13 ms 14464 KB Output is correct
33 Correct 14 ms 14464 KB Output is correct
34 Correct 16 ms 14592 KB Output is correct
35 Correct 105 ms 20216 KB Output is correct
36 Correct 148 ms 22904 KB Output is correct
37 Correct 149 ms 23032 KB Output is correct
38 Correct 153 ms 22820 KB Output is correct
39 Correct 16 ms 14592 KB Output is correct
40 Correct 19 ms 14720 KB Output is correct
41 Correct 19 ms 14720 KB Output is correct
42 Correct 16 ms 14632 KB Output is correct
43 Correct 18 ms 14720 KB Output is correct
44 Correct 20 ms 14720 KB Output is correct
45 Correct 16 ms 14592 KB Output is correct
46 Correct 15 ms 14592 KB Output is correct
47 Correct 19 ms 14720 KB Output is correct
48 Correct 19 ms 14720 KB Output is correct
49 Correct 30 ms 15488 KB Output is correct
50 Correct 147 ms 23080 KB Output is correct
51 Correct 23 ms 14976 KB Output is correct
52 Correct 123 ms 21372 KB Output is correct
53 Correct 29 ms 15360 KB Output is correct
54 Correct 136 ms 22264 KB Output is correct
55 Correct 22 ms 15232 KB Output is correct
56 Correct 22 ms 15104 KB Output is correct
57 Correct 21 ms 15104 KB Output is correct
58 Correct 22 ms 15104 KB Output is correct
59 Correct 16 ms 14592 KB Output is correct
60 Correct 98 ms 19576 KB Output is correct
61 Correct 21 ms 14848 KB Output is correct
62 Correct 135 ms 22520 KB Output is correct
63 Correct 856 ms 67248 KB Output is correct
64 Correct 843 ms 67320 KB Output is correct
65 Correct 861 ms 67512 KB Output is correct
66 Correct 255 ms 23980 KB Output is correct
67 Correct 725 ms 30200 KB Output is correct
68 Correct 224 ms 23944 KB Output is correct
69 Correct 648 ms 31224 KB Output is correct
70 Correct 249 ms 23928 KB Output is correct
71 Correct 247 ms 24056 KB Output is correct
72 Correct 768 ms 30456 KB Output is correct
73 Correct 786 ms 30472 KB Output is correct
74 Correct 1810 ms 41592 KB Output is correct
75 Correct 939 ms 36984 KB Output is correct
76 Correct 1389 ms 41292 KB Output is correct
77 Correct 1490 ms 41360 KB Output is correct
78 Correct 372 ms 32248 KB Output is correct
79 Correct 661 ms 35320 KB Output is correct
80 Correct 372 ms 32124 KB Output is correct
81 Correct 689 ms 35400 KB Output is correct
82 Correct 1676 ms 51576 KB Output is correct
83 Correct 1700 ms 51704 KB Output is correct
84 Correct 1290 ms 51448 KB Output is correct
85 Correct 1295 ms 51448 KB Output is correct
86 Correct 319 ms 24824 KB Output is correct
87 Correct 361 ms 27256 KB Output is correct
88 Correct 808 ms 30468 KB Output is correct
89 Correct 1371 ms 41024 KB Output is correct