Submission #387284

# Submission time Handle Problem Language Result Execution time Memory
387284 2021-04-08T08:38:06 Z cheissmart Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
2185 ms 107416 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define V vector
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "LINE(" << __LINE__ << ") ->" << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;

set<int> G[N], rG[N], ch[N];
int p[N], sz[N];
queue<pi> q;
ll ans = 0;

int find(int u) {
    return p[u] == u ? u : p[u] = find(p[u]);
}

void add_edge(int u, int v) {
    G[u].insert(v);
    rG[v].insert(u);
    if(G[v].count(u)) {
        q.push({u, v});
    }
}

int get_size(int u) {
    return G[u].size() + rG[u].size() + ch[u].size();
}

void unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return;
    if(get_size(x) > get_size(y)) swap(x, y);
    ans += (ll) sz[x] * ch[y].size() + (ll) sz[y] * ch[x].size();
    p[x] = y;
    sz[y] += sz[x];
    // merge x into y
    for(int i:ch[x]) {
        if(ch[y].count(i)) ans -= sz[y];
        else ch[y].insert(i);
    }
    G[x].erase(y), G[y].erase(x);
    rG[x].erase(y), rG[y].erase(x);
    for(int i:G[x]) {
        G[y].insert(i);
        add_edge(y, i);
    }
    for(int i:rG[x]) {
        rG[y].insert(i);
        add_edge(i, y);
    }
}

signed main()
{
	ios::sync_with_stdio(0), cin.tie(0);

    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        p[i] = i, sz[i] = 1;
        ch[i].insert(i);
    }
    for(int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        v = find(v);
        if(find(u) != v && ch[v].count(u) == 0) {
            ch[v].insert(u);
            ans += sz[v]; // u will connect to everyone in v's component
            u = find(u);
            add_edge(u, v);
            while(q.size()) {
                pi p = q.front(); q.pop();
                unite(p.F, p.S);
            }
        }
        cout << ans << '\n';
    }

}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 11 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 11 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 11 ms 14572 KB Output is correct
9 Correct 11 ms 14572 KB Output is correct
10 Correct 11 ms 14444 KB Output is correct
11 Correct 11 ms 14444 KB Output is correct
12 Correct 11 ms 14444 KB Output is correct
13 Correct 11 ms 14444 KB Output is correct
14 Correct 11 ms 14444 KB Output is correct
15 Correct 11 ms 14592 KB Output is correct
16 Correct 11 ms 14444 KB Output is correct
17 Correct 11 ms 14444 KB Output is correct
18 Correct 11 ms 14588 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 11 ms 14444 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 11 ms 14444 KB Output is correct
23 Correct 12 ms 14444 KB Output is correct
24 Correct 11 ms 14444 KB Output is correct
25 Correct 11 ms 14572 KB Output is correct
26 Correct 11 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 11 ms 14444 KB Output is correct
30 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 11 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 11 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 11 ms 14572 KB Output is correct
9 Correct 11 ms 14572 KB Output is correct
10 Correct 11 ms 14444 KB Output is correct
11 Correct 11 ms 14444 KB Output is correct
12 Correct 11 ms 14444 KB Output is correct
13 Correct 11 ms 14444 KB Output is correct
14 Correct 11 ms 14444 KB Output is correct
15 Correct 11 ms 14592 KB Output is correct
16 Correct 11 ms 14444 KB Output is correct
17 Correct 11 ms 14444 KB Output is correct
18 Correct 11 ms 14588 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 11 ms 14444 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 11 ms 14444 KB Output is correct
23 Correct 12 ms 14444 KB Output is correct
24 Correct 11 ms 14444 KB Output is correct
25 Correct 11 ms 14572 KB Output is correct
26 Correct 11 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 11 ms 14444 KB Output is correct
30 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14572 KB Output is correct
34 Correct 17 ms 14700 KB Output is correct
35 Correct 125 ms 20588 KB Output is correct
36 Correct 152 ms 23728 KB Output is correct
37 Correct 147 ms 23916 KB Output is correct
38 Correct 141 ms 23404 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 18 ms 15852 KB Output is correct
41 Correct 18 ms 15596 KB Output is correct
42 Correct 13 ms 14828 KB Output is correct
43 Correct 15 ms 14828 KB Output is correct
44 Correct 15 ms 14828 KB Output is correct
45 Correct 14 ms 14828 KB Output is correct
46 Correct 15 ms 14828 KB Output is correct
47 Correct 17 ms 15212 KB Output is correct
48 Correct 16 ms 15340 KB Output is correct
49 Correct 28 ms 16236 KB Output is correct
50 Correct 159 ms 24044 KB Output is correct
51 Correct 21 ms 15340 KB Output is correct
52 Correct 123 ms 21996 KB Output is correct
53 Correct 27 ms 16108 KB Output is correct
54 Correct 141 ms 23000 KB Output is correct
55 Correct 20 ms 15468 KB Output is correct
56 Correct 19 ms 15468 KB Output is correct
57 Correct 19 ms 15468 KB Output is correct
58 Correct 19 ms 15468 KB Output is correct
59 Correct 16 ms 15212 KB Output is correct
60 Correct 102 ms 20332 KB Output is correct
61 Correct 17 ms 15340 KB Output is correct
62 Correct 145 ms 23148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 11 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 11 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 11 ms 14572 KB Output is correct
9 Correct 11 ms 14572 KB Output is correct
10 Correct 11 ms 14444 KB Output is correct
11 Correct 11 ms 14444 KB Output is correct
12 Correct 11 ms 14444 KB Output is correct
13 Correct 11 ms 14444 KB Output is correct
14 Correct 11 ms 14444 KB Output is correct
15 Correct 11 ms 14592 KB Output is correct
16 Correct 11 ms 14444 KB Output is correct
17 Correct 11 ms 14444 KB Output is correct
18 Correct 11 ms 14588 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 11 ms 14444 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 11 ms 14444 KB Output is correct
23 Correct 12 ms 14444 KB Output is correct
24 Correct 11 ms 14444 KB Output is correct
25 Correct 11 ms 14572 KB Output is correct
26 Correct 11 ms 14444 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 10 ms 14444 KB Output is correct
29 Correct 11 ms 14444 KB Output is correct
30 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 10 ms 14444 KB Output is correct
33 Correct 11 ms 14572 KB Output is correct
34 Correct 17 ms 14700 KB Output is correct
35 Correct 125 ms 20588 KB Output is correct
36 Correct 152 ms 23728 KB Output is correct
37 Correct 147 ms 23916 KB Output is correct
38 Correct 141 ms 23404 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 18 ms 15852 KB Output is correct
41 Correct 18 ms 15596 KB Output is correct
42 Correct 13 ms 14828 KB Output is correct
43 Correct 15 ms 14828 KB Output is correct
44 Correct 15 ms 14828 KB Output is correct
45 Correct 14 ms 14828 KB Output is correct
46 Correct 15 ms 14828 KB Output is correct
47 Correct 17 ms 15212 KB Output is correct
48 Correct 16 ms 15340 KB Output is correct
49 Correct 28 ms 16236 KB Output is correct
50 Correct 159 ms 24044 KB Output is correct
51 Correct 21 ms 15340 KB Output is correct
52 Correct 123 ms 21996 KB Output is correct
53 Correct 27 ms 16108 KB Output is correct
54 Correct 141 ms 23000 KB Output is correct
55 Correct 20 ms 15468 KB Output is correct
56 Correct 19 ms 15468 KB Output is correct
57 Correct 19 ms 15468 KB Output is correct
58 Correct 19 ms 15468 KB Output is correct
59 Correct 16 ms 15212 KB Output is correct
60 Correct 102 ms 20332 KB Output is correct
61 Correct 17 ms 15340 KB Output is correct
62 Correct 145 ms 23148 KB Output is correct
63 Correct 653 ms 67824 KB Output is correct
64 Correct 693 ms 67692 KB Output is correct
65 Correct 664 ms 67820 KB Output is correct
66 Correct 299 ms 33772 KB Output is correct
67 Correct 951 ms 96748 KB Output is correct
68 Correct 286 ms 33772 KB Output is correct
69 Correct 584 ms 35052 KB Output is correct
70 Correct 322 ms 33900 KB Output is correct
71 Correct 334 ms 33772 KB Output is correct
72 Correct 780 ms 54220 KB Output is correct
73 Correct 765 ms 57196 KB Output is correct
74 Correct 2185 ms 107416 KB Output is correct
75 Correct 937 ms 46316 KB Output is correct
76 Correct 1418 ms 70764 KB Output is correct
77 Correct 1457 ms 71148 KB Output is correct
78 Correct 365 ms 40940 KB Output is correct
79 Correct 648 ms 46828 KB Output is correct
80 Correct 318 ms 39660 KB Output is correct
81 Correct 543 ms 43884 KB Output is correct
82 Correct 1532 ms 66012 KB Output is correct
83 Correct 1496 ms 66188 KB Output is correct
84 Correct 1227 ms 68876 KB Output is correct
85 Correct 1218 ms 69100 KB Output is correct
86 Correct 458 ms 67060 KB Output is correct
87 Correct 460 ms 68784 KB Output is correct
88 Correct 789 ms 55532 KB Output is correct
89 Correct 1351 ms 68076 KB Output is correct