답안 #684310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684310 2023-01-20T21:04:12 Z finn__ 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
971 ms 64772 KB
#include <bits/stdc++.h>
using namespace std;

set<unsigned> g[100000], rg[100000], pre[100000];
int d[100000];
queue<pair<unsigned, unsigned>> q;
int64_t x = 0;

int representant(int u)
{
    return d[u] < 0 ? u : (d[u] = representant(d[u]));
}

size_t total_edge_count(unsigned u)
{
    return g[u].size() + rg[u].size() + pre[u].size();
}

void insert_weak_connection(unsigned u, unsigned v)
{
    g[u].insert(v);
    rg[v].insert(u);
    if (g[v].find(u) != g[v].end())
        q.emplace(u, v);
}

void merge(unsigned u, unsigned v)
{
    if (u == v)
        return;

    if (total_edge_count(u) < total_edge_count(v)) // Make u the larger component.
        swap(u, v);

    x -= d[u] * pre[v].size() + d[v] * pre[u].size();
    d[u] += d[v];
    d[v] = u;

    for (unsigned const &y : pre[v])
        if (pre[u].find(y) != pre[u].end())
            x += d[u];
        else
            pre[u].insert(y);

    g[u].erase(v);
    g[v].erase(u);
    rg[u].erase(v);
    rg[v].erase(u);

    for (unsigned const &y : g[v])
    {
        rg[y].erase(v);
        insert_weak_connection(u, y);
    }
    for (unsigned const &y : rg[v])
    {
        g[y].erase(v);
        insert_weak_connection(y, u);
    }
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    size_t n, m;
    cin >> n >> m;

    for (unsigned i = 0; i < n; i++)
    {
        d[i] = -1;
        pre[i].insert(i);
    }

    for (size_t i = 0; i < m; i++)
    {
        unsigned u, v;
        cin >> u >> v;
        u--, v--;

        v = representant(v);
        if (representant(u) != v && pre[v].find(u) == pre[v].end())
        {
            pre[v].insert(u);
            x -= d[v];
            u = representant(u);
            insert_weak_connection(u, v);

            while (!q.empty())
            {
                auto const [a, b] = q.front();
                q.pop();
                merge(representant(a), representant(b));
            }
        }
        cout << x << '\n';
    }
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:82:29: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   82 |         if (representant(u) != v && pre[v].find(u) == pre[v].end())
      |             ~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 7 ms 14292 KB Output is correct
4 Correct 7 ms 14292 KB Output is correct
5 Correct 8 ms 14400 KB Output is correct
6 Correct 8 ms 14292 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14340 KB Output is correct
9 Correct 8 ms 14352 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14292 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14356 KB Output is correct
18 Correct 7 ms 14404 KB Output is correct
19 Correct 7 ms 14392 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14468 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 7 ms 14336 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14456 KB Output is correct
26 Correct 8 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14328 KB Output is correct
29 Correct 8 ms 14396 KB Output is correct
30 Correct 8 ms 14392 KB Output is correct
31 Correct 7 ms 14420 KB Output is correct
32 Correct 7 ms 14356 KB Output is correct
33 Correct 7 ms 14408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 7 ms 14292 KB Output is correct
4 Correct 7 ms 14292 KB Output is correct
5 Correct 8 ms 14400 KB Output is correct
6 Correct 8 ms 14292 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14340 KB Output is correct
9 Correct 8 ms 14352 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14292 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14356 KB Output is correct
18 Correct 7 ms 14404 KB Output is correct
19 Correct 7 ms 14392 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14468 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 7 ms 14336 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14456 KB Output is correct
26 Correct 8 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14328 KB Output is correct
29 Correct 8 ms 14396 KB Output is correct
30 Correct 8 ms 14392 KB Output is correct
31 Correct 7 ms 14420 KB Output is correct
32 Correct 7 ms 14356 KB Output is correct
33 Correct 7 ms 14408 KB Output is correct
34 Correct 9 ms 14420 KB Output is correct
35 Correct 69 ms 18000 KB Output is correct
36 Correct 98 ms 20372 KB Output is correct
37 Correct 92 ms 20528 KB Output is correct
38 Correct 93 ms 20132 KB Output is correct
39 Correct 12 ms 14676 KB Output is correct
40 Correct 15 ms 15024 KB Output is correct
41 Correct 11 ms 15048 KB Output is correct
42 Correct 10 ms 14676 KB Output is correct
43 Correct 11 ms 14644 KB Output is correct
44 Correct 11 ms 14676 KB Output is correct
45 Correct 9 ms 14676 KB Output is correct
46 Correct 11 ms 14680 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 11 ms 14932 KB Output is correct
49 Correct 17 ms 15572 KB Output is correct
50 Correct 99 ms 20660 KB Output is correct
51 Correct 14 ms 14932 KB Output is correct
52 Correct 92 ms 18896 KB Output is correct
53 Correct 18 ms 15444 KB Output is correct
54 Correct 87 ms 19632 KB Output is correct
55 Correct 12 ms 15060 KB Output is correct
56 Correct 17 ms 15132 KB Output is correct
57 Correct 13 ms 15128 KB Output is correct
58 Correct 11 ms 15132 KB Output is correct
59 Correct 13 ms 15064 KB Output is correct
60 Correct 68 ms 17496 KB Output is correct
61 Correct 11 ms 14932 KB Output is correct
62 Correct 92 ms 19928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 7 ms 14292 KB Output is correct
4 Correct 7 ms 14292 KB Output is correct
5 Correct 8 ms 14400 KB Output is correct
6 Correct 8 ms 14292 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14340 KB Output is correct
9 Correct 8 ms 14352 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 9 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14292 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14356 KB Output is correct
18 Correct 7 ms 14404 KB Output is correct
19 Correct 7 ms 14392 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14468 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 7 ms 14336 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14456 KB Output is correct
26 Correct 8 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14328 KB Output is correct
29 Correct 8 ms 14396 KB Output is correct
30 Correct 8 ms 14392 KB Output is correct
31 Correct 7 ms 14420 KB Output is correct
32 Correct 7 ms 14356 KB Output is correct
33 Correct 7 ms 14408 KB Output is correct
34 Correct 9 ms 14420 KB Output is correct
35 Correct 69 ms 18000 KB Output is correct
36 Correct 98 ms 20372 KB Output is correct
37 Correct 92 ms 20528 KB Output is correct
38 Correct 93 ms 20132 KB Output is correct
39 Correct 12 ms 14676 KB Output is correct
40 Correct 15 ms 15024 KB Output is correct
41 Correct 11 ms 15048 KB Output is correct
42 Correct 10 ms 14676 KB Output is correct
43 Correct 11 ms 14644 KB Output is correct
44 Correct 11 ms 14676 KB Output is correct
45 Correct 9 ms 14676 KB Output is correct
46 Correct 11 ms 14680 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 11 ms 14932 KB Output is correct
49 Correct 17 ms 15572 KB Output is correct
50 Correct 99 ms 20660 KB Output is correct
51 Correct 14 ms 14932 KB Output is correct
52 Correct 92 ms 18896 KB Output is correct
53 Correct 18 ms 15444 KB Output is correct
54 Correct 87 ms 19632 KB Output is correct
55 Correct 12 ms 15060 KB Output is correct
56 Correct 17 ms 15132 KB Output is correct
57 Correct 13 ms 15128 KB Output is correct
58 Correct 11 ms 15132 KB Output is correct
59 Correct 13 ms 15064 KB Output is correct
60 Correct 68 ms 17496 KB Output is correct
61 Correct 11 ms 14932 KB Output is correct
62 Correct 92 ms 19928 KB Output is correct
63 Correct 489 ms 63676 KB Output is correct
64 Correct 501 ms 63968 KB Output is correct
65 Correct 512 ms 63684 KB Output is correct
66 Correct 207 ms 30888 KB Output is correct
67 Correct 389 ms 57788 KB Output is correct
68 Correct 200 ms 30908 KB Output is correct
69 Correct 380 ms 31980 KB Output is correct
70 Correct 215 ms 30904 KB Output is correct
71 Correct 204 ms 30980 KB Output is correct
72 Correct 414 ms 41896 KB Output is correct
73 Correct 418 ms 43624 KB Output is correct
74 Correct 971 ms 64328 KB Output is correct
75 Correct 591 ms 37788 KB Output is correct
76 Correct 759 ms 49596 KB Output is correct
77 Correct 722 ms 49900 KB Output is correct
78 Correct 238 ms 35048 KB Output is correct
79 Correct 369 ms 37912 KB Output is correct
80 Correct 202 ms 33868 KB Output is correct
81 Correct 330 ms 35892 KB Output is correct
82 Correct 797 ms 51588 KB Output is correct
83 Correct 811 ms 51684 KB Output is correct
84 Correct 604 ms 50184 KB Output is correct
85 Correct 643 ms 50252 KB Output is correct
86 Correct 294 ms 63700 KB Output is correct
87 Correct 348 ms 64772 KB Output is correct
88 Correct 433 ms 42912 KB Output is correct
89 Correct 685 ms 47752 KB Output is correct