답안 #1091707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091707 2024-09-21T21:35:55 Z raphaelp Mergers (JOI19_mergers) C++14
100 / 100
708 ms 99120 KB
#include <bits/stdc++.h>
using namespace std;
void dfs(int x, int par, vector<vector<int>> &AR, vector<int> &depth, vector<int> &p, int prof)
{
    depth[x] = prof;
    p[x] = par;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i] == par)
            continue;
        dfs(AR[x][i], x, AR, depth, p, prof + 1);
    }
}
vector<int> lead;
int find(int x) { return (lead[x] == x) ? x : lead[x] = find(lead[x]); }
void dfs2(int x, int p, vector<vector<int>> &AR, int &leafs)
{
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i] == p)
            continue;
        dfs2(AR[x][i], x, AR, leafs);
    }
    if (AR[x].size() == 1)
        leafs++;
}
int main()
{
    int N, K;
    cin >> N >> K;
    vector<vector<int>> AR(N);
    for (int i = 0; i < N - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--, b--;
        AR[a].push_back(b);
        AR[b].push_back(a);
    }
    vector<vector<int>> groups(K);
    vector<int> Tab(N);
    lead.assign(N, 0);
    for (int i = 0; i < N; i++)
        lead[i] = i;
    for (int i = 0; i < N; i++)
    {
        cin >> Tab[i];
        Tab[i]--;
        groups[Tab[i]].push_back(i);
    }
    vector<int> depth(N), p(N), occ(N);
    dfs(0, 0, AR, depth, p, 0);
    for (int i = 0; i < K; i++)
    {
        while (groups[i].size() > 1)
        {
            int a = groups[i].back();
            groups[i].pop_back();
            int b = groups[i].back();
            groups[i].pop_back();
            if (occ[a] || occ[b])
            {
                if (occ[a] == 0)
                    groups[i].push_back(a), occ[a] = 0;
                if (occ[b] == 0)
                    groups[i].push_back(b), occ[b] = 0;
                continue;
            }
            occ[a] = 1, occ[b] = 1;
            while (a != b)
            {
                if (depth[b] > depth[a])
                    swap(a, b);
                int next = find(p[a]);
                occ[next] = 1;
                if (Tab[next] > i)
                {
                    for (auto val : groups[Tab[next]])
                        groups[i].push_back(val);
                    groups[Tab[next]].clear();
                }
                if (AR[next].size() < AR[a].size())
                    swap(AR[next], AR[a]);
                for (auto val : AR[a])
                    AR[next].push_back(val);
                lead[a] = next;
                a = next;
            }
            occ[a] = 0;
            groups[i].push_back(a);
        }
    }
    for (int i = 0; i < N; i++)
    {
        if (occ[i])
            continue;
        for (int j = 0; j < AR[i].size(); j++)
        {
            AR[i][j] = find(AR[i][j]);
            if (AR[i][j] == i)
            {
                swap(AR[i][j], AR[i].back());
                AR[i].pop_back();
                j--;
            }
        }
        sort(AR[i].begin(), AR[i].end());
        for (int j = 1; j < AR[i].size(); j++)
        {
            if (AR[i][j] == AR[i][j - 1])
            {
                swap(AR[i][j], AR[i].back());
                AR.pop_back();
                j--;
            }
        }
    }
    int leafs = 0;
    dfs2(find(0), find(0), AR, leafs);
    if (leafs == 1)
        cout << 0;
    else
        cout << leafs / 2 + leafs % 2;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<int>&, int)':
mergers.cpp:7:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int, std::vector<std::vector<int> >&, int&)':
mergers.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for (int j = 0; j < AR[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~
mergers.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         for (int j = 1; j < AR[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 440 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 440 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 440 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 3 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 3 ms 860 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 2 ms 612 KB Output is correct
35 Correct 2 ms 860 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 860 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 3 ms 628 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 0 ms 436 KB Output is correct
45 Correct 3 ms 860 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 864 KB Output is correct
50 Correct 3 ms 748 KB Output is correct
51 Correct 2 ms 616 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 4 ms 708 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 440 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 440 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 56 ms 10684 KB Output is correct
27 Correct 78 ms 11628 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 68 ms 11104 KB Output is correct
32 Correct 2 ms 600 KB Output is correct
33 Correct 71 ms 18780 KB Output is correct
34 Correct 85 ms 11748 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 75 ms 11600 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 3 ms 600 KB Output is correct
39 Correct 54 ms 10816 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 67 ms 11176 KB Output is correct
42 Correct 77 ms 14424 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 81 ms 18868 KB Output is correct
45 Correct 86 ms 15280 KB Output is correct
46 Correct 3 ms 600 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 10700 KB Output is correct
2 Correct 73 ms 15480 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 87 ms 14168 KB Output is correct
9 Correct 2 ms 600 KB Output is correct
10 Correct 70 ms 11496 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 69 ms 11344 KB Output is correct
13 Correct 84 ms 13936 KB Output is correct
14 Correct 73 ms 15188 KB Output is correct
15 Correct 67 ms 10952 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 69 ms 15232 KB Output is correct
19 Correct 81 ms 18984 KB Output is correct
20 Correct 3 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 63 ms 13120 KB Output is correct
23 Correct 4 ms 604 KB Output is correct
24 Correct 78 ms 12208 KB Output is correct
25 Correct 79 ms 17692 KB Output is correct
26 Correct 2 ms 860 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 440 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 440 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 3 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 3 ms 860 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 2 ms 612 KB Output is correct
35 Correct 2 ms 860 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 860 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 3 ms 628 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 0 ms 436 KB Output is correct
45 Correct 3 ms 860 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 864 KB Output is correct
50 Correct 3 ms 748 KB Output is correct
51 Correct 2 ms 616 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 4 ms 708 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 56 ms 10684 KB Output is correct
57 Correct 78 ms 11628 KB Output is correct
58 Correct 2 ms 600 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 68 ms 11104 KB Output is correct
62 Correct 2 ms 600 KB Output is correct
63 Correct 71 ms 18780 KB Output is correct
64 Correct 85 ms 11748 KB Output is correct
65 Correct 2 ms 600 KB Output is correct
66 Correct 75 ms 11600 KB Output is correct
67 Correct 3 ms 600 KB Output is correct
68 Correct 3 ms 600 KB Output is correct
69 Correct 54 ms 10816 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 67 ms 11176 KB Output is correct
72 Correct 77 ms 14424 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 81 ms 18868 KB Output is correct
75 Correct 86 ms 15280 KB Output is correct
76 Correct 3 ms 600 KB Output is correct
77 Correct 2 ms 604 KB Output is correct
78 Correct 56 ms 10700 KB Output is correct
79 Correct 73 ms 15480 KB Output is correct
80 Correct 3 ms 600 KB Output is correct
81 Correct 2 ms 632 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 2 ms 640 KB Output is correct
85 Correct 87 ms 14168 KB Output is correct
86 Correct 2 ms 600 KB Output is correct
87 Correct 70 ms 11496 KB Output is correct
88 Correct 0 ms 348 KB Output is correct
89 Correct 69 ms 11344 KB Output is correct
90 Correct 84 ms 13936 KB Output is correct
91 Correct 73 ms 15188 KB Output is correct
92 Correct 67 ms 10952 KB Output is correct
93 Correct 2 ms 600 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 69 ms 15232 KB Output is correct
96 Correct 81 ms 18984 KB Output is correct
97 Correct 3 ms 600 KB Output is correct
98 Correct 0 ms 348 KB Output is correct
99 Correct 63 ms 13120 KB Output is correct
100 Correct 4 ms 604 KB Output is correct
101 Correct 78 ms 12208 KB Output is correct
102 Correct 79 ms 17692 KB Output is correct
103 Correct 2 ms 860 KB Output is correct
104 Correct 2 ms 860 KB Output is correct
105 Correct 2 ms 604 KB Output is correct
106 Correct 2 ms 604 KB Output is correct
107 Correct 2 ms 604 KB Output is correct
108 Correct 470 ms 62768 KB Output is correct
109 Correct 524 ms 73368 KB Output is correct
110 Correct 585 ms 83520 KB Output is correct
111 Correct 708 ms 99120 KB Output is correct
112 Correct 682 ms 95392 KB Output is correct
113 Correct 507 ms 77364 KB Output is correct
114 Correct 456 ms 55472 KB Output is correct
115 Correct 487 ms 54896 KB Output is correct
116 Correct 566 ms 64092 KB Output is correct
117 Correct 602 ms 75348 KB Output is correct
118 Correct 537 ms 57424 KB Output is correct
119 Correct 586 ms 75348 KB Output is correct
120 Correct 586 ms 89300 KB Output is correct
121 Correct 623 ms 75408 KB Output is correct
122 Correct 607 ms 71616 KB Output is correct
123 Correct 487 ms 77000 KB Output is correct
124 Correct 662 ms 76448 KB Output is correct