답안 #384166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
384166 2021-03-31T15:42:02 Z VodkaInTheJar 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
772 ms 53216 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 1e5 + 3; 

int n, m; 
void read()
{
	cin >> n >> m; 
}

int par[maxn], sz[maxn];
set <int> adj[maxn], rev_adj[maxn];
vector <int> members[maxn];

int find_root(int ver)
{
    if (ver == par[ver])
        return ver;

    return par[ver] = find_root(par[ver]);
}

long long ans = 0; 
vector <pair <int, int> > to_add;

void merge(int a, int b)
{
    a = find_root(a), b = find_root(b);

    if (a == b)
        return;

    ans -= 1ll * sz[a] * (sz[a] - 1);
    ans -= 1ll * sz[b] * (sz[b] - 1);
    ans -= 1ll * (int)rev_adj[a].size() * sz[a];
    ans -= 1ll * (int)rev_adj[b].size() * sz[b];

    if (sz[a] < sz[b])
        swap(a, b);

    for (auto i: members[b])
        members[a].push_back(i);

    for (auto j: adj[b])
        if (j != a)
        {
            adj[a].insert(j);
            if (adj[j].find(a) != adj[j].end())
                to_add.push_back({a, j});
        }

    for (auto j: rev_adj[b])
    {
        if (find_root(j) == a)
            continue;

        if (adj[a].find(find_root(j)) != adj[a].end())
            to_add.push_back({j, a});

        adj[find_root(j)].erase(b);
        adj[find_root(j)].insert(a);
        rev_adj[a].insert(j);
    }
    
    adj[a].erase(b);
    vector <int> to_remove;
    for (auto i: members[b])
        if (rev_adj[a].find(i) != rev_adj[a].end())
            rev_adj[a].erase(i);

    par[b] = a;
    sz[a] += sz[b];
    ans += 1ll * sz[a] * (sz[a] - 1);
    ans += 1ll * (int)rev_adj[a].size() * sz[a];
}

pair <int, int> e[maxn * 3];
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        par[i] = i, sz[i] = 1; 
        members[i].push_back(i);
    }

    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].first >> e[i].second;
        
        int a = find_root(e[i].first), b = find_root(e[i].second);
        if (a != b)
        {
            ans -= 1ll * (int)rev_adj[b].size() * sz[b]; 
            adj[a].insert(b);
            rev_adj[b].insert(e[i].first);

            ans += 1ll * (int)rev_adj[b].size() * sz[b];

            if (adj[b].find(a) != adj[b].end())
            {
                to_add.push_back({a, b});
                while (!to_add.empty())
                {
                    auto curr = to_add.back();
                    to_add.pop_back();

                    merge(curr.first, curr.second);
                }
            }
        }

       cout << ans << endl; 
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	read();
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 10 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 10 ms 12140 KB Output is correct
26 Correct 8 ms 12140 KB Output is correct
27 Correct 8 ms 12140 KB Output is correct
28 Correct 8 ms 12140 KB Output is correct
29 Correct 8 ms 12140 KB Output is correct
30 Correct 8 ms 12140 KB Output is correct
31 Correct 9 ms 12140 KB Output is correct
32 Correct 8 ms 12140 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 10 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 10 ms 12140 KB Output is correct
26 Correct 8 ms 12140 KB Output is correct
27 Correct 8 ms 12140 KB Output is correct
28 Correct 8 ms 12140 KB Output is correct
29 Correct 8 ms 12140 KB Output is correct
30 Correct 8 ms 12140 KB Output is correct
31 Correct 9 ms 12140 KB Output is correct
32 Correct 8 ms 12140 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 11 ms 12268 KB Output is correct
35 Correct 93 ms 17644 KB Output is correct
36 Correct 114 ms 19460 KB Output is correct
37 Correct 113 ms 19560 KB Output is correct
38 Correct 110 ms 19308 KB Output is correct
39 Correct 11 ms 12396 KB Output is correct
40 Correct 12 ms 12572 KB Output is correct
41 Correct 12 ms 12524 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 11 ms 12396 KB Output is correct
44 Correct 11 ms 12396 KB Output is correct
45 Correct 10 ms 12396 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 11 ms 12524 KB Output is correct
48 Correct 12 ms 12524 KB Output is correct
49 Correct 17 ms 13036 KB Output is correct
50 Correct 114 ms 19588 KB Output is correct
51 Correct 15 ms 12652 KB Output is correct
52 Correct 110 ms 18412 KB Output is correct
53 Correct 16 ms 13036 KB Output is correct
54 Correct 111 ms 18924 KB Output is correct
55 Correct 14 ms 12652 KB Output is correct
56 Correct 14 ms 12652 KB Output is correct
57 Correct 12 ms 12652 KB Output is correct
58 Correct 12 ms 12652 KB Output is correct
59 Correct 11 ms 12524 KB Output is correct
60 Correct 93 ms 17132 KB Output is correct
61 Correct 13 ms 12524 KB Output is correct
62 Correct 115 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12140 KB Output is correct
3 Correct 8 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 8 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 10 ms 12140 KB Output is correct
17 Correct 8 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 8 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12140 KB Output is correct
25 Correct 10 ms 12140 KB Output is correct
26 Correct 8 ms 12140 KB Output is correct
27 Correct 8 ms 12140 KB Output is correct
28 Correct 8 ms 12140 KB Output is correct
29 Correct 8 ms 12140 KB Output is correct
30 Correct 8 ms 12140 KB Output is correct
31 Correct 9 ms 12140 KB Output is correct
32 Correct 8 ms 12140 KB Output is correct
33 Correct 9 ms 12140 KB Output is correct
34 Correct 11 ms 12268 KB Output is correct
35 Correct 93 ms 17644 KB Output is correct
36 Correct 114 ms 19460 KB Output is correct
37 Correct 113 ms 19560 KB Output is correct
38 Correct 110 ms 19308 KB Output is correct
39 Correct 11 ms 12396 KB Output is correct
40 Correct 12 ms 12572 KB Output is correct
41 Correct 12 ms 12524 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 11 ms 12396 KB Output is correct
44 Correct 11 ms 12396 KB Output is correct
45 Correct 10 ms 12396 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 11 ms 12524 KB Output is correct
48 Correct 12 ms 12524 KB Output is correct
49 Correct 17 ms 13036 KB Output is correct
50 Correct 114 ms 19588 KB Output is correct
51 Correct 15 ms 12652 KB Output is correct
52 Correct 110 ms 18412 KB Output is correct
53 Correct 16 ms 13036 KB Output is correct
54 Correct 111 ms 18924 KB Output is correct
55 Correct 14 ms 12652 KB Output is correct
56 Correct 14 ms 12652 KB Output is correct
57 Correct 12 ms 12652 KB Output is correct
58 Correct 12 ms 12652 KB Output is correct
59 Correct 11 ms 12524 KB Output is correct
60 Correct 93 ms 17132 KB Output is correct
61 Correct 13 ms 12524 KB Output is correct
62 Correct 115 ms 19180 KB Output is correct
63 Correct 417 ms 48492 KB Output is correct
64 Correct 432 ms 48620 KB Output is correct
65 Correct 436 ms 48492 KB Output is correct
66 Correct 171 ms 29448 KB Output is correct
67 Correct 324 ms 35412 KB Output is correct
68 Correct 162 ms 29540 KB Output is correct
69 Correct 373 ms 29412 KB Output is correct
70 Correct 190 ms 29540 KB Output is correct
71 Correct 174 ms 29560 KB Output is correct
72 Correct 344 ms 33512 KB Output is correct
73 Correct 350 ms 34024 KB Output is correct
74 Correct 772 ms 53216 KB Output is correct
75 Correct 507 ms 34276 KB Output is correct
76 Correct 568 ms 41848 KB Output is correct
77 Correct 577 ms 42132 KB Output is correct
78 Correct 210 ms 29672 KB Output is correct
79 Correct 343 ms 34948 KB Output is correct
80 Correct 191 ms 29544 KB Output is correct
81 Correct 327 ms 33124 KB Output is correct
82 Correct 464 ms 40296 KB Output is correct
83 Correct 461 ms 40168 KB Output is correct
84 Correct 414 ms 40172 KB Output is correct
85 Correct 405 ms 40168 KB Output is correct
86 Correct 206 ms 33456 KB Output is correct
87 Correct 244 ms 35084 KB Output is correct
88 Correct 335 ms 33640 KB Output is correct
89 Correct 567 ms 40808 KB Output is correct