Submission #568147

# Submission time Handle Problem Language Result Execution time Memory
568147 2022-05-24T17:26:58 Z alvingogo Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1516 ms 162696 KB
//#define __GLIBCXX_DEBUG
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;

struct no{
    int bo,sz;
    map<int,int> tb,fb;
    set<int> tp,fp;
    vector<int> ch;
};
vector<no> v;
long long ans=0;
int find(int a){
    return v[a].bo==a?a:v[a].bo=find(v[a].bo);
}
void merge(int x,int y){
    queue<pair<int,int> > q;
    q.push({x,y});
    while(!q.empty()){
        auto g=q.front();
        q.pop();
        int a=find(g.fs),b=find(g.sc);
        if(a==b){
            continue;
        }
        if(v[a].sz>v[b].sz){
            swap(a,b);
        }
        ans-=1ll*v[b].tp.size()*v[b].sz;
        ans-=1ll*v[a].tp.size()*v[a].sz;
        ans-=1ll*(v[b].sz)*(v[b].sz-1);
        ans-=1ll*(v[a].sz)*(v[a].sz-1);
        for(auto y:v[a].tp){
            int u=find(y);
            if(u==b){
                continue;
            }
            v[b].tp.insert(y);
            v[u].fb.erase(a);
            v[u].fb[b]++;
        }
        for(auto y:v[a].fp){
            int u=find(y);
            if(u==b){
                continue;
            }
            v[b].fp.insert(y);
            v[u].tb.erase(a);
            v[u].tb[b]++;
        }
        for(auto y:v[a].ch){
            v[b].tp.erase(y);
            v[b].fp.erase(y);
            v[b].ch.push_back(y);
        }
        v[b].sz+=v[a].sz;
        v[a].bo=b;
        ans+=1ll*v[b].tp.size()*v[b].sz;
        ans+=1ll*(v[b].sz)*(v[b].sz-1);
        set<int> nxt;
        for(auto y:v[a].tp){
            int u=find(y);
            if(u==b){
                continue;
            }
            if(v[u].tb.find(b)!=v[u].tb.end()){
                nxt.insert(u);
            }
        }
        for(auto y:v[a].fp){
            int u=find(y);
            if(u==b){
                continue;
            }
            if(v[u].fb.find(b)!=v[u].fb.end()){
                nxt.insert(u);
            }
        }
        for(auto y:v[a].tb){
            if(y.fs!=b){
                v[b].tb[y.fs]++;
            }
        }
        for(auto y:v[a].fb){
            if(y.fs!=b){
                v[b].fb[y.fs]++;
            }
        }
        for(auto h:nxt){
            q.push({h,b});
        }
    }
}
signed main(){
    AquA;
    int n,m;
    cin >> n >> m;
    v.resize(n);
    for(int i=0;i<n;i++){
        v[i].bo=i;
        v[i].sz=1;
        v[i].ch.push_back(i);
    }
    for(int i=0;i<m;i++){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        int c=find(a),d=find(b);
        if(c==d){
            cout << ans << "\n";
            continue;
        }
        else{
            if(v[d].tp.find(a)==v[d].tp.end()){
                v[d].tp.insert(a);
                ans+=v[d].sz;
                v[d].tb[c]++;
                v[c].fb[d]++;
                v[c].fp.insert(b);
                if(v[c].tb.find(d)!=v[c].tb.end()){
                    merge(a,b);
                }
            }
        }
        cout << ans << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 224 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 224 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 75 ms 6988 KB Output is correct
36 Correct 112 ms 11488 KB Output is correct
37 Correct 119 ms 11720 KB Output is correct
38 Correct 119 ms 11000 KB Output is correct
39 Correct 4 ms 1612 KB Output is correct
40 Correct 7 ms 2644 KB Output is correct
41 Correct 7 ms 2560 KB Output is correct
42 Correct 4 ms 1620 KB Output is correct
43 Correct 5 ms 1620 KB Output is correct
44 Correct 5 ms 1612 KB Output is correct
45 Correct 5 ms 1620 KB Output is correct
46 Correct 4 ms 1620 KB Output is correct
47 Correct 7 ms 2132 KB Output is correct
48 Correct 7 ms 2056 KB Output is correct
49 Correct 14 ms 2956 KB Output is correct
50 Correct 124 ms 11852 KB Output is correct
51 Correct 9 ms 2004 KB Output is correct
52 Correct 84 ms 8976 KB Output is correct
53 Correct 17 ms 2680 KB Output is correct
54 Correct 108 ms 10332 KB Output is correct
55 Correct 7 ms 2004 KB Output is correct
56 Correct 6 ms 1988 KB Output is correct
57 Correct 7 ms 2004 KB Output is correct
58 Correct 5 ms 1996 KB Output is correct
59 Correct 6 ms 2764 KB Output is correct
60 Correct 72 ms 7628 KB Output is correct
61 Correct 8 ms 2128 KB Output is correct
62 Correct 106 ms 10780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 224 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 75 ms 6988 KB Output is correct
36 Correct 112 ms 11488 KB Output is correct
37 Correct 119 ms 11720 KB Output is correct
38 Correct 119 ms 11000 KB Output is correct
39 Correct 4 ms 1612 KB Output is correct
40 Correct 7 ms 2644 KB Output is correct
41 Correct 7 ms 2560 KB Output is correct
42 Correct 4 ms 1620 KB Output is correct
43 Correct 5 ms 1620 KB Output is correct
44 Correct 5 ms 1612 KB Output is correct
45 Correct 5 ms 1620 KB Output is correct
46 Correct 4 ms 1620 KB Output is correct
47 Correct 7 ms 2132 KB Output is correct
48 Correct 7 ms 2056 KB Output is correct
49 Correct 14 ms 2956 KB Output is correct
50 Correct 124 ms 11852 KB Output is correct
51 Correct 9 ms 2004 KB Output is correct
52 Correct 84 ms 8976 KB Output is correct
53 Correct 17 ms 2680 KB Output is correct
54 Correct 108 ms 10332 KB Output is correct
55 Correct 7 ms 2004 KB Output is correct
56 Correct 6 ms 1988 KB Output is correct
57 Correct 7 ms 2004 KB Output is correct
58 Correct 5 ms 1996 KB Output is correct
59 Correct 6 ms 2764 KB Output is correct
60 Correct 72 ms 7628 KB Output is correct
61 Correct 8 ms 2128 KB Output is correct
62 Correct 106 ms 10780 KB Output is correct
63 Correct 457 ms 97244 KB Output is correct
64 Correct 445 ms 97232 KB Output is correct
65 Correct 444 ms 97228 KB Output is correct
66 Correct 290 ms 65752 KB Output is correct
67 Correct 789 ms 140032 KB Output is correct
68 Correct 302 ms 65788 KB Output is correct
69 Correct 606 ms 65836 KB Output is correct
70 Correct 325 ms 65772 KB Output is correct
71 Correct 338 ms 65748 KB Output is correct
72 Correct 834 ms 95336 KB Output is correct
73 Correct 853 ms 99972 KB Output is correct
74 Correct 1516 ms 130136 KB Output is correct
75 Correct 806 ms 74332 KB Output is correct
76 Correct 1088 ms 99636 KB Output is correct
77 Correct 1025 ms 99964 KB Output is correct
78 Correct 361 ms 66568 KB Output is correct
79 Correct 620 ms 77868 KB Output is correct
80 Correct 327 ms 66592 KB Output is correct
81 Correct 621 ms 76544 KB Output is correct
82 Correct 738 ms 88032 KB Output is correct
83 Correct 763 ms 88048 KB Output is correct
84 Correct 599 ms 87524 KB Output is correct
85 Correct 606 ms 87544 KB Output is correct
86 Correct 654 ms 160392 KB Output is correct
87 Correct 667 ms 162696 KB Output is correct
88 Correct 784 ms 97552 KB Output is correct
89 Correct 920 ms 96960 KB Output is correct