Submission #612407

# Submission time Handle Problem Language Result Execution time Memory
612407 2022-07-29T14:25:30 Z alireza_kaviani Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
953 ms 107732 KB
#include <bits/stdc++.h>
using namespace std;

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

#define all(x)      (x).begin(),(x).end()
#define X           first
#define Y           second
#define sep         ' '
#define endl        '\n'
#define SZ(x)       ll(x.size())

const ll MAXN = 1e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;

ll n , m , ans , sz[MAXN] , par[MAXN];
set<int> inDeg[MAXN] , outDeg[MAXN];
vector<pii> Q;

int Find(int v){
    return (par[v] == -1 ? v : par[v] = Find(par[v]));
}

void Union(int v , int u){
    v = Find(v); u = Find(u);
    if(v == u)  return;
    if(sz[v] < sz[u])   swap(v , u);
    ans -= sz[v] * SZ(inDeg[v]) + sz[u] * SZ(inDeg[u]);
    par[u] = v; sz[v] += sz[u];
    for(auto &i : inDeg[u]){
        inDeg[v].insert(i);
        outDeg[Find(i)].insert(v);
        if(Find(i) != v && outDeg[v].find(Find(i)) != outDeg[v].end()){
            Q.push_back({v , i});
        }
    }
    for(auto &i : outDeg[u]){
        outDeg[v].insert(i);
        if(Find(i) != v && outDeg[Find(i)].find(v) != outDeg[Find(i)].end()){
            Q.push_back({v , i});
        }
    }
    ans += sz[v] * SZ(inDeg[v]);
}

void Add(int v , int u){
    u = Find(u);
    if(Find(v) == u)  return;
    if(inDeg[u].find(v) != inDeg[u].end()){
        return;
    }
    if(outDeg[u].find(Find(v)) != outDeg[u].end()){
        Q.push_back({v , u});
        while(!Q.empty()){
            pii A = Q.back(); Q.pop_back();
            Union(A.X , A.Y);
        }
        return;
    }
    inDeg[u].insert(v);
    outDeg[Find(v)].insert(u);
    ans += sz[u];
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    fill(par , par + MAXN , -1);
    fill(sz , sz + MAXN , 1);
    for(int i = 0 ; i < MAXN ; i++){
        inDeg[i].insert(i);
        outDeg[i].insert(i);
    }

    cin >> n >> m;
    for(int i = 0 ; i < m ; i++){
        int v , u;
        cin >> v >> u;
        Add(v , u);
        cout << ans << endl;
    }

    return 0;
}
/*

*/
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20564 KB Output is correct
2 Correct 19 ms 20600 KB Output is correct
3 Correct 16 ms 20584 KB Output is correct
4 Correct 17 ms 20580 KB Output is correct
5 Correct 16 ms 20672 KB Output is correct
6 Correct 16 ms 20564 KB Output is correct
7 Correct 17 ms 20688 KB Output is correct
8 Correct 21 ms 20692 KB Output is correct
9 Correct 18 ms 20692 KB Output is correct
10 Correct 20 ms 20676 KB Output is correct
11 Correct 20 ms 20684 KB Output is correct
12 Correct 17 ms 20672 KB Output is correct
13 Correct 16 ms 20568 KB Output is correct
14 Correct 17 ms 20632 KB Output is correct
15 Correct 16 ms 20564 KB Output is correct
16 Correct 17 ms 20676 KB Output is correct
17 Correct 15 ms 20564 KB Output is correct
18 Correct 18 ms 20564 KB Output is correct
19 Correct 15 ms 20664 KB Output is correct
20 Correct 15 ms 20692 KB Output is correct
21 Correct 18 ms 20736 KB Output is correct
22 Correct 17 ms 20660 KB Output is correct
23 Correct 18 ms 20692 KB Output is correct
24 Correct 17 ms 20692 KB Output is correct
25 Correct 21 ms 20696 KB Output is correct
26 Correct 21 ms 20664 KB Output is correct
27 Correct 18 ms 20676 KB Output is correct
28 Correct 19 ms 20660 KB Output is correct
29 Correct 16 ms 20608 KB Output is correct
30 Correct 18 ms 20660 KB Output is correct
31 Correct 18 ms 20748 KB Output is correct
32 Correct 18 ms 20592 KB Output is correct
33 Correct 17 ms 20644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20564 KB Output is correct
2 Correct 19 ms 20600 KB Output is correct
3 Correct 16 ms 20584 KB Output is correct
4 Correct 17 ms 20580 KB Output is correct
5 Correct 16 ms 20672 KB Output is correct
6 Correct 16 ms 20564 KB Output is correct
7 Correct 17 ms 20688 KB Output is correct
8 Correct 21 ms 20692 KB Output is correct
9 Correct 18 ms 20692 KB Output is correct
10 Correct 20 ms 20676 KB Output is correct
11 Correct 20 ms 20684 KB Output is correct
12 Correct 17 ms 20672 KB Output is correct
13 Correct 16 ms 20568 KB Output is correct
14 Correct 17 ms 20632 KB Output is correct
15 Correct 16 ms 20564 KB Output is correct
16 Correct 17 ms 20676 KB Output is correct
17 Correct 15 ms 20564 KB Output is correct
18 Correct 18 ms 20564 KB Output is correct
19 Correct 15 ms 20664 KB Output is correct
20 Correct 15 ms 20692 KB Output is correct
21 Correct 18 ms 20736 KB Output is correct
22 Correct 17 ms 20660 KB Output is correct
23 Correct 18 ms 20692 KB Output is correct
24 Correct 17 ms 20692 KB Output is correct
25 Correct 21 ms 20696 KB Output is correct
26 Correct 21 ms 20664 KB Output is correct
27 Correct 18 ms 20676 KB Output is correct
28 Correct 19 ms 20660 KB Output is correct
29 Correct 16 ms 20608 KB Output is correct
30 Correct 18 ms 20660 KB Output is correct
31 Correct 18 ms 20748 KB Output is correct
32 Correct 18 ms 20592 KB Output is correct
33 Correct 17 ms 20644 KB Output is correct
34 Correct 27 ms 20796 KB Output is correct
35 Correct 86 ms 26072 KB Output is correct
36 Correct 109 ms 28396 KB Output is correct
37 Correct 122 ms 28416 KB Output is correct
38 Correct 111 ms 28176 KB Output is correct
39 Correct 21 ms 20936 KB Output is correct
40 Correct 26 ms 21748 KB Output is correct
41 Correct 27 ms 21568 KB Output is correct
42 Correct 18 ms 20944 KB Output is correct
43 Correct 19 ms 20932 KB Output is correct
44 Correct 18 ms 20932 KB Output is correct
45 Correct 19 ms 20900 KB Output is correct
46 Correct 19 ms 21004 KB Output is correct
47 Correct 20 ms 21324 KB Output is correct
48 Correct 19 ms 21400 KB Output is correct
49 Correct 25 ms 21972 KB Output is correct
50 Correct 109 ms 28628 KB Output is correct
51 Correct 21 ms 21452 KB Output is correct
52 Correct 94 ms 27184 KB Output is correct
53 Correct 24 ms 21724 KB Output is correct
54 Correct 93 ms 27924 KB Output is correct
55 Correct 19 ms 21332 KB Output is correct
56 Correct 19 ms 21308 KB Output is correct
57 Correct 19 ms 21324 KB Output is correct
58 Correct 24 ms 21292 KB Output is correct
59 Correct 22 ms 21792 KB Output is correct
60 Correct 113 ms 26716 KB Output is correct
61 Correct 20 ms 21332 KB Output is correct
62 Correct 103 ms 28056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20564 KB Output is correct
2 Correct 19 ms 20600 KB Output is correct
3 Correct 16 ms 20584 KB Output is correct
4 Correct 17 ms 20580 KB Output is correct
5 Correct 16 ms 20672 KB Output is correct
6 Correct 16 ms 20564 KB Output is correct
7 Correct 17 ms 20688 KB Output is correct
8 Correct 21 ms 20692 KB Output is correct
9 Correct 18 ms 20692 KB Output is correct
10 Correct 20 ms 20676 KB Output is correct
11 Correct 20 ms 20684 KB Output is correct
12 Correct 17 ms 20672 KB Output is correct
13 Correct 16 ms 20568 KB Output is correct
14 Correct 17 ms 20632 KB Output is correct
15 Correct 16 ms 20564 KB Output is correct
16 Correct 17 ms 20676 KB Output is correct
17 Correct 15 ms 20564 KB Output is correct
18 Correct 18 ms 20564 KB Output is correct
19 Correct 15 ms 20664 KB Output is correct
20 Correct 15 ms 20692 KB Output is correct
21 Correct 18 ms 20736 KB Output is correct
22 Correct 17 ms 20660 KB Output is correct
23 Correct 18 ms 20692 KB Output is correct
24 Correct 17 ms 20692 KB Output is correct
25 Correct 21 ms 20696 KB Output is correct
26 Correct 21 ms 20664 KB Output is correct
27 Correct 18 ms 20676 KB Output is correct
28 Correct 19 ms 20660 KB Output is correct
29 Correct 16 ms 20608 KB Output is correct
30 Correct 18 ms 20660 KB Output is correct
31 Correct 18 ms 20748 KB Output is correct
32 Correct 18 ms 20592 KB Output is correct
33 Correct 17 ms 20644 KB Output is correct
34 Correct 27 ms 20796 KB Output is correct
35 Correct 86 ms 26072 KB Output is correct
36 Correct 109 ms 28396 KB Output is correct
37 Correct 122 ms 28416 KB Output is correct
38 Correct 111 ms 28176 KB Output is correct
39 Correct 21 ms 20936 KB Output is correct
40 Correct 26 ms 21748 KB Output is correct
41 Correct 27 ms 21568 KB Output is correct
42 Correct 18 ms 20944 KB Output is correct
43 Correct 19 ms 20932 KB Output is correct
44 Correct 18 ms 20932 KB Output is correct
45 Correct 19 ms 20900 KB Output is correct
46 Correct 19 ms 21004 KB Output is correct
47 Correct 20 ms 21324 KB Output is correct
48 Correct 19 ms 21400 KB Output is correct
49 Correct 25 ms 21972 KB Output is correct
50 Correct 109 ms 28628 KB Output is correct
51 Correct 21 ms 21452 KB Output is correct
52 Correct 94 ms 27184 KB Output is correct
53 Correct 24 ms 21724 KB Output is correct
54 Correct 93 ms 27924 KB Output is correct
55 Correct 19 ms 21332 KB Output is correct
56 Correct 19 ms 21308 KB Output is correct
57 Correct 19 ms 21324 KB Output is correct
58 Correct 24 ms 21292 KB Output is correct
59 Correct 22 ms 21792 KB Output is correct
60 Correct 113 ms 26716 KB Output is correct
61 Correct 20 ms 21332 KB Output is correct
62 Correct 103 ms 28056 KB Output is correct
63 Correct 403 ms 54212 KB Output is correct
64 Correct 424 ms 54316 KB Output is correct
65 Correct 392 ms 54348 KB Output is correct
66 Correct 283 ms 39056 KB Output is correct
67 Correct 514 ms 90416 KB Output is correct
68 Correct 274 ms 39088 KB Output is correct
69 Correct 351 ms 39272 KB Output is correct
70 Correct 294 ms 39112 KB Output is correct
71 Correct 282 ms 39212 KB Output is correct
72 Correct 536 ms 58952 KB Output is correct
73 Correct 604 ms 62268 KB Output is correct
74 Correct 953 ms 84800 KB Output is correct
75 Correct 532 ms 46200 KB Output is correct
76 Correct 674 ms 63392 KB Output is correct
77 Correct 685 ms 63592 KB Output is correct
78 Correct 219 ms 39264 KB Output is correct
79 Correct 479 ms 47216 KB Output is correct
80 Correct 218 ms 38092 KB Output is correct
81 Correct 374 ms 44340 KB Output is correct
82 Correct 595 ms 57016 KB Output is correct
83 Correct 544 ms 57068 KB Output is correct
84 Correct 499 ms 57096 KB Output is correct
85 Correct 577 ms 57136 KB Output is correct
86 Correct 476 ms 105512 KB Output is correct
87 Correct 464 ms 107732 KB Output is correct
88 Correct 564 ms 60628 KB Output is correct
89 Correct 666 ms 61596 KB Output is correct