Submission #1108908

# Submission time Handle Problem Language Result Execution time Memory
1108908 2024-11-05T16:10:50 Z vjudge1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
975 ms 115640 KB

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)

#define int long long
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn=3e5+10;
const ll offset=2e5;
const ll inf=1e18;
const int base=350;
const ll mod=998244353;
int n,m;
int par[maxn];
set<int> adj[maxn],adj_r[maxn],L[maxn];
int res;
vector<pii> Q;
int Find(int u)
{
    return par[u]<0?u:par[u]=Find(par[u]);
}

void Union(int u,int v)
{
    if ((u=Find(u))==(v=Find(v))) return;
    if (L[u].size() + adj[u].size() + adj_r[u].size() < L[v].size()+adj[v].size()+adj_r[v].size()) swap(u,v);
    res+=-par[u]*L[v].size() - par[v]*L[u].size();
//    cerr<<u<<' '<<v<<' '<< res<<'\n';
    par[u]+=par[v];
    par[v]=u;
    for(int x: L[v])
    {
        if (L[u].find(x)!=L[u].end())
        {
            res+=par[u];
            continue;
        }
        L[u].insert(x);
    }
    L[v].clear();
    adj[u].erase(v);adj_r[u].erase(v);adj[v].erase(u);adj_r[v].erase(u);
    for(int x:adj[v])
    {
//        x=Find(x);
        adj[u].insert(x);
        adj_r[x].insert(u);
        if (adj_r[u].find(x)!=adj_r[u].end()) Q.pb({u,x});
    }
//    adj[v].clear();
    for(int x:adj_r[v])
    {
//        x=Find(x);
        adj_r[u].insert(x);
        adj[x].insert(u);
//        cerr<< adj[u].count(x)<<'\n';
        if (adj[u].find(x)!=adj[u].end()) Q.pb({u,x});
    }
//    adj_r[v].clear();

}

void sol()
{
    cin >>n>>m;
    for1(i,1,n)
    {
        par[i]=-1;
        L[i].insert(i);
    }

    for1(i,1,m)
    {
        int u,v;cin >> u>> v;
        v=Find(v);
        if (u==v || L[v].find(u)!=L[v].end()) cout << res<<'\n';
        else
        {
            L[v].insert(u);
            u=Find(u);
            adj[u].insert(v);
            adj_r[v].insert(u);
            res-=par[v];
//            cerr<< res<<" ashiba\n";
            if (adj_r[u].find(v)!=adj_r[u].end()) Q.pb({u,v});
            while (!Q.empty())
            {
//                cerr<<" ulion "<<Q.back().fi<<' '<<Q.back().se<<'\n';
                pii x=Q.back();
                Q.pop_back();
                Union(x.fi,x.se);

            }
            cout << res<<'\n';

        }
    }

}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("adfaf.inp","r",stdin);
//    freopen("adfaf.out","w",stdout);

    int t=1;//cin >> t;
    while (t--) {
        sol();
    }
}

/*
4 3
1 2
2 3
3 2



*/






# Verdict Execution time Memory Grader output
1 Correct 7 ms 43768 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43684 KB Output is correct
4 Correct 7 ms 43600 KB Output is correct
5 Correct 7 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 7 ms 43600 KB Output is correct
8 Correct 8 ms 43600 KB Output is correct
9 Correct 7 ms 43600 KB Output is correct
10 Correct 7 ms 43768 KB Output is correct
11 Correct 7 ms 43768 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43772 KB Output is correct
14 Correct 9 ms 43600 KB Output is correct
15 Correct 7 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 8 ms 43612 KB Output is correct
18 Correct 8 ms 43600 KB Output is correct
19 Correct 8 ms 43600 KB Output is correct
20 Correct 8 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 8 ms 43748 KB Output is correct
23 Correct 7 ms 43732 KB Output is correct
24 Correct 8 ms 43600 KB Output is correct
25 Correct 9 ms 43736 KB Output is correct
26 Correct 8 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 7 ms 43728 KB Output is correct
29 Correct 7 ms 43900 KB Output is correct
30 Correct 8 ms 43672 KB Output is correct
31 Correct 9 ms 43772 KB Output is correct
32 Correct 8 ms 43600 KB Output is correct
33 Correct 8 ms 43600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 43768 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43684 KB Output is correct
4 Correct 7 ms 43600 KB Output is correct
5 Correct 7 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 7 ms 43600 KB Output is correct
8 Correct 8 ms 43600 KB Output is correct
9 Correct 7 ms 43600 KB Output is correct
10 Correct 7 ms 43768 KB Output is correct
11 Correct 7 ms 43768 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43772 KB Output is correct
14 Correct 9 ms 43600 KB Output is correct
15 Correct 7 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 8 ms 43612 KB Output is correct
18 Correct 8 ms 43600 KB Output is correct
19 Correct 8 ms 43600 KB Output is correct
20 Correct 8 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 8 ms 43748 KB Output is correct
23 Correct 7 ms 43732 KB Output is correct
24 Correct 8 ms 43600 KB Output is correct
25 Correct 9 ms 43736 KB Output is correct
26 Correct 8 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 7 ms 43728 KB Output is correct
29 Correct 7 ms 43900 KB Output is correct
30 Correct 8 ms 43672 KB Output is correct
31 Correct 9 ms 43772 KB Output is correct
32 Correct 8 ms 43600 KB Output is correct
33 Correct 8 ms 43600 KB Output is correct
34 Correct 9 ms 43856 KB Output is correct
35 Correct 88 ms 49820 KB Output is correct
36 Correct 118 ms 53004 KB Output is correct
37 Correct 109 ms 52928 KB Output is correct
38 Correct 108 ms 52416 KB Output is correct
39 Correct 9 ms 43856 KB Output is correct
40 Correct 14 ms 44368 KB Output is correct
41 Correct 12 ms 44368 KB Output is correct
42 Correct 9 ms 44036 KB Output is correct
43 Correct 11 ms 43856 KB Output is correct
44 Correct 11 ms 43856 KB Output is correct
45 Correct 11 ms 43856 KB Output is correct
46 Correct 10 ms 44024 KB Output is correct
47 Correct 12 ms 44112 KB Output is correct
48 Correct 12 ms 44112 KB Output is correct
49 Correct 19 ms 45136 KB Output is correct
50 Correct 112 ms 52928 KB Output is correct
51 Correct 17 ms 44368 KB Output is correct
52 Correct 122 ms 50996 KB Output is correct
53 Correct 19 ms 44880 KB Output is correct
54 Correct 107 ms 52040 KB Output is correct
55 Correct 12 ms 44368 KB Output is correct
56 Correct 13 ms 44368 KB Output is correct
57 Correct 13 ms 44368 KB Output is correct
58 Correct 14 ms 44496 KB Output is correct
59 Correct 10 ms 43856 KB Output is correct
60 Correct 80 ms 48712 KB Output is correct
61 Correct 14 ms 44112 KB Output is correct
62 Correct 112 ms 52232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 43768 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43684 KB Output is correct
4 Correct 7 ms 43600 KB Output is correct
5 Correct 7 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 7 ms 43600 KB Output is correct
8 Correct 8 ms 43600 KB Output is correct
9 Correct 7 ms 43600 KB Output is correct
10 Correct 7 ms 43768 KB Output is correct
11 Correct 7 ms 43768 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43772 KB Output is correct
14 Correct 9 ms 43600 KB Output is correct
15 Correct 7 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 8 ms 43612 KB Output is correct
18 Correct 8 ms 43600 KB Output is correct
19 Correct 8 ms 43600 KB Output is correct
20 Correct 8 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 8 ms 43748 KB Output is correct
23 Correct 7 ms 43732 KB Output is correct
24 Correct 8 ms 43600 KB Output is correct
25 Correct 9 ms 43736 KB Output is correct
26 Correct 8 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 7 ms 43728 KB Output is correct
29 Correct 7 ms 43900 KB Output is correct
30 Correct 8 ms 43672 KB Output is correct
31 Correct 9 ms 43772 KB Output is correct
32 Correct 8 ms 43600 KB Output is correct
33 Correct 8 ms 43600 KB Output is correct
34 Correct 9 ms 43856 KB Output is correct
35 Correct 88 ms 49820 KB Output is correct
36 Correct 118 ms 53004 KB Output is correct
37 Correct 109 ms 52928 KB Output is correct
38 Correct 108 ms 52416 KB Output is correct
39 Correct 9 ms 43856 KB Output is correct
40 Correct 14 ms 44368 KB Output is correct
41 Correct 12 ms 44368 KB Output is correct
42 Correct 9 ms 44036 KB Output is correct
43 Correct 11 ms 43856 KB Output is correct
44 Correct 11 ms 43856 KB Output is correct
45 Correct 11 ms 43856 KB Output is correct
46 Correct 10 ms 44024 KB Output is correct
47 Correct 12 ms 44112 KB Output is correct
48 Correct 12 ms 44112 KB Output is correct
49 Correct 19 ms 45136 KB Output is correct
50 Correct 112 ms 52928 KB Output is correct
51 Correct 17 ms 44368 KB Output is correct
52 Correct 122 ms 50996 KB Output is correct
53 Correct 19 ms 44880 KB Output is correct
54 Correct 107 ms 52040 KB Output is correct
55 Correct 12 ms 44368 KB Output is correct
56 Correct 13 ms 44368 KB Output is correct
57 Correct 13 ms 44368 KB Output is correct
58 Correct 14 ms 44496 KB Output is correct
59 Correct 10 ms 43856 KB Output is correct
60 Correct 80 ms 48712 KB Output is correct
61 Correct 14 ms 44112 KB Output is correct
62 Correct 112 ms 52232 KB Output is correct
63 Correct 401 ms 96084 KB Output is correct
64 Correct 441 ms 96016 KB Output is correct
65 Correct 401 ms 96132 KB Output is correct
66 Correct 137 ms 52808 KB Output is correct
67 Correct 532 ms 90968 KB Output is correct
68 Correct 140 ms 52808 KB Output is correct
69 Correct 254 ms 59720 KB Output is correct
70 Correct 134 ms 52632 KB Output is correct
71 Correct 145 ms 52820 KB Output is correct
72 Correct 287 ms 64328 KB Output is correct
73 Correct 311 ms 65596 KB Output is correct
74 Correct 975 ms 115640 KB Output is correct
75 Correct 442 ms 67660 KB Output is correct
76 Correct 726 ms 83064 KB Output is correct
77 Correct 709 ms 83420 KB Output is correct
78 Correct 174 ms 63304 KB Output is correct
79 Correct 323 ms 66640 KB Output is correct
80 Correct 167 ms 63184 KB Output is correct
81 Correct 316 ms 66632 KB Output is correct
82 Correct 672 ms 84048 KB Output is correct
83 Correct 640 ms 84040 KB Output is correct
84 Correct 589 ms 85828 KB Output is correct
85 Correct 613 ms 85844 KB Output is correct
86 Correct 207 ms 53576 KB Output is correct
87 Correct 249 ms 55888 KB Output is correct
88 Correct 317 ms 65096 KB Output is correct
89 Correct 657 ms 81156 KB Output is correct