QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#629476 | #3561. Capital City | Dimash# | 31 | 119ms | 50732kb | C++23 | 2.4kb | 2024-10-11 12:16:29 | 2024-10-11 12:16:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 12, MOD = (int)1e9 + 7;
int n, k, par[N], p[N], r[N], tin[N], tout[N], timer, w[N], cl[N];
vector<int> g[N], c[N], G[N], Gr[N], f[N];
void dfs(int v, int pr = -1) {
par[v] = pr;
tin[v] = ++timer;
for(int to:g[v]) if(to != pr){
dfs(to, v);
}
tout[v] = ++timer;
}
bool vis[N];
vector<int> ord;
void dfs1(int v) {
vis[v] = 1;
for(int to:G[v]) {
if(!vis[to]) {
dfs1(to);
}
}
ord.push_back(v);
}
int t = 1;
void dfs2(int v) {
cl[v] = t;
f[t].push_back(v);
for(int to:Gr[v]) {
if(!cl[to]) {
dfs2(to);
}
}
}
void test() {
cin >> n >> k;
for(int i = 1; i <= k; i++) {
p[i] = i;
}
for(int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
for(int i = 1; i <= n; i++) {
int a;
cin >> a;
w[i] = a;
c[a].push_back(i);
}
for(int i = 1; i <= k; i++) {
if((int)c[i].size() == 1) {
cout << 0 << '\n';
return;
}
sort(c[i].begin(), c[i].end(), [&](int v, int u) {
return tin[v] < tin[u];
});
for(int j = 1; j < (int)c[i].size(); j++) {
int v = c[i][j];
if(w[par[v]] != i) {
G[i].push_back(w[par[v]]);
Gr[w[par[v]]].push_back(i);
}
}
}
for(int i = 1; i <= k; i++) {
if(!vis[i]) {
dfs1(i);
}
}
reverse(ord.begin(), ord.end());
for(int i:ord) {
if(!cl[i]) {
dfs2(i);
t++;
}
}
int res = k - 1;
for(int i = 1; i < t; i++) {
bool bad = 0;
for(int j:f[i]) {
for(int to:G[j]) {
// cout << i << ' ' << cl[to] << "x\n";
if(cl[to] != i) {
bad = 1;
break;
}
}
}
if(!bad) {
res = min(res, (int)f[i].size() - 1);
}
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 1
Accepted
Test #1:
score: 1
Accepted
time: 2ms
memory: 5668kb
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: 2ms
memory: 5724kb
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: 2ms
memory: 7740kb
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: 1ms
memory: 5680kb
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: 2ms
memory: 5736kb
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: 5744kb
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: 2ms
memory: 5740kb
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: 2ms
memory: 5680kb
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: 2ms
memory: 5720kb
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: 2ms
memory: 7676kb
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: 5944kb
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:
242
result:
wrong answer 1st lines differ - expected: '244', found: '242'
Subtask #3:
score: 30
Accepted
Test #31:
score: 30
Accepted
time: 114ms
memory: 50196kb
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: 65ms
memory: 49884kb
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: 119ms
memory: 50416kb
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: 48ms
memory: 50644kb
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: 30
Accepted
time: 119ms
memory: 46844kb
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:
8
result:
ok single line: '8'
Test #36:
score: 30
Accepted
time: 49ms
memory: 50732kb
input:
200000 98538 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 5...
output:
10
result:
ok single line: '10'
Test #37:
score: 30
Accepted
time: 111ms
memory: 45860kb
input:
200000 85796 134991 80269 80269 173185 173185 27752 27752 29879 29879 39943 39943 89030 89030 19246 19246 189377 189377 32595 32595 25167 25167 124720 124720 111123 111123 92985 92985 93436 93436 127124 127124 67035 67035 156339 156339 29358 29358 92195 92195 103104 103104 72646 72646 124927 124927 ...
output:
9
result:
ok single line: '9'
Test #38:
score: 30
Accepted
time: 53ms
memory: 47180kb
input:
200000 82396 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 5...
output:
7
result:
ok single line: '7'
Test #39:
score: 30
Accepted
time: 95ms
memory: 35616kb
input:
200000 50013 170581 23186 23186 177134 163331 177134 163331 132181 194751 35470 194751 132181 35470 65283 28070 65283 28070 781 180242 143138 180242 143600 48463 148936 48463 100936 148936 96574 96574 78505 78505 63572 194815 63572 194815 48573 152482 167917 152482 48573 167917 87036 87036 85202 160...
output:
2
result:
ok single line: '2'
Test #40:
score: 30
Accepted
time: 106ms
memory: 34552kb
input:
200000 50009 53254 34021 125082 34021 125082 101474 183290 70640 183290 60628 118612 135923 118612 101741 151744 188168 151744 106117 187249 99079 187249 28295 161668 143405 161668 178611 143405 186756 186756 432 432 174138 174138 18523 89734 116022 89734 62141 18384 58216 18384 14826 58216 38113 38...
output:
0
result:
ok single line: '0'
Test #41:
score: 30
Accepted
time: 96ms
memory: 36000kb
input:
200000 50013 88111 107134 54853 107134 54853 107827 40532 78310 40532 107827 78310 153257 182406 163365 182406 192796 34868 77975 34868 53279 77975 62729 15477 62729 15477 152283 130336 152283 130336 175136 196175 175136 196175 32916 111036 168236 111036 138509 14793 32143 14793 149750 99955 149750 ...
output:
2
result:
ok single line: '2'
Test #42:
score: 30
Accepted
time: 106ms
memory: 37440kb
input:
200000 50016 191219 46655 79886 46655 79886 132683 126512 21011 126512 132683 21011 79636 132650 45498 132650 196169 180748 170342 180748 179651 53230 179651 53230 193295 113881 193295 113881 187037 122368 89628 122368 39922 1483 36046 1483 89628 36046 124372 124372 14058 91758 14058 91758 130182 36...
output:
3
result:
ok single line: '3'
Test #43:
score: 30
Accepted
time: 103ms
memory: 34440kb
input:
200000 50018 154589 9697 76973 4356 76973 162487 94252 51141 94252 127195 107784 72724 107784 144011 42535 10544 42535 144011 100980 17309 100980 145916 78361 131057 78361 3275 138599 3275 138599 79138 38607 171999 38607 79138 171999 150076 150076 34712 152426 34712 152426 61160 166671 2276 166671 3...
output:
3
result:
ok single line: '3'
Test #44:
score: 30
Accepted
time: 105ms
memory: 37700kb
input:
200000 50014 47360 128036 128036 191338 191338 157765 157765 141561 57871 141561 57871 58778 138682 139852 138682 6448 147905 15680 147905 6448 15680 137305 137305 194588 194588 6409 6409 131353 131353 179360 179360 54860 54860 111448 123782 111448 123782 76593 188181 2537 188181 76593 2537 46075 62...
output:
3
result:
ok single line: '3'
Test #45:
score: 30
Accepted
time: 100ms
memory: 37824kb
input:
200000 50029 164841 183189 183189 155845 155845 141581 141581 24144 24144 170222 170222 129184 95002 129184 95002 135901 41224 29736 41224 135901 158337 29736 158337 99202 140435 99202 140435 198953 49064 157415 49064 151463 123429 84281 123429 141037 84281 46503 46503 9523 9523 122954 122954 61136 ...
output:
8
result:
ok single line: '8'
Test #46:
score: 30
Accepted
time: 96ms
memory: 33648kb
input:
200000 50015 94558 13078 120450 13078 120450 58559 113171 58559 113171 115752 58951 115752 58951 130988 59217 56758 59217 130988 175441 56758 175441 101975 181623 16394 181623 20434 35410 40765 35410 143333 61929 40765 61929 173496 133807 43920 133807 173496 43920 1520 1520 51520 79530 107878 79530 ...
output:
2
result:
ok single line: '2'
Test #47:
score: 30
Accepted
time: 96ms
memory: 34724kb
input:
200000 50019 100976 21386 54405 21386 54405 106988 187496 165594 187496 198768 108910 4552 108910 169830 153197 44334 153197 141781 11091 167119 11091 44334 167119 133936 133936 117111 117111 17706 13050 17706 13050 37494 173717 40923 173717 37494 40923 61691 179943 135508 179943 33828 48814 40443 4...
output:
4
result:
ok single line: '4'
Test #48:
score: 30
Accepted
time: 92ms
memory: 34216kb
input:
200000 50015 41218 36746 36746 61530 182706 61530 182706 11941 49603 11941 49603 144834 8207 144834 8207 167684 5403 26230 5403 32778 54058 104163 54058 32778 95753 104163 95753 171272 39726 3635 39726 141985 26531 17601 26531 110064 192916 41569 192916 17601 41569 78920 78920 144967 144967 133941 1...
output:
2
result:
ok single line: '2'
Test #49:
score: 30
Accepted
time: 98ms
memory: 37384kb
input:
200000 50032 32139 110705 136340 88294 136340 43597 198825 29886 198825 163916 197709 186561 197709 50762 165557 61591 165557 122505 165839 122505 165839 162251 2427 78007 2427 181894 79339 38177 79339 48295 171133 364 171133 56124 125448 77502 125448 364 77502 159484 156307 159484 156307 83400 1289...
output:
9
result:
ok single line: '9'
Test #50:
score: 30
Accepted
time: 99ms
memory: 38412kb
input:
200000 50008 192794 76389 6492 76389 6492 110252 26774 145638 26774 110252 145638 31685 31685 101692 101692 103033 75760 103033 75760 188802 80328 107656 80328 182799 21451 166787 21451 107656 166787 182607 182607 61306 61306 107984 82116 107984 82116 18031 30598 90659 30598 18031 90659 47780 103629...
output:
1
result:
ok single line: '1'
Test #51:
score: 30
Accepted
time: 0ms
memory: 5728kb
input:
1 1 1
output:
0
result:
ok single line: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%