Submission #1041483

# Submission time Handle Problem Language Result Execution time Memory
1041483 2024-08-02T04:42:38 Z hotboy2703 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
214 ms 53216 KB
#include<bits/stdc++.h>
using ll = int;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll INF = 1e9;
const ll MAXN = 1e5+100;
set <ll> s[MAXN];
set <pll> rev[MAXN];
set <ll> edge[MAXN];
ll dsu[MAXN];
ll ans;
vector <pll> all;
void join(ll x,ll y){
    x = dsu[x],y = dsu[y];
    if (x==y)return;
    // cout<<"JOIN "<<x<<' '<<y<<'\n';
    if (sz(s[x]) < sz(s[y]))swap(x,y);
    for (auto it = rev[x].lower_bound(MP(y,0));it != rev[x].end() && (*it).fi == y;it=rev[x].erase(it)){
        // cout<<x<<' '<<(*it).se<<'\n';
        ans -= sz(s[x]);
    }
    for (auto it = rev[y].lower_bound(MP(x,0));it != rev[y].end() && (*it).fi == x;it=rev[y].erase(it)){
        // cout<<y<<' '<<(*it).se<<'\n';
        ans -= sz(s[y]);
    }
    // cout<<"BEF "<<ans<<'\n';
    ans += sz(s[x]) * sz(s[y]) * 2;
    ans += sz(rev[x]) * sz(s[y]) + sz(rev[y]) * sz(s[x]);
    // cout<<"AF "<<ans<<'\n';
    for (auto z:rev[y]){
        if (rev[x].insert(z).se == 0){
            ans -= sz(s[x]) + sz(s[y]);
        }
        edge[z.fi].erase(y);
        edge[z.fi].insert(x);
        if (edge[x].find(z.fi) != edge[x].end()){
            all.push_back(MP(x,z.fi));
        }
    }
    edge[y].erase(x);
    for (auto z:edge[y]){
        edge[x].insert(z);
        if (z!=x){
            vector <pll> tmp;
            // cout<<"REV "<<z<<'\n';
            // for (auto x:rev[z])cout<<x.fi<<' '<<x.se<<' ';
            // cout<<'\n';
            for (auto it = rev[z].lower_bound(MP(y,0));it != rev[z].end() && (*it).fi == y;it=rev[z].erase(it)){
                tmp.push_back(MP(x,(*it).se));
                // cout<<"ADDREV "<<z<<' '<<x<<' '<<(*it).se<<'\n';
            }
            // for (auto x:rev[z])cout<<x.fi<<' '<<x.se<<' ';
            // cout<<'\n';
            for (auto z1:tmp)rev[z].insert(z1);
            // for (auto x:rev[z])cout<<x.fi<<' '<<x.se<<' ';
            // cout<<'\n';
        }
    // cout<<"SUS "<<z<<' '<<x<<'\n';
    // for (auto z1:edge[z])cout<<z1<<' ';
    // cout<<'\n';
        if (edge[z].find(x) != edge[z].end()){
            // cout<<"ADD "<<x<<' '<<z<<'\n';
            all.push_back(MP(x,z));
        }
    }
    for (auto z:s[y]){
        dsu[z] = x;
        s[x].insert(z);
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);
    ll n,m;
    cin>>n>>m;
    for (ll i = 1;i <= n;i ++){
        s[i].insert(i);
        dsu[i] = i;
    }
    for (ll j = 1;j <= m;j ++){
        ll x,y;
        cin>>x>>y;
        if (dsu[x] != dsu[y]){
            // x = dsu[x],y = dsu[y];
            edge[dsu[x]].insert(dsu[y]);
            if (rev[dsu[y]].insert(MP(dsu[x],x)).se)ans+=sz(s[dsu[y]]);
            // if (j==6)cout<<"SUS" <<ans<<' '<<dsu[x]<<' '<<dsu[y]<<'\n';
            if (edge[dsu[y]].find(dsu[x]) != edge[dsu[y]].end()){
                all.push_back(MP(dsu[x],dsu[y]));
            }
            // if (rev[x].find(y) != rev[x].end()){
            //     all.push_back(MP(x,y));
            // }
            while (!all.empty()){
                // cout<<"TRY JOIN "<<all.back().fi<<' '<<all.back().se<<'\n';
                pll u = all.back();all.pop_back();
                join(u.fi,u.se);
                // all.pop_back();
            }
        }
        cout<<ans<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14748 KB Output is correct
7 Correct 3 ms 14808 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14760 KB Output is correct
12 Correct 2 ms 14684 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Correct 2 ms 14684 KB Output is correct
18 Correct 2 ms 14684 KB Output is correct
19 Correct 2 ms 14684 KB Output is correct
20 Correct 2 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 2 ms 14684 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 2 ms 14684 KB Output is correct
25 Correct 2 ms 14684 KB Output is correct
26 Correct 2 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
30 Correct 2 ms 14684 KB Output is correct
31 Correct 2 ms 14684 KB Output is correct
32 Correct 2 ms 14684 KB Output is correct
33 Correct 3 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14748 KB Output is correct
7 Correct 3 ms 14808 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14760 KB Output is correct
12 Correct 2 ms 14684 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Correct 2 ms 14684 KB Output is correct
18 Correct 2 ms 14684 KB Output is correct
19 Correct 2 ms 14684 KB Output is correct
20 Correct 2 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 2 ms 14684 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 2 ms 14684 KB Output is correct
25 Correct 2 ms 14684 KB Output is correct
26 Correct 2 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
30 Correct 2 ms 14684 KB Output is correct
31 Correct 2 ms 14684 KB Output is correct
32 Correct 2 ms 14684 KB Output is correct
33 Correct 3 ms 14684 KB Output is correct
34 Correct 4 ms 14940 KB Output is correct
35 Correct 42 ms 20048 KB Output is correct
36 Correct 56 ms 22352 KB Output is correct
37 Correct 55 ms 22496 KB Output is correct
38 Correct 59 ms 22100 KB Output is correct
39 Correct 4 ms 14936 KB Output is correct
40 Correct 6 ms 15708 KB Output is correct
41 Correct 5 ms 15564 KB Output is correct
42 Correct 4 ms 14940 KB Output is correct
43 Correct 4 ms 14940 KB Output is correct
44 Correct 5 ms 14940 KB Output is correct
45 Correct 3 ms 14996 KB Output is correct
46 Correct 7 ms 14940 KB Output is correct
47 Correct 5 ms 15452 KB Output is correct
48 Correct 5 ms 15452 KB Output is correct
49 Correct 8 ms 15824 KB Output is correct
50 Correct 61 ms 22480 KB Output is correct
51 Correct 6 ms 15196 KB Output is correct
52 Correct 48 ms 21076 KB Output is correct
53 Correct 7 ms 15548 KB Output is correct
54 Correct 53 ms 21728 KB Output is correct
55 Correct 5 ms 15192 KB Output is correct
56 Correct 6 ms 15196 KB Output is correct
57 Correct 4 ms 15196 KB Output is correct
58 Correct 5 ms 15196 KB Output is correct
59 Correct 5 ms 15964 KB Output is correct
60 Correct 42 ms 20816 KB Output is correct
61 Correct 6 ms 15196 KB Output is correct
62 Correct 52 ms 21908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14748 KB Output is correct
7 Correct 3 ms 14808 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 2 ms 14684 KB Output is correct
11 Correct 2 ms 14760 KB Output is correct
12 Correct 2 ms 14684 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Correct 2 ms 14684 KB Output is correct
18 Correct 2 ms 14684 KB Output is correct
19 Correct 2 ms 14684 KB Output is correct
20 Correct 2 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 2 ms 14684 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 2 ms 14684 KB Output is correct
25 Correct 2 ms 14684 KB Output is correct
26 Correct 2 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
30 Correct 2 ms 14684 KB Output is correct
31 Correct 2 ms 14684 KB Output is correct
32 Correct 2 ms 14684 KB Output is correct
33 Correct 3 ms 14684 KB Output is correct
34 Correct 4 ms 14940 KB Output is correct
35 Correct 42 ms 20048 KB Output is correct
36 Correct 56 ms 22352 KB Output is correct
37 Correct 55 ms 22496 KB Output is correct
38 Correct 59 ms 22100 KB Output is correct
39 Correct 4 ms 14936 KB Output is correct
40 Correct 6 ms 15708 KB Output is correct
41 Correct 5 ms 15564 KB Output is correct
42 Correct 4 ms 14940 KB Output is correct
43 Correct 4 ms 14940 KB Output is correct
44 Correct 5 ms 14940 KB Output is correct
45 Correct 3 ms 14996 KB Output is correct
46 Correct 7 ms 14940 KB Output is correct
47 Correct 5 ms 15452 KB Output is correct
48 Correct 5 ms 15452 KB Output is correct
49 Correct 8 ms 15824 KB Output is correct
50 Correct 61 ms 22480 KB Output is correct
51 Correct 6 ms 15196 KB Output is correct
52 Correct 48 ms 21076 KB Output is correct
53 Correct 7 ms 15548 KB Output is correct
54 Correct 53 ms 21728 KB Output is correct
55 Correct 5 ms 15192 KB Output is correct
56 Correct 6 ms 15196 KB Output is correct
57 Correct 4 ms 15196 KB Output is correct
58 Correct 5 ms 15196 KB Output is correct
59 Correct 5 ms 15964 KB Output is correct
60 Correct 42 ms 20816 KB Output is correct
61 Correct 6 ms 15196 KB Output is correct
62 Correct 52 ms 21908 KB Output is correct
63 Correct 214 ms 53216 KB Output is correct
64 Correct 192 ms 53028 KB Output is correct
65 Correct 214 ms 53156 KB Output is correct
66 Incorrect 105 ms 33364 KB Output isn't correct
67 Halted 0 ms 0 KB -