QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#827306#3561. Capital CityRedDiamond#1 129ms44936kbC++203.0kb2024-12-22 21:23:542024-12-22 21:24:00

Judging History

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

  • [2024-12-22 21:24:00]
  • 评测
  • 测评结果:1
  • 用时:129ms
  • 内存:44936kb
  • [2024-12-22 21:23:54]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


int n, k;
vector<vector<int>> g, occ;
vector<int> c, vis, sz, par, dep, in;
vector<int> l, r, last;
int tt = 0;


int centr = -1;
int ans = 1e9;


void build(int u, int p = 0) {
    l[u] = ++tt;
    for (auto v : g[u]) {
        if (v != p) {
            build(v, u);
        }
    }
    r[u] = tt;
}


void dfs(int u, int size, int p) {
    sz[u] = 1;
    par[u] = p;
    bool good = 1;
    for (auto v : g[u]) {
        if (v != p && !vis[v]) {
            dfs(v, size, u);
            sz[u] += sz[v];
            good &= (sz[v] <= size / 2);
        }
    }
    good &= (size - sz[u] <= size / 2);
    if (good == 1) {
        centr = u;
    }
}


void mark(int u, int c = 0, int p = 0) {
    last[u] = c;
    par[u] = p;
    for (auto v : g[u]) {
        if (v != p && !vis[v]) {
            mark(v, c, u);
        }
    }
}


vector<ll> f;


void update_ans(int centr) {
    queue<int> q;
    for (auto x : occ[c[centr]]) {
        if (last[x] != centr) {
            return;
        }
        q.push(x);
    }
    in[c[centr]] = 1;
    vector<int> revert;
    revert.push_back(c[centr]);
    bool ok = 1;
    int total = 1;
    while (!q.empty() && ok) {
        int u = q.front();
        q.pop();
        if (c[u] != c[par[u]] && in[c[par[u]]] == 0) {
            in[c[par[u]]] = 1;
            revert.push_back(c[par[u]]);
            total += 1;
            for (auto x : occ[c[par[u]]]) {
                if (last[x] != centr) {
                    ok = 0;
                    break;
                }
                q.push(x);
            }
        }
    }
    for (auto i : revert) {
        in[i] = 0;
    }
    if (ok == 1) {
        ans = min(ans, total - 1);
    }
}


void solve(int u, int p = 0) {
    centr = -1;
    dfs(u, 0, u);
    dfs(u, sz[u], u);
    mark(centr, centr);
    par[centr] = centr;
    update_ans(centr);
    vis[centr] = 1;
    dep[centr] = dep[p] + 1;
    par[centr] = p;
    int c = centr;
    for (auto v : g[u]) {
        if (!vis[v]) {
            solve(v, c);
        }
    }
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
	}
	cin >> n >> k;
    g.resize(n + 1);
    for (int i = 1; i < n; i += 1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    c.resize(n + 1);
    occ.resize(k + 1);
    in.resize(k + 1);
    for (int i = 1; i <= n; i += 1) {
        cin >> c[i];
        occ[c[i]].push_back(i);
    }
    l.resize(n + 1);
    r.resize(n + 1);
    last.resize(n + 1);
    build(1);
    vis.resize(n + 1);
    dep.resize(n + 1);
    par.resize(n + 1);
    sz.resize(n + 1);
    solve(1);
    cout << ans << "\n";
    return 0;
}

详细

Subtask #1:

score: 1
Accepted

Test #1:

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

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: 1
Accepted
time: 0ms
memory: 3528kb

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: 1
Accepted
time: 0ms
memory: 3480kb

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: 1
Accepted
time: 0ms
memory: 3612kb

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: 1
Accepted
time: 0ms
memory: 3528kb

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: 1
Accepted
time: 0ms
memory: 3816kb

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: 1
Accepted
time: 0ms
memory: 3556kb

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: 1
Accepted
time: 0ms
memory: 3592kb

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: 1
Accepted
time: 0ms
memory: 3600kb

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: 1
Accepted
time: 0ms
memory: 3612kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #11:

score: 10
Accepted
time: 1ms
memory: 4040kb

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:

244

result:

ok single line: '244'

Test #12:

score: 10
Accepted
time: 1ms
memory: 3792kb

input:

2000 250
37 10
1592 1517
607 125
77 194
56 1371
1470 1162
1004 323
309 567
925 188
389 509
1644 1619
286 1000
144 1539
244 900
644 28
528 26
251 140
183 81
764 248
21 775
191 25
1178 819
29 94
1166 934
271 1066
3 27
316 1063
901 91
219 64
853 983
13 5
180 70
394 992
1537 1193
188 1557
618 1613
116 7...

output:

246

result:

ok single line: '246'

Test #13:

score: 10
Accepted
time: 1ms
memory: 3744kb

input:

2000 250
1155 1655
183 259
19 685
845 610
1139 1514
665 549
199 272
516 1425
510 499
721 1497
316 497
452 1417
136 379
12 10
99 281
1850 1671
275 356
786 306
108 1833
1216 238
1914 210
1908 1158
771 893
41 635
67 988
202 726
16 55
671 577
199 306
731 1723
281 293
115 106
1365 374
1239 658
106 194
47...

output:

245

result:

ok single line: '245'

Test #14:

score: 10
Accepted
time: 1ms
memory: 3960kb

input:

2000 250
1580 249
640 178
2 1
615 1054
112 816
949 22
57 793
407 950
865 416
1903 1229
975 365
455 1355
1494 98
1565 497
1244 780
1323 1074
1588 138
1503 1145
447 352
1264 1880
951 1564
821 393
232 569
1023 572
158 255
571 1257
1693 704
1816 309
726 255
570 528
284 471
1430 569
26 1408
357 1902
452 ...

output:

245

result:

ok single line: '245'

Test #15:

score: 10
Accepted
time: 1ms
memory: 3792kb

input:

2000 758
1645 394
394 842
842 1368
1368 89
89 805
805 351
351 811
811 1752
1752 1787
1787 1219
1219 1299
1299 822
822 878
878 1582
1582 807
807 1371
1371 1142
1645 924
1645 282
282 834
282 74
74 1744
74 1834
1834 1309
1834 1009
1009 870
1009 1163
1163 1879
1163 25
25 1967
25 1779
1779 1974
1779 268
...

output:

7

result:

ok single line: '7'

Test #16:

score: 0
Wrong Answer
time: 1ms
memory: 3868kb

input:

2000 884
178 1218
1218 1351
1351 1815
1815 98
98 343
343 1095
1095 862
862 719
719 1071
1071 1231
1231 1366
1366 72
72 816
178 1470
178 1696
1696 298
1696 1448
1448 1172
1448 1006
1006 514
1006 647
647 544
647 1707
1707 872
1707 563
563 1049
563 1428
1428 665
1428 716
716 734
716 1195
1195 935
1195 ...

output:

7

result:

wrong answer 1st lines differ - expected: '6', found: '7'

Subtask #3:

score: 0
Wrong Answer

Test #31:

score: 30
Accepted
time: 116ms
memory: 44340kb

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:

4

result:

ok single line: '4'

Test #32:

score: 30
Accepted
time: 54ms
memory: 44864kb

input:

200000 100000
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
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

8

result:

ok single line: '8'

Test #33:

score: 30
Accepted
time: 114ms
memory: 43960kb

input:

200000 100000
16998 125645
125645 171820
171820 114276
114276 56649
56649 79575
79575 12368
12368 165362
165362 121507
121507 97604
97604 95803
95803 166064
166064 34692
34692 79122
79122 196245
196245 118382
118382 23706
23706 5613
5613 79967
79967 189807
189807 22420
22420 91378
91378 163988
16398...

output:

6

result:

ok single line: '6'

Test #34:

score: 30
Accepted
time: 68ms
memory: 44936kb

input:

200000 100000
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
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

2

result:

ok single line: '2'

Test #35:

score: 0
Wrong Answer
time: 129ms
memory: 40520kb

input:

200000 92212
154665 186755
186755 34426
34426 62560
62560 102764
102764 146653
146653 10601
10601 57489
57489 175122
175122 106567
106567 17032
17032 143043
143043 144788
144788 80662
80662 23840
23840 22198
22198 187257
187257 102646
102646 119845
119845 1996
1996 166213
166213 164599
164599 198625...

output:

7788

result:

wrong answer 1st lines differ - expected: '8', found: '7788'

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%