Submission #1108277

# Submission time Handle Problem Language Result Execution time Memory
1108277 2024-11-03T15:58:04 Z damwuan Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1176 ms 115664 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+12;
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 43600 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43600 KB Output is correct
4 Correct 8 ms 43600 KB Output is correct
5 Correct 8 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 8 ms 43600 KB Output is correct
8 Correct 9 ms 43600 KB Output is correct
9 Correct 8 ms 43776 KB Output is correct
10 Correct 7 ms 43600 KB Output is correct
11 Correct 8 ms 43600 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43600 KB Output is correct
14 Correct 8 ms 43600 KB Output is correct
15 Correct 8 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 7 ms 43600 KB Output is correct
18 Correct 7 ms 43488 KB Output is correct
19 Correct 7 ms 43600 KB Output is correct
20 Correct 7 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 9 ms 43600 KB Output is correct
23 Correct 8 ms 43600 KB Output is correct
24 Correct 7 ms 43600 KB Output is correct
25 Correct 8 ms 43600 KB Output is correct
26 Correct 10 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 8 ms 43600 KB Output is correct
29 Correct 8 ms 43600 KB Output is correct
30 Correct 7 ms 43600 KB Output is correct
31 Correct 9 ms 43600 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 43600 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43600 KB Output is correct
4 Correct 8 ms 43600 KB Output is correct
5 Correct 8 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 8 ms 43600 KB Output is correct
8 Correct 9 ms 43600 KB Output is correct
9 Correct 8 ms 43776 KB Output is correct
10 Correct 7 ms 43600 KB Output is correct
11 Correct 8 ms 43600 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43600 KB Output is correct
14 Correct 8 ms 43600 KB Output is correct
15 Correct 8 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 7 ms 43600 KB Output is correct
18 Correct 7 ms 43488 KB Output is correct
19 Correct 7 ms 43600 KB Output is correct
20 Correct 7 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 9 ms 43600 KB Output is correct
23 Correct 8 ms 43600 KB Output is correct
24 Correct 7 ms 43600 KB Output is correct
25 Correct 8 ms 43600 KB Output is correct
26 Correct 10 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 8 ms 43600 KB Output is correct
29 Correct 8 ms 43600 KB Output is correct
30 Correct 7 ms 43600 KB Output is correct
31 Correct 9 ms 43600 KB Output is correct
32 Correct 8 ms 43600 KB Output is correct
33 Correct 8 ms 43600 KB Output is correct
34 Correct 10 ms 43856 KB Output is correct
35 Correct 80 ms 49656 KB Output is correct
36 Correct 107 ms 52912 KB Output is correct
37 Correct 99 ms 52928 KB Output is correct
38 Correct 98 ms 52424 KB Output is correct
39 Correct 8 ms 44024 KB Output is correct
40 Correct 12 ms 44368 KB Output is correct
41 Correct 11 ms 44252 KB Output is correct
42 Correct 9 ms 43912 KB Output is correct
43 Correct 10 ms 43856 KB Output is correct
44 Correct 9 ms 43856 KB Output is correct
45 Correct 9 ms 43856 KB Output is correct
46 Correct 10 ms 43856 KB Output is correct
47 Correct 12 ms 44112 KB Output is correct
48 Correct 11 ms 44112 KB Output is correct
49 Correct 20 ms 45224 KB Output is correct
50 Correct 102 ms 52928 KB Output is correct
51 Correct 13 ms 44368 KB Output is correct
52 Correct 92 ms 51176 KB Output is correct
53 Correct 16 ms 44880 KB Output is correct
54 Correct 95 ms 51872 KB Output is correct
55 Correct 14 ms 44368 KB Output is correct
56 Correct 13 ms 44536 KB Output is correct
57 Correct 12 ms 44536 KB Output is correct
58 Correct 12 ms 44380 KB Output is correct
59 Correct 10 ms 43856 KB Output is correct
60 Correct 76 ms 48732 KB Output is correct
61 Correct 11 ms 44112 KB Output is correct
62 Correct 100 ms 52200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 43600 KB Output is correct
2 Correct 7 ms 43600 KB Output is correct
3 Correct 7 ms 43600 KB Output is correct
4 Correct 8 ms 43600 KB Output is correct
5 Correct 8 ms 43600 KB Output is correct
6 Correct 7 ms 43600 KB Output is correct
7 Correct 8 ms 43600 KB Output is correct
8 Correct 9 ms 43600 KB Output is correct
9 Correct 8 ms 43776 KB Output is correct
10 Correct 7 ms 43600 KB Output is correct
11 Correct 8 ms 43600 KB Output is correct
12 Correct 8 ms 43600 KB Output is correct
13 Correct 8 ms 43600 KB Output is correct
14 Correct 8 ms 43600 KB Output is correct
15 Correct 8 ms 43600 KB Output is correct
16 Correct 8 ms 43600 KB Output is correct
17 Correct 7 ms 43600 KB Output is correct
18 Correct 7 ms 43488 KB Output is correct
19 Correct 7 ms 43600 KB Output is correct
20 Correct 7 ms 43600 KB Output is correct
21 Correct 8 ms 43600 KB Output is correct
22 Correct 9 ms 43600 KB Output is correct
23 Correct 8 ms 43600 KB Output is correct
24 Correct 7 ms 43600 KB Output is correct
25 Correct 8 ms 43600 KB Output is correct
26 Correct 10 ms 43600 KB Output is correct
27 Correct 8 ms 43600 KB Output is correct
28 Correct 8 ms 43600 KB Output is correct
29 Correct 8 ms 43600 KB Output is correct
30 Correct 7 ms 43600 KB Output is correct
31 Correct 9 ms 43600 KB Output is correct
32 Correct 8 ms 43600 KB Output is correct
33 Correct 8 ms 43600 KB Output is correct
34 Correct 10 ms 43856 KB Output is correct
35 Correct 80 ms 49656 KB Output is correct
36 Correct 107 ms 52912 KB Output is correct
37 Correct 99 ms 52928 KB Output is correct
38 Correct 98 ms 52424 KB Output is correct
39 Correct 8 ms 44024 KB Output is correct
40 Correct 12 ms 44368 KB Output is correct
41 Correct 11 ms 44252 KB Output is correct
42 Correct 9 ms 43912 KB Output is correct
43 Correct 10 ms 43856 KB Output is correct
44 Correct 9 ms 43856 KB Output is correct
45 Correct 9 ms 43856 KB Output is correct
46 Correct 10 ms 43856 KB Output is correct
47 Correct 12 ms 44112 KB Output is correct
48 Correct 11 ms 44112 KB Output is correct
49 Correct 20 ms 45224 KB Output is correct
50 Correct 102 ms 52928 KB Output is correct
51 Correct 13 ms 44368 KB Output is correct
52 Correct 92 ms 51176 KB Output is correct
53 Correct 16 ms 44880 KB Output is correct
54 Correct 95 ms 51872 KB Output is correct
55 Correct 14 ms 44368 KB Output is correct
56 Correct 13 ms 44536 KB Output is correct
57 Correct 12 ms 44536 KB Output is correct
58 Correct 12 ms 44380 KB Output is correct
59 Correct 10 ms 43856 KB Output is correct
60 Correct 76 ms 48732 KB Output is correct
61 Correct 11 ms 44112 KB Output is correct
62 Correct 100 ms 52200 KB Output is correct
63 Correct 373 ms 96072 KB Output is correct
64 Correct 395 ms 96072 KB Output is correct
65 Correct 424 ms 96072 KB Output is correct
66 Correct 142 ms 52824 KB Output is correct
67 Correct 520 ms 90952 KB Output is correct
68 Correct 125 ms 52712 KB Output is correct
69 Correct 290 ms 59720 KB Output is correct
70 Correct 148 ms 52800 KB Output is correct
71 Correct 146 ms 52808 KB Output is correct
72 Correct 368 ms 64316 KB Output is correct
73 Correct 363 ms 65608 KB Output is correct
74 Correct 1176 ms 115664 KB Output is correct
75 Correct 455 ms 67572 KB Output is correct
76 Correct 696 ms 83140 KB Output is correct
77 Correct 716 ms 83396 KB Output is correct
78 Correct 150 ms 63132 KB Output is correct
79 Correct 308 ms 66628 KB Output is correct
80 Correct 154 ms 63352 KB Output is correct
81 Correct 304 ms 66888 KB Output is correct
82 Correct 635 ms 84040 KB Output is correct
83 Correct 621 ms 84040 KB Output is correct
84 Correct 650 ms 86064 KB Output is correct
85 Correct 705 ms 85996 KB Output is correct
86 Correct 195 ms 53576 KB Output is correct
87 Correct 261 ms 55880 KB Output is correct
88 Correct 387 ms 65096 KB Output is correct
89 Correct 735 ms 81176 KB Output is correct