답안 #387286

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387286 2021-04-08T08:41:05 Z cheissmart 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1750 ms 102280 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;

unordered_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() + sz[u];
}

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';
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16748 KB Output is correct
2 Correct 13 ms 16876 KB Output is correct
3 Correct 15 ms 16748 KB Output is correct
4 Correct 13 ms 16748 KB Output is correct
5 Correct 13 ms 16748 KB Output is correct
6 Correct 13 ms 16748 KB Output is correct
7 Correct 14 ms 16876 KB Output is correct
8 Correct 14 ms 16876 KB Output is correct
9 Correct 14 ms 16876 KB Output is correct
10 Correct 13 ms 16748 KB Output is correct
11 Correct 13 ms 16748 KB Output is correct
12 Correct 13 ms 16748 KB Output is correct
13 Correct 13 ms 16748 KB Output is correct
14 Correct 14 ms 16748 KB Output is correct
15 Correct 13 ms 16748 KB Output is correct
16 Correct 14 ms 16748 KB Output is correct
17 Correct 14 ms 16748 KB Output is correct
18 Correct 13 ms 16748 KB Output is correct
19 Correct 13 ms 16748 KB Output is correct
20 Correct 13 ms 16876 KB Output is correct
21 Correct 15 ms 16876 KB Output is correct
22 Correct 13 ms 16748 KB Output is correct
23 Correct 15 ms 16876 KB Output is correct
24 Correct 14 ms 16876 KB Output is correct
25 Correct 14 ms 16876 KB Output is correct
26 Correct 13 ms 16748 KB Output is correct
27 Correct 13 ms 16748 KB Output is correct
28 Correct 13 ms 16748 KB Output is correct
29 Correct 13 ms 16748 KB Output is correct
30 Correct 15 ms 16748 KB Output is correct
31 Correct 14 ms 16876 KB Output is correct
32 Correct 15 ms 17004 KB Output is correct
33 Correct 14 ms 16876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16748 KB Output is correct
2 Correct 13 ms 16876 KB Output is correct
3 Correct 15 ms 16748 KB Output is correct
4 Correct 13 ms 16748 KB Output is correct
5 Correct 13 ms 16748 KB Output is correct
6 Correct 13 ms 16748 KB Output is correct
7 Correct 14 ms 16876 KB Output is correct
8 Correct 14 ms 16876 KB Output is correct
9 Correct 14 ms 16876 KB Output is correct
10 Correct 13 ms 16748 KB Output is correct
11 Correct 13 ms 16748 KB Output is correct
12 Correct 13 ms 16748 KB Output is correct
13 Correct 13 ms 16748 KB Output is correct
14 Correct 14 ms 16748 KB Output is correct
15 Correct 13 ms 16748 KB Output is correct
16 Correct 14 ms 16748 KB Output is correct
17 Correct 14 ms 16748 KB Output is correct
18 Correct 13 ms 16748 KB Output is correct
19 Correct 13 ms 16748 KB Output is correct
20 Correct 13 ms 16876 KB Output is correct
21 Correct 15 ms 16876 KB Output is correct
22 Correct 13 ms 16748 KB Output is correct
23 Correct 15 ms 16876 KB Output is correct
24 Correct 14 ms 16876 KB Output is correct
25 Correct 14 ms 16876 KB Output is correct
26 Correct 13 ms 16748 KB Output is correct
27 Correct 13 ms 16748 KB Output is correct
28 Correct 13 ms 16748 KB Output is correct
29 Correct 13 ms 16748 KB Output is correct
30 Correct 15 ms 16748 KB Output is correct
31 Correct 14 ms 16876 KB Output is correct
32 Correct 15 ms 17004 KB Output is correct
33 Correct 14 ms 16876 KB Output is correct
34 Correct 16 ms 17004 KB Output is correct
35 Correct 104 ms 21740 KB Output is correct
36 Correct 133 ms 24428 KB Output is correct
37 Correct 131 ms 24556 KB Output is correct
38 Correct 127 ms 24172 KB Output is correct
39 Correct 16 ms 17260 KB Output is correct
40 Correct 21 ms 18028 KB Output is correct
41 Correct 21 ms 18028 KB Output is correct
42 Correct 16 ms 17260 KB Output is correct
43 Correct 17 ms 17260 KB Output is correct
44 Correct 17 ms 17260 KB Output is correct
45 Correct 17 ms 17260 KB Output is correct
46 Correct 17 ms 17260 KB Output is correct
47 Correct 19 ms 17644 KB Output is correct
48 Correct 19 ms 17644 KB Output is correct
49 Correct 27 ms 18540 KB Output is correct
50 Correct 133 ms 24772 KB Output is correct
51 Correct 22 ms 17772 KB Output is correct
52 Correct 120 ms 22764 KB Output is correct
53 Correct 30 ms 18412 KB Output is correct
54 Correct 123 ms 23788 KB Output is correct
55 Correct 20 ms 17772 KB Output is correct
56 Correct 21 ms 17772 KB Output is correct
57 Correct 20 ms 17900 KB Output is correct
58 Correct 20 ms 17900 KB Output is correct
59 Correct 19 ms 17644 KB Output is correct
60 Correct 104 ms 21228 KB Output is correct
61 Correct 21 ms 17644 KB Output is correct
62 Correct 132 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16748 KB Output is correct
2 Correct 13 ms 16876 KB Output is correct
3 Correct 15 ms 16748 KB Output is correct
4 Correct 13 ms 16748 KB Output is correct
5 Correct 13 ms 16748 KB Output is correct
6 Correct 13 ms 16748 KB Output is correct
7 Correct 14 ms 16876 KB Output is correct
8 Correct 14 ms 16876 KB Output is correct
9 Correct 14 ms 16876 KB Output is correct
10 Correct 13 ms 16748 KB Output is correct
11 Correct 13 ms 16748 KB Output is correct
12 Correct 13 ms 16748 KB Output is correct
13 Correct 13 ms 16748 KB Output is correct
14 Correct 14 ms 16748 KB Output is correct
15 Correct 13 ms 16748 KB Output is correct
16 Correct 14 ms 16748 KB Output is correct
17 Correct 14 ms 16748 KB Output is correct
18 Correct 13 ms 16748 KB Output is correct
19 Correct 13 ms 16748 KB Output is correct
20 Correct 13 ms 16876 KB Output is correct
21 Correct 15 ms 16876 KB Output is correct
22 Correct 13 ms 16748 KB Output is correct
23 Correct 15 ms 16876 KB Output is correct
24 Correct 14 ms 16876 KB Output is correct
25 Correct 14 ms 16876 KB Output is correct
26 Correct 13 ms 16748 KB Output is correct
27 Correct 13 ms 16748 KB Output is correct
28 Correct 13 ms 16748 KB Output is correct
29 Correct 13 ms 16748 KB Output is correct
30 Correct 15 ms 16748 KB Output is correct
31 Correct 14 ms 16876 KB Output is correct
32 Correct 15 ms 17004 KB Output is correct
33 Correct 14 ms 16876 KB Output is correct
34 Correct 16 ms 17004 KB Output is correct
35 Correct 104 ms 21740 KB Output is correct
36 Correct 133 ms 24428 KB Output is correct
37 Correct 131 ms 24556 KB Output is correct
38 Correct 127 ms 24172 KB Output is correct
39 Correct 16 ms 17260 KB Output is correct
40 Correct 21 ms 18028 KB Output is correct
41 Correct 21 ms 18028 KB Output is correct
42 Correct 16 ms 17260 KB Output is correct
43 Correct 17 ms 17260 KB Output is correct
44 Correct 17 ms 17260 KB Output is correct
45 Correct 17 ms 17260 KB Output is correct
46 Correct 17 ms 17260 KB Output is correct
47 Correct 19 ms 17644 KB Output is correct
48 Correct 19 ms 17644 KB Output is correct
49 Correct 27 ms 18540 KB Output is correct
50 Correct 133 ms 24772 KB Output is correct
51 Correct 22 ms 17772 KB Output is correct
52 Correct 120 ms 22764 KB Output is correct
53 Correct 30 ms 18412 KB Output is correct
54 Correct 123 ms 23788 KB Output is correct
55 Correct 20 ms 17772 KB Output is correct
56 Correct 21 ms 17772 KB Output is correct
57 Correct 20 ms 17900 KB Output is correct
58 Correct 20 ms 17900 KB Output is correct
59 Correct 19 ms 17644 KB Output is correct
60 Correct 104 ms 21228 KB Output is correct
61 Correct 21 ms 17644 KB Output is correct
62 Correct 132 ms 24064 KB Output is correct
63 Correct 747 ms 65900 KB Output is correct
64 Correct 747 ms 66032 KB Output is correct
65 Correct 754 ms 66028 KB Output is correct
66 Correct 291 ms 40820 KB Output is correct
67 Correct 1292 ms 96912 KB Output is correct
68 Correct 274 ms 39568 KB Output is correct
69 Correct 416 ms 39952 KB Output is correct
70 Correct 305 ms 39440 KB Output is correct
71 Correct 322 ms 39696 KB Output is correct
72 Correct 819 ms 57360 KB Output is correct
73 Correct 870 ms 59920 KB Output is correct
74 Correct 1750 ms 102280 KB Output is correct
75 Correct 687 ms 48720 KB Output is correct
76 Correct 1118 ms 69304 KB Output is correct
77 Correct 1123 ms 69672 KB Output is correct
78 Correct 298 ms 43664 KB Output is correct
79 Correct 492 ms 49296 KB Output is correct
80 Correct 259 ms 43060 KB Output is correct
81 Correct 419 ms 46772 KB Output is correct
82 Correct 1055 ms 64936 KB Output is correct
83 Correct 1030 ms 65064 KB Output is correct
84 Correct 856 ms 68184 KB Output is correct
85 Correct 880 ms 68348 KB Output is correct
86 Correct 529 ms 70184 KB Output is correct
87 Correct 584 ms 71336 KB Output is correct
88 Correct 875 ms 58384 KB Output is correct
89 Correct 1049 ms 67112 KB Output is correct