QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#296356#3561. Capital Cityjames1BadCreeper1 5ms13064kbC++141.5kb2024-01-02 20:06:242024-01-02 20:06:24

Judging History

你现在查看的是最新测评结果

  • [2024-01-02 20:06:24]
  • 评测
  • 测评结果:1
  • 用时:5ms
  • 内存:13064kb
  • [2024-01-02 20:06:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std; 
const int N = 200005; 

int n, k, ans, a[N], bel[N], hit[N]; 
vector<int> G[N]; 
int sz[N], rt, vis[N], mx[N], f[N]; 
vector<int> col[N]; 

void froot(int x, int fa, int tot) {
    sz[x] = 1; 
    for (int y : G[x]) if (y != fa && !vis[y]) froot(y, x, tot), sz[x] += sz[y], mx[x] = max(mx[x], sz[y]); 
    mx[x] = max(mx[x], tot - sz[x]); 
    if (mx[x] < mx[rt]) rt = x; 
}
void inTree(int x, int fa, int anc) {
    f[x] = fa, bel[x] = anc; 
    for (int y : G[x]) if (y != fa && !vis[y]) inTree(y, x, anc); 
}

void divide(int x) {
    // 覆盖所有含 x 的颜色
    vis[x] = 1; inTree(x, 0, x); 
    queue<int> q; q.push(a[x]); 
    int cnt = 0; 
    while (!q.empty()) {
        int c = q.front(); q.pop(); ++cnt; hit[c] = x; 
        // if (x == 3) cout << c << "\n"; 
        for (int i : col[c]) {
            if (bel[i] != x) goto fail; 
            if (f[i] && hit[a[f[i]]] != x) q.push(a[f[i]]); 
        }
    }
    ans = min(ans, cnt); 
    // cout << "G " << x << " " << cnt << "\n"; 
fail:
    for (int y : G[x]) if (!vis[y]) rt = 0, froot(y, x, sz[y]), divide(rt); 
}

int main(void) {
    ios::sync_with_stdio(0); 
    cin >> n >> k; mx[0] = 1e9; ans = k; 
    for (int i = 1, u, v; i < n; ++i) cin >> u >> v, G[u].emplace_back(v), G[v].emplace_back(u); 
    for (int i = 1; i <= n; ++i) cin >> a[i], col[a[i]].emplace_back(i); 
    froot(1, 0, n); divide(rt); 
    cout << ans - 1 << "\n"; 
    return 0; 
}

详细

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 0ms
memory: 12972kb

input:

20 3
16 10
10 3
18 2
4 5
8 6
11 12
2 14
1 2
6 3
1 11
1 4
7 20
3 2
9 7
3 13
15 19
5 7
17 6
12 15
2
2
1
1
1
2
2
1
3
3
1
3
1
3
2
2
1
2
2
3

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 5ms
memory: 12896kb

input:

20 3
13 1
5 17
14 1
15 2
19 17
7 9
4 6
12 5
15 18
1 2
16 20
3 4
11 8
2 7
9 16
5 1
3 2
5 8
7 10
1
2
3
2
1
3
3
3
2
3
3
3
3
2
2
1
3
1
2
3

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 5ms
memory: 12892kb

input:

20 3
7 6
9 13
12 11
16 6
20 8
14 17
2 3
9 11
4 2
2 1
12 14
15 8
18 16
9 19
10 4
2 9
8 3
4 5
5 6
2
2
2
3
2
3
3
1
2
2
1
2
1
1
1
2
3
2
3
3

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 0ms
memory: 12928kb

input:

20 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
10
9
8
5
6
7
1
2
3
4
5
6
7
1
2
3
4
8
9
10

output:

6

result:

ok single line: '6'

Test #5:

score: 0
Accepted
time: 0ms
memory: 12848kb

input:

20 10
19 6
6 3
3 18
18 20
20 11
11 7
7 17
17 14
14 9
9 13
13 5
5 12
12 4
4 10
10 2
2 16
16 15
15 8
8 1
8
3
2
6
7
5
7
5
1
1
4
9
4
6
2
10
9
10
8
3

output:

4

result:

ok single line: '4'

Test #6:

score: 0
Accepted
time: 4ms
memory: 12924kb

input:

20 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
10
9
8
7
6
5
4
3
2
1
2
1
3
4
5
6
7
8
9
10

output:

1

result:

ok single line: '1'

Test #7:

score: 0
Accepted
time: 4ms
memory: 12892kb

input:

20 10
20 9
9 17
17 4
15 4
15 16
3 19
3 16
19 6
19 2
19 7
6 11
11 8
8 13
13 10
10 5
5 14
14 12
12 1
1 18
10
2
7
9
10
3
2
3
7
1
4
1
4
8
5
9
6
8
6
5

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 0ms
memory: 12908kb

input:

20 10
18 12
12 11
9 11
9 19
8 7
8 19
16 7
16 20
3 6
3 20
6 15
15 2
2 14
14 10
10 13
13 4
4 5
5 17
17 1
9
8
6
9
2
8
4
3
6
2
5
10
7
1
1
10
7
3
5
4

output:

1

result:

ok single line: '1'

Test #9:

score: 0
Accepted
time: 0ms
memory: 12956kb

input:

20 10
13 15
15 12
15 17
15 5
12 16
16 4
18 4
18 11
2 14
2 11
14 3
3 9
9 8
8 6
6 1
1 7
7 19
19 10
10 20
9
6
5
7
8
1
3
5
4
9
7
6
2
4
10
2
8
10
1
3

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 0ms
memory: 12896kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #11:

score: 0
Wrong Answer
time: 2ms
memory: 13064kb

input:

2000 250
1875 208
1788 262
675 397
779 1033
185 238
469 70
650 1600
146 1093
248 1604
167 504
914 1041
1263 1427
131 68
1759 81
114 170
676 923
489 95
1747 107
133 91
582 164
35 1315
592 740
888 475
1230 117
818 522
1108 52
1276 1891
4 1
212 1917
1298 662
642 391
7 5
1035 1804
856 656
119 99
385 355...

output:

249

result:

wrong answer 1st lines differ - expected: '244', found: '249'

Subtask #3:

score: 0
Time Limit Exceeded

Test #31:

score: 0
Time Limit Exceeded

input:

200000 100000
185785 19011
19011 181550
181550 117972
117972 192238
192238 137685
137685 10126
10126 193657
193657 130856
130856 119980
119980 37122
37122 24497
24497 162102
162102 104298
104298 61332
61332 103789
103789 71060
71060 54044
54044 12075
12075 55296
55296 70106
70106 27512
27512 190160
...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%