Submission #233217

# Submission time Handle Problem Language Result Execution time Memory
233217 2020-05-19T23:17:19 Z medk Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1737 ms 136368 KB
#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define x first
#define y second
#define sz(u) (long long)(u.size())
#define all(u) u.begin(),u.end()

using namespace std;

const int MAXN=1e5;
int n,m;
ll ans=0;

map<int,set<int>> in[MAXN],out[MAXN];
set<int> in2[MAXN],out2[MAXN];
vector<ll> dsu,sz,acc(MAXN);


//in : **node** -> this group
//out : this node -> **group**
//in2 : **group** -> this group
//out2: this group -> **group**

int getp(int u)
{
    if(dsu[u]==u) return u;
    return dsu[u]=getp(dsu[u]);
}

void connect(int u, int v)
{
    u=getp(u), v=getp(v);
    if(u==v) return;
    if(sz[v]+acc[v]+sz(in2[v])+sz(out2[v])>sz[u]+acc[u]+sz(in2[u])+sz(out2[u])) swap(u,v);
    ans-=sz[u]*(sz[u]-1);
    ans-=sz[v]*(sz[v]-1);
    ans+=(sz[u]+sz[v])*(sz[u]+sz[v]-1);
    in2[u].erase(v);
    out2[u].erase(v);
    in2[v].erase(u);
    out2[v].erase(u);
    vector<int> qu;
    acc[u]-=sz(in[u][v]);
    ans+=acc[u]*sz[v];
    for(int w:in2[v])
    {
        in2[u].insert(w);
        out2[w].erase(v);
        out2[w].insert(u);
        for(int z:in[v][w])
        {
            if(!in[u][w].count(z)) {in[u][w].insert(z), ans+=sz[u], acc[u]++;}
            else ans-=sz[v];
            out[w][u].insert(z);
        }
    }
    for(int w:out2[v])
    {
        out2[u].insert(w);
        in2[w].erase(v);
        in2[w].insert(u);
        if(in2[u].count(w)) qu.pb(w);
        for(int z:out[v][w])
            out[u][w].insert(z), in[w][u].insert(z);
    }
    for(int w:in2[v])
        if(w!=u) if(out2[u].count(w)) qu.pb(w);
    ans-=sz(in[v][u])*sz[v]+sz(in[u][v])*sz[u];
    sz[u]+=sz[v];
    dsu[v]=u;
    in[u][v].clear();
    out[u][v].clear();
    in[v][u].clear();
    out[v][u].clear();
    in2[v].clear();
    in[v].clear();
    out2[v].clear();
    out[v].clear();
    for(int q:qu) connect(u,q);
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++) dsu.pb(i), sz.pb(1);
    for(int i=0;i<m;i++)
    {
        int u,v; cin>>u>>v;
        u--,v--;
        int paru=getp(u), parv=getp(v);
        if(paru==parv || in[parv][paru].count(u))
        {
            cout<<ans<<'\n';
            continue;
        }
        if(in2[paru].count(parv))
        {
            connect(paru,parv);
            cout<<ans<<'\n';
            continue;
        }
        in2[parv].insert(paru);
        out2[paru].insert(parv);
        in[parv][paru].insert(u), acc[parv]++;
        out[paru][parv].insert(u);
        ans+=sz[parv];
        cout<<ans<<'\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19968 KB Output is correct
2 Correct 16 ms 19968 KB Output is correct
3 Correct 16 ms 19968 KB Output is correct
4 Correct 16 ms 19968 KB Output is correct
5 Correct 16 ms 19968 KB Output is correct
6 Correct 16 ms 19968 KB Output is correct
7 Correct 16 ms 19968 KB Output is correct
8 Correct 16 ms 19968 KB Output is correct
9 Correct 17 ms 19968 KB Output is correct
10 Correct 16 ms 19968 KB Output is correct
11 Correct 17 ms 19968 KB Output is correct
12 Correct 15 ms 19968 KB Output is correct
13 Correct 16 ms 19968 KB Output is correct
14 Correct 16 ms 19968 KB Output is correct
15 Correct 17 ms 19968 KB Output is correct
16 Correct 16 ms 19968 KB Output is correct
17 Correct 16 ms 19968 KB Output is correct
18 Correct 16 ms 19968 KB Output is correct
19 Correct 16 ms 19968 KB Output is correct
20 Correct 16 ms 19968 KB Output is correct
21 Correct 16 ms 19968 KB Output is correct
22 Correct 16 ms 19968 KB Output is correct
23 Correct 17 ms 19968 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 16 ms 19968 KB Output is correct
26 Correct 16 ms 19968 KB Output is correct
27 Correct 16 ms 19968 KB Output is correct
28 Correct 16 ms 19968 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 16 ms 19968 KB Output is correct
31 Correct 16 ms 19968 KB Output is correct
32 Correct 16 ms 19944 KB Output is correct
33 Correct 17 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19968 KB Output is correct
2 Correct 16 ms 19968 KB Output is correct
3 Correct 16 ms 19968 KB Output is correct
4 Correct 16 ms 19968 KB Output is correct
5 Correct 16 ms 19968 KB Output is correct
6 Correct 16 ms 19968 KB Output is correct
7 Correct 16 ms 19968 KB Output is correct
8 Correct 16 ms 19968 KB Output is correct
9 Correct 17 ms 19968 KB Output is correct
10 Correct 16 ms 19968 KB Output is correct
11 Correct 17 ms 19968 KB Output is correct
12 Correct 15 ms 19968 KB Output is correct
13 Correct 16 ms 19968 KB Output is correct
14 Correct 16 ms 19968 KB Output is correct
15 Correct 17 ms 19968 KB Output is correct
16 Correct 16 ms 19968 KB Output is correct
17 Correct 16 ms 19968 KB Output is correct
18 Correct 16 ms 19968 KB Output is correct
19 Correct 16 ms 19968 KB Output is correct
20 Correct 16 ms 19968 KB Output is correct
21 Correct 16 ms 19968 KB Output is correct
22 Correct 16 ms 19968 KB Output is correct
23 Correct 17 ms 19968 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 16 ms 19968 KB Output is correct
26 Correct 16 ms 19968 KB Output is correct
27 Correct 16 ms 19968 KB Output is correct
28 Correct 16 ms 19968 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 16 ms 19968 KB Output is correct
31 Correct 16 ms 19968 KB Output is correct
32 Correct 16 ms 19944 KB Output is correct
33 Correct 17 ms 19968 KB Output is correct
34 Correct 19 ms 20224 KB Output is correct
35 Correct 115 ms 25848 KB Output is correct
36 Correct 184 ms 31992 KB Output is correct
37 Correct 171 ms 32248 KB Output is correct
38 Correct 159 ms 31096 KB Output is correct
39 Correct 20 ms 20352 KB Output is correct
40 Correct 20 ms 20480 KB Output is correct
41 Correct 20 ms 20480 KB Output is correct
42 Correct 19 ms 20352 KB Output is correct
43 Correct 21 ms 20480 KB Output is correct
44 Correct 21 ms 20608 KB Output is correct
45 Correct 20 ms 20352 KB Output is correct
46 Correct 19 ms 20352 KB Output is correct
47 Correct 21 ms 20480 KB Output is correct
48 Correct 21 ms 20480 KB Output is correct
49 Correct 33 ms 22520 KB Output is correct
50 Correct 168 ms 32480 KB Output is correct
51 Correct 26 ms 20992 KB Output is correct
52 Correct 129 ms 27896 KB Output is correct
53 Correct 32 ms 22264 KB Output is correct
54 Correct 148 ms 30200 KB Output is correct
55 Correct 26 ms 21504 KB Output is correct
56 Correct 25 ms 21504 KB Output is correct
57 Correct 26 ms 21888 KB Output is correct
58 Correct 25 ms 21888 KB Output is correct
59 Correct 22 ms 20096 KB Output is correct
60 Correct 97 ms 22648 KB Output is correct
61 Correct 23 ms 20736 KB Output is correct
62 Correct 162 ms 30704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19968 KB Output is correct
2 Correct 16 ms 19968 KB Output is correct
3 Correct 16 ms 19968 KB Output is correct
4 Correct 16 ms 19968 KB Output is correct
5 Correct 16 ms 19968 KB Output is correct
6 Correct 16 ms 19968 KB Output is correct
7 Correct 16 ms 19968 KB Output is correct
8 Correct 16 ms 19968 KB Output is correct
9 Correct 17 ms 19968 KB Output is correct
10 Correct 16 ms 19968 KB Output is correct
11 Correct 17 ms 19968 KB Output is correct
12 Correct 15 ms 19968 KB Output is correct
13 Correct 16 ms 19968 KB Output is correct
14 Correct 16 ms 19968 KB Output is correct
15 Correct 17 ms 19968 KB Output is correct
16 Correct 16 ms 19968 KB Output is correct
17 Correct 16 ms 19968 KB Output is correct
18 Correct 16 ms 19968 KB Output is correct
19 Correct 16 ms 19968 KB Output is correct
20 Correct 16 ms 19968 KB Output is correct
21 Correct 16 ms 19968 KB Output is correct
22 Correct 16 ms 19968 KB Output is correct
23 Correct 17 ms 19968 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 16 ms 19968 KB Output is correct
26 Correct 16 ms 19968 KB Output is correct
27 Correct 16 ms 19968 KB Output is correct
28 Correct 16 ms 19968 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 16 ms 19968 KB Output is correct
31 Correct 16 ms 19968 KB Output is correct
32 Correct 16 ms 19944 KB Output is correct
33 Correct 17 ms 19968 KB Output is correct
34 Correct 19 ms 20224 KB Output is correct
35 Correct 115 ms 25848 KB Output is correct
36 Correct 184 ms 31992 KB Output is correct
37 Correct 171 ms 32248 KB Output is correct
38 Correct 159 ms 31096 KB Output is correct
39 Correct 20 ms 20352 KB Output is correct
40 Correct 20 ms 20480 KB Output is correct
41 Correct 20 ms 20480 KB Output is correct
42 Correct 19 ms 20352 KB Output is correct
43 Correct 21 ms 20480 KB Output is correct
44 Correct 21 ms 20608 KB Output is correct
45 Correct 20 ms 20352 KB Output is correct
46 Correct 19 ms 20352 KB Output is correct
47 Correct 21 ms 20480 KB Output is correct
48 Correct 21 ms 20480 KB Output is correct
49 Correct 33 ms 22520 KB Output is correct
50 Correct 168 ms 32480 KB Output is correct
51 Correct 26 ms 20992 KB Output is correct
52 Correct 129 ms 27896 KB Output is correct
53 Correct 32 ms 22264 KB Output is correct
54 Correct 148 ms 30200 KB Output is correct
55 Correct 26 ms 21504 KB Output is correct
56 Correct 25 ms 21504 KB Output is correct
57 Correct 26 ms 21888 KB Output is correct
58 Correct 25 ms 21888 KB Output is correct
59 Correct 22 ms 20096 KB Output is correct
60 Correct 97 ms 22648 KB Output is correct
61 Correct 23 ms 20736 KB Output is correct
62 Correct 162 ms 30704 KB Output is correct
63 Correct 752 ms 136244 KB Output is correct
64 Correct 768 ms 136300 KB Output is correct
65 Correct 798 ms 136368 KB Output is correct
66 Correct 356 ms 42612 KB Output is correct
67 Correct 494 ms 49508 KB Output is correct
68 Correct 360 ms 42340 KB Output is correct
69 Correct 751 ms 49648 KB Output is correct
70 Correct 366 ms 42340 KB Output is correct
71 Correct 364 ms 42468 KB Output is correct
72 Correct 598 ms 49448 KB Output is correct
73 Correct 584 ms 49252 KB Output is correct
74 Correct 1737 ms 90272 KB Output is correct
75 Correct 1024 ms 59952 KB Output is correct
76 Correct 1223 ms 81664 KB Output is correct
77 Correct 1246 ms 82404 KB Output is correct
78 Correct 391 ms 51428 KB Output is correct
79 Correct 678 ms 57700 KB Output is correct
80 Correct 402 ms 53220 KB Output is correct
81 Correct 639 ms 57060 KB Output is correct
82 Correct 1294 ms 101604 KB Output is correct
83 Correct 1284 ms 101496 KB Output is correct
84 Correct 1289 ms 122872 KB Output is correct
85 Correct 1294 ms 122724 KB Output is correct
86 Correct 241 ms 34532 KB Output is correct
87 Correct 277 ms 36836 KB Output is correct
88 Correct 604 ms 51716 KB Output is correct
89 Correct 1177 ms 80924 KB Output is correct