Submission #260599

# Submission time Handle Problem Language Result Execution time Memory
260599 2020-08-10T15:00:59 Z doowey Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1578 ms 108424 KB
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)2e5 + 10;
vector<int> nds[N];
set<int> out[N];
set<int> in[N];
set<int> so[N];
set<int> si[N];
int par[N];
int sz[N];

ll res = 0;

int fin(int x){
    if(par[x]==x)
        return x;
    return par[x]=fin(par[x]);
}

ll f(int x){
    return x * 1ll * (x - 1);
}

void join(int a, int b){
    a = fin(a);
    b = fin(b);
    if(a == b)
        return;
    if(sz[a] > sz[b]) swap(a, b);
    res -= f(sz[a]);
    res -= f(sz[b]);
    res -= in[b].size() * 1ll * sz[b];
    res -= in[a].size() * 1ll * sz[a];
    for(auto v : nds[a]){
        nds[b].push_back(v);
        auto it = out[v].lower_bound(b);
        if(it != out[v].end() && (*it) == b){
            in[b].erase(v);
            out[v].erase(it);
        }
    }
    nds[a].clear();
    for(auto y : in[a]){
        if(fin(y) == b){
            out[y].erase(a);
        }
        else{
            out[y].erase(a);
            out[y].insert(b);
            in[b].insert(y);
        }
    }
    in[a].clear();
    vector<pii> nxi;
    for(auto v : so[a]){
        if(v == b){
            si[v].erase(a);
            continue;
        }
        else{
            si[v].erase(a);
            si[v].insert(b);
            so[b].insert(v);
            if(so[b].count(v) && so[v].count(b)){
                nxi.push_back(mp(v, b));
            }
        }
    }
    so[a].clear();
    for(auto v : si[a]){
        if(v == b){
            so[v].erase(a);
            continue;
        }
        else{
            so[v].erase(a);
            so[v].insert(b);
            si[b].insert(v);
            if(so[b].count(v) && so[v].count(b)){
                nxi.push_back(mp(v, b));
            }
        }
    }
    si[a].clear();
    par[a] = b;
    sz[b] += sz[a];
    res += in[b].size() * 1ll * sz[b];
    res += f(sz[b]);
    for(auto qq : nxi)
        join(qq.fi, qq.se);
}

void add_edge(int u, int v){
    int uf = fin(u);
    int vf = fin(v);
    if(uf == vf) return;
    so[uf].insert(vf);
    si[vf].insert(uf);
    if(out[u].count(vf)){
        return;
    }
    res += sz[vf];
    out[u].insert(vf);
    in[vf].insert(u);

    if(so[uf].count(vf) && so[vf].count(uf)){
        join(uf, vf);
    }
}

int main(){
    fastIO;
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n ; i ++ ){
        nds[i].push_back(i);
        par[i]=i;
        sz[i]=1;
    }
    int u, v;
    for(int i = 0; i < m ; i ++ ){
        cin >> u >> v;
        add_edge(u, v);
        cout << res << "\n";
    }
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 25 ms 42576 KB Output is correct
2 Correct 25 ms 42624 KB Output is correct
3 Correct 24 ms 42624 KB Output is correct
4 Correct 25 ms 42624 KB Output is correct
5 Correct 25 ms 42624 KB Output is correct
6 Correct 26 ms 42624 KB Output is correct
7 Correct 31 ms 42624 KB Output is correct
8 Correct 26 ms 42752 KB Output is correct
9 Correct 39 ms 42744 KB Output is correct
10 Correct 25 ms 42624 KB Output is correct
11 Correct 33 ms 42616 KB Output is correct
12 Correct 25 ms 42624 KB Output is correct
13 Correct 67 ms 42616 KB Output is correct
14 Correct 30 ms 42624 KB Output is correct
15 Correct 32 ms 42624 KB Output is correct
16 Correct 25 ms 42624 KB Output is correct
17 Correct 26 ms 42624 KB Output is correct
18 Correct 30 ms 42616 KB Output is correct
19 Correct 31 ms 42624 KB Output is correct
20 Correct 27 ms 42624 KB Output is correct
21 Correct 26 ms 42752 KB Output is correct
22 Correct 29 ms 42624 KB Output is correct
23 Correct 25 ms 42624 KB Output is correct
24 Correct 27 ms 42616 KB Output is correct
25 Correct 27 ms 42624 KB Output is correct
26 Correct 31 ms 42624 KB Output is correct
27 Correct 25 ms 42624 KB Output is correct
28 Correct 25 ms 42616 KB Output is correct
29 Correct 32 ms 42616 KB Output is correct
30 Correct 25 ms 42624 KB Output is correct
31 Correct 39 ms 42616 KB Output is correct
32 Correct 28 ms 42624 KB Output is correct
33 Correct 26 ms 42624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 42576 KB Output is correct
2 Correct 25 ms 42624 KB Output is correct
3 Correct 24 ms 42624 KB Output is correct
4 Correct 25 ms 42624 KB Output is correct
5 Correct 25 ms 42624 KB Output is correct
6 Correct 26 ms 42624 KB Output is correct
7 Correct 31 ms 42624 KB Output is correct
8 Correct 26 ms 42752 KB Output is correct
9 Correct 39 ms 42744 KB Output is correct
10 Correct 25 ms 42624 KB Output is correct
11 Correct 33 ms 42616 KB Output is correct
12 Correct 25 ms 42624 KB Output is correct
13 Correct 67 ms 42616 KB Output is correct
14 Correct 30 ms 42624 KB Output is correct
15 Correct 32 ms 42624 KB Output is correct
16 Correct 25 ms 42624 KB Output is correct
17 Correct 26 ms 42624 KB Output is correct
18 Correct 30 ms 42616 KB Output is correct
19 Correct 31 ms 42624 KB Output is correct
20 Correct 27 ms 42624 KB Output is correct
21 Correct 26 ms 42752 KB Output is correct
22 Correct 29 ms 42624 KB Output is correct
23 Correct 25 ms 42624 KB Output is correct
24 Correct 27 ms 42616 KB Output is correct
25 Correct 27 ms 42624 KB Output is correct
26 Correct 31 ms 42624 KB Output is correct
27 Correct 25 ms 42624 KB Output is correct
28 Correct 25 ms 42616 KB Output is correct
29 Correct 32 ms 42616 KB Output is correct
30 Correct 25 ms 42624 KB Output is correct
31 Correct 39 ms 42616 KB Output is correct
32 Correct 28 ms 42624 KB Output is correct
33 Correct 26 ms 42624 KB Output is correct
34 Correct 29 ms 42872 KB Output is correct
35 Correct 130 ms 48912 KB Output is correct
36 Correct 179 ms 52320 KB Output is correct
37 Correct 183 ms 52420 KB Output is correct
38 Correct 159 ms 51960 KB Output is correct
39 Correct 35 ms 42752 KB Output is correct
40 Correct 35 ms 43008 KB Output is correct
41 Correct 32 ms 43008 KB Output is correct
42 Correct 28 ms 42752 KB Output is correct
43 Correct 31 ms 43008 KB Output is correct
44 Correct 30 ms 43008 KB Output is correct
45 Correct 28 ms 42752 KB Output is correct
46 Correct 32 ms 42752 KB Output is correct
47 Correct 30 ms 43008 KB Output is correct
48 Correct 30 ms 43008 KB Output is correct
49 Correct 39 ms 43904 KB Output is correct
50 Correct 171 ms 52472 KB Output is correct
51 Correct 35 ms 43264 KB Output is correct
52 Correct 147 ms 50424 KB Output is correct
53 Correct 38 ms 43776 KB Output is correct
54 Correct 164 ms 51440 KB Output is correct
55 Correct 43 ms 43516 KB Output is correct
56 Correct 39 ms 43512 KB Output is correct
57 Correct 32 ms 43776 KB Output is correct
58 Correct 45 ms 43768 KB Output is correct
59 Correct 34 ms 42880 KB Output is correct
60 Correct 131 ms 47916 KB Output is correct
61 Correct 36 ms 43000 KB Output is correct
62 Correct 174 ms 51704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 42576 KB Output is correct
2 Correct 25 ms 42624 KB Output is correct
3 Correct 24 ms 42624 KB Output is correct
4 Correct 25 ms 42624 KB Output is correct
5 Correct 25 ms 42624 KB Output is correct
6 Correct 26 ms 42624 KB Output is correct
7 Correct 31 ms 42624 KB Output is correct
8 Correct 26 ms 42752 KB Output is correct
9 Correct 39 ms 42744 KB Output is correct
10 Correct 25 ms 42624 KB Output is correct
11 Correct 33 ms 42616 KB Output is correct
12 Correct 25 ms 42624 KB Output is correct
13 Correct 67 ms 42616 KB Output is correct
14 Correct 30 ms 42624 KB Output is correct
15 Correct 32 ms 42624 KB Output is correct
16 Correct 25 ms 42624 KB Output is correct
17 Correct 26 ms 42624 KB Output is correct
18 Correct 30 ms 42616 KB Output is correct
19 Correct 31 ms 42624 KB Output is correct
20 Correct 27 ms 42624 KB Output is correct
21 Correct 26 ms 42752 KB Output is correct
22 Correct 29 ms 42624 KB Output is correct
23 Correct 25 ms 42624 KB Output is correct
24 Correct 27 ms 42616 KB Output is correct
25 Correct 27 ms 42624 KB Output is correct
26 Correct 31 ms 42624 KB Output is correct
27 Correct 25 ms 42624 KB Output is correct
28 Correct 25 ms 42616 KB Output is correct
29 Correct 32 ms 42616 KB Output is correct
30 Correct 25 ms 42624 KB Output is correct
31 Correct 39 ms 42616 KB Output is correct
32 Correct 28 ms 42624 KB Output is correct
33 Correct 26 ms 42624 KB Output is correct
34 Correct 29 ms 42872 KB Output is correct
35 Correct 130 ms 48912 KB Output is correct
36 Correct 179 ms 52320 KB Output is correct
37 Correct 183 ms 52420 KB Output is correct
38 Correct 159 ms 51960 KB Output is correct
39 Correct 35 ms 42752 KB Output is correct
40 Correct 35 ms 43008 KB Output is correct
41 Correct 32 ms 43008 KB Output is correct
42 Correct 28 ms 42752 KB Output is correct
43 Correct 31 ms 43008 KB Output is correct
44 Correct 30 ms 43008 KB Output is correct
45 Correct 28 ms 42752 KB Output is correct
46 Correct 32 ms 42752 KB Output is correct
47 Correct 30 ms 43008 KB Output is correct
48 Correct 30 ms 43008 KB Output is correct
49 Correct 39 ms 43904 KB Output is correct
50 Correct 171 ms 52472 KB Output is correct
51 Correct 35 ms 43264 KB Output is correct
52 Correct 147 ms 50424 KB Output is correct
53 Correct 38 ms 43776 KB Output is correct
54 Correct 164 ms 51440 KB Output is correct
55 Correct 43 ms 43516 KB Output is correct
56 Correct 39 ms 43512 KB Output is correct
57 Correct 32 ms 43776 KB Output is correct
58 Correct 45 ms 43768 KB Output is correct
59 Correct 34 ms 42880 KB Output is correct
60 Correct 131 ms 47916 KB Output is correct
61 Correct 36 ms 43000 KB Output is correct
62 Correct 174 ms 51704 KB Output is correct
63 Correct 756 ms 108424 KB Output is correct
64 Correct 769 ms 108412 KB Output is correct
65 Correct 731 ms 108284 KB Output is correct
66 Correct 225 ms 51440 KB Output is correct
67 Correct 497 ms 61424 KB Output is correct
68 Correct 231 ms 51492 KB Output is correct
69 Correct 694 ms 60656 KB Output is correct
70 Correct 245 ms 51436 KB Output is correct
71 Correct 241 ms 51572 KB Output is correct
72 Correct 561 ms 60796 KB Output is correct
73 Correct 758 ms 60808 KB Output is correct
74 Correct 1578 ms 75760 KB Output is correct
75 Correct 855 ms 67884 KB Output is correct
76 Correct 1016 ms 74616 KB Output is correct
77 Correct 1176 ms 74668 KB Output is correct
78 Correct 400 ms 61812 KB Output is correct
79 Correct 596 ms 65508 KB Output is correct
80 Correct 361 ms 61812 KB Output is correct
81 Correct 582 ms 65292 KB Output is correct
82 Correct 996 ms 88564 KB Output is correct
83 Correct 1012 ms 88436 KB Output is correct
84 Correct 883 ms 99200 KB Output is correct
85 Correct 882 ms 99368 KB Output is correct
86 Correct 296 ms 56172 KB Output is correct
87 Correct 341 ms 58444 KB Output is correct
88 Correct 556 ms 60920 KB Output is correct
89 Correct 985 ms 73764 KB Output is correct