Submission #1000166

# Submission time Handle Problem Language Result Execution time Memory
1000166 2024-06-16T19:04:57 Z snpmrnhlol Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
5000 ms 38740 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
typedef long long ll;
ll ans = 0;
int n,m;
struct DSU{
    int p,sz;
    set <int> boys;
    vector <int> comp;
}dsu[N];
int leader(int x){
    if(x == dsu[x].p)return x;
    return dsu[x].p = leader(dsu[x].p);
}
bool checkedge(int x,int y){
    int compx = leader(x);
    int compy = leader(y);
    if(compx == compy)return 1;
    if(dsu[compy].boys.find(x) != dsu[compy].boys.end())return 1;
    return 0;
}
bool check(int x,int y){
    if(x == y)return 1;
    for(auto i:dsu[x].comp){
        if(dsu[y].boys.find(i) != dsu[y].boys.end()){
            return 1;
        }
    }
    return 0;
}
void connect(int x,int y){
    x = leader(x);
    y = leader(y);
    if(x == y)return;
    if(dsu[x].sz > dsu[y].sz)swap(x,y);
    for(auto i:dsu[y].comp){
        auto it = dsu[x].boys.find(i);
        if(it != dsu[x].boys.end()){
            ans-=dsu[x].sz;
            dsu[x].boys.erase(it);
        }
    }
    for(auto i:dsu[x].comp){
        auto it = dsu[y].boys.find(i);
        if(it != dsu[y].boys.end()){
            ans-=dsu[y].sz;
            dsu[y].boys.erase(it);
        }
        dsu[y].comp.push_back(i);
    }
    ans+=1ll*dsu[x].boys.size()*dsu[y].sz;
    ans+=1ll*dsu[y].boys.size()*dsu[x].sz;
    for(auto i:dsu[x].boys){
        assert(leader(i) != y);
        if(dsu[y].boys.find(i) != dsu[y].boys.end()){
            ans-=dsu[x].sz;
            ans-=dsu[y].sz;
        }
        dsu[y].boys.insert(i);
    }
    ans+=2ll*dsu[x].sz*dsu[y].sz;
    dsu[x].comp.clear();
    dsu[x].boys.clear();
    dsu[x].p = y;
    dsu[y].sz+=dsu[x].sz;
    for(auto i:dsu[y].boys){
        if(check(leader(i),y) && check(y,leader(i))){
            connect(leader(i),y);
            break;
        }
    }
}
void addedge(int x,int y){
    int compx = leader(x);
    int compy = leader(y);
    if(checkedge(x,y))return;
    if(check(compy,compx)){
        connect(x,y);
    }else{
        dsu[compy].boys.insert(x);
        ans+=dsu[compy].sz;
    }
}
void dbg(){
    for(int i = 0;i < n;i++){
        cout<<"dsuinvestigate:"<<i<<'\n';
        cout<<dsu[i].p<<' '<<dsu[i].sz<<'\n';
        for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
        for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    for(int i = 0;i < n;i++){
        dsu[i] = {i,1};
        dsu[i].comp.push_back(i);
    }
    for(int i = 0;i < m;i++){
        int u,w;
        cin>>u>>w;
        u--;w--;
        addedge(u,w);
        //dbg();
        cout<<ans<<'\n';
    }
    return 0;
}
/**
5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2
5 2
1 4
4 1
3 4
5 1
2 4
2 1
4 3
1 3
5 4
3 5
5 3

**/

Compilation message

joitter2.cpp: In function 'void dbg()':
joitter2.cpp:89:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   89 |         for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
      |         ^~~
joitter2.cpp:89:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   89 |         for(auto j:dsu[i].boys)cout<<j<<' ';cout<<'\n';
      |                                             ^~~~
joitter2.cpp:90:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   90 |         for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
      |         ^~~
joitter2.cpp:90:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   90 |         for(auto j:dsu[i].comp)cout<<j<<' ';cout<<'\n';
      |                                             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 5 ms 16120 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15888 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 16108 KB Output is correct
7 Correct 6 ms 15964 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 6 ms 16060 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
11 Correct 7 ms 16216 KB Output is correct
12 Correct 5 ms 15960 KB Output is correct
13 Correct 5 ms 15964 KB Output is correct
14 Correct 5 ms 15912 KB Output is correct
15 Correct 5 ms 15964 KB Output is correct
16 Correct 5 ms 15964 KB Output is correct
17 Correct 5 ms 15960 KB Output is correct
18 Correct 5 ms 15960 KB Output is correct
19 Correct 6 ms 15964 KB Output is correct
20 Correct 7 ms 15960 KB Output is correct
21 Correct 6 ms 15964 KB Output is correct
22 Correct 5 ms 15960 KB Output is correct
23 Correct 6 ms 15960 KB Output is correct
24 Correct 5 ms 15964 KB Output is correct
25 Correct 6 ms 15964 KB Output is correct
26 Correct 6 ms 15964 KB Output is correct
27 Correct 6 ms 15964 KB Output is correct
28 Correct 6 ms 15964 KB Output is correct
29 Correct 6 ms 15964 KB Output is correct
30 Correct 7 ms 15964 KB Output is correct
31 Correct 6 ms 15964 KB Output is correct
32 Correct 7 ms 15964 KB Output is correct
33 Correct 6 ms 16016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 5 ms 16120 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15888 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 16108 KB Output is correct
7 Correct 6 ms 15964 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 6 ms 16060 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
11 Correct 7 ms 16216 KB Output is correct
12 Correct 5 ms 15960 KB Output is correct
13 Correct 5 ms 15964 KB Output is correct
14 Correct 5 ms 15912 KB Output is correct
15 Correct 5 ms 15964 KB Output is correct
16 Correct 5 ms 15964 KB Output is correct
17 Correct 5 ms 15960 KB Output is correct
18 Correct 5 ms 15960 KB Output is correct
19 Correct 6 ms 15964 KB Output is correct
20 Correct 7 ms 15960 KB Output is correct
21 Correct 6 ms 15964 KB Output is correct
22 Correct 5 ms 15960 KB Output is correct
23 Correct 6 ms 15960 KB Output is correct
24 Correct 5 ms 15964 KB Output is correct
25 Correct 6 ms 15964 KB Output is correct
26 Correct 6 ms 15964 KB Output is correct
27 Correct 6 ms 15964 KB Output is correct
28 Correct 6 ms 15964 KB Output is correct
29 Correct 6 ms 15964 KB Output is correct
30 Correct 7 ms 15964 KB Output is correct
31 Correct 6 ms 15964 KB Output is correct
32 Correct 7 ms 15964 KB Output is correct
33 Correct 6 ms 16016 KB Output is correct
34 Correct 6 ms 16216 KB Output is correct
35 Correct 58 ms 20688 KB Output is correct
36 Correct 100 ms 22124 KB Output is correct
37 Correct 94 ms 22168 KB Output is correct
38 Correct 107 ms 22100 KB Output is correct
39 Correct 18 ms 16216 KB Output is correct
40 Correct 6 ms 16220 KB Output is correct
41 Correct 7 ms 16220 KB Output is correct
42 Correct 7 ms 16220 KB Output is correct
43 Correct 412 ms 16220 KB Output is correct
44 Correct 385 ms 16216 KB Output is correct
45 Correct 12 ms 16220 KB Output is correct
46 Correct 13 ms 16248 KB Output is correct
47 Correct 19 ms 16308 KB Output is correct
48 Correct 25 ms 16220 KB Output is correct
49 Correct 154 ms 16472 KB Output is correct
50 Correct 92 ms 22096 KB Output is correct
51 Correct 819 ms 16372 KB Output is correct
52 Correct 175 ms 21580 KB Output is correct
53 Correct 326 ms 16444 KB Output is correct
54 Correct 121 ms 21844 KB Output is correct
55 Correct 786 ms 16408 KB Output is correct
56 Correct 859 ms 16216 KB Output is correct
57 Correct 978 ms 16400 KB Output is correct
58 Correct 1053 ms 16468 KB Output is correct
59 Correct 6 ms 16216 KB Output is correct
60 Correct 52 ms 21076 KB Output is correct
61 Correct 155 ms 16220 KB Output is correct
62 Correct 106 ms 22096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 5 ms 16120 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15888 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 16108 KB Output is correct
7 Correct 6 ms 15964 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 6 ms 16060 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
11 Correct 7 ms 16216 KB Output is correct
12 Correct 5 ms 15960 KB Output is correct
13 Correct 5 ms 15964 KB Output is correct
14 Correct 5 ms 15912 KB Output is correct
15 Correct 5 ms 15964 KB Output is correct
16 Correct 5 ms 15964 KB Output is correct
17 Correct 5 ms 15960 KB Output is correct
18 Correct 5 ms 15960 KB Output is correct
19 Correct 6 ms 15964 KB Output is correct
20 Correct 7 ms 15960 KB Output is correct
21 Correct 6 ms 15964 KB Output is correct
22 Correct 5 ms 15960 KB Output is correct
23 Correct 6 ms 15960 KB Output is correct
24 Correct 5 ms 15964 KB Output is correct
25 Correct 6 ms 15964 KB Output is correct
26 Correct 6 ms 15964 KB Output is correct
27 Correct 6 ms 15964 KB Output is correct
28 Correct 6 ms 15964 KB Output is correct
29 Correct 6 ms 15964 KB Output is correct
30 Correct 7 ms 15964 KB Output is correct
31 Correct 6 ms 15964 KB Output is correct
32 Correct 7 ms 15964 KB Output is correct
33 Correct 6 ms 16016 KB Output is correct
34 Correct 6 ms 16216 KB Output is correct
35 Correct 58 ms 20688 KB Output is correct
36 Correct 100 ms 22124 KB Output is correct
37 Correct 94 ms 22168 KB Output is correct
38 Correct 107 ms 22100 KB Output is correct
39 Correct 18 ms 16216 KB Output is correct
40 Correct 6 ms 16220 KB Output is correct
41 Correct 7 ms 16220 KB Output is correct
42 Correct 7 ms 16220 KB Output is correct
43 Correct 412 ms 16220 KB Output is correct
44 Correct 385 ms 16216 KB Output is correct
45 Correct 12 ms 16220 KB Output is correct
46 Correct 13 ms 16248 KB Output is correct
47 Correct 19 ms 16308 KB Output is correct
48 Correct 25 ms 16220 KB Output is correct
49 Correct 154 ms 16472 KB Output is correct
50 Correct 92 ms 22096 KB Output is correct
51 Correct 819 ms 16372 KB Output is correct
52 Correct 175 ms 21580 KB Output is correct
53 Correct 326 ms 16444 KB Output is correct
54 Correct 121 ms 21844 KB Output is correct
55 Correct 786 ms 16408 KB Output is correct
56 Correct 859 ms 16216 KB Output is correct
57 Correct 978 ms 16400 KB Output is correct
58 Correct 1053 ms 16468 KB Output is correct
59 Correct 6 ms 16216 KB Output is correct
60 Correct 52 ms 21076 KB Output is correct
61 Correct 155 ms 16220 KB Output is correct
62 Correct 106 ms 22096 KB Output is correct
63 Correct 115 ms 38740 KB Output is correct
64 Correct 123 ms 38740 KB Output is correct
65 Correct 113 ms 38548 KB Output is correct
66 Execution timed out 5061 ms 21140 KB Time limit exceeded
67 Halted 0 ms 0 KB -