#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define R register int
#define re(i,a,b) for(R i=a; i<=b; i++)
#define ms(i,a) memset(a,i,sizeof(a))
#define MAX(a,b) (((a)>(b)) ? (a):(b))
#define MIN(a,b) (((a)<(b)) ? (a):(b))
using namespace std;
typedef long long LL;
namespace IO {
#include <cctype>
template <typename T>
inline void read(T &x){
x=0;
char c=0;
T w=0;
while (!isdigit(c)) w|=c=='-',c=getchar();
while (isdigit(c)) x=x*10+(c^48),c=getchar();
if(w) x=-x;
}
template <typename T>
inline void write(T x) {
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template <typename T>
inline void writeln(T x) {
write(x);
putchar('\n');
}
}
using IO::read;
using IO::write;
using IO::writeln;
const int N=2e5+5;
int n,k,clk,scc,ans;
int dfn[N],c[N],mn[N],rr[N],idfn[N],f[N],low[N],st[N],tp,sz[N],bel[N];
bool ins[N],ban[N];
vector<int> e[N],v[N];
inline void cmin(int &x,int y) {
y < x ? x= y : 0;
}
void dfs(int x) {
idfn[dfn[x]=++clk]=x;
for(int i=0,y; i<e[x].size(); i++)
if(!dfn[y=e[x][i]]) f[y]=x,dfs(y);
rr[x]=clk;
}
void tarjan(int x) {
dfn[x]=low[x]=++clk;
st[++tp]=x;
ins[x]=1;
for(int i=0,y; i<v[x].size(); i++)
if(!dfn[y=v[x][i]]) tarjan(y),cmin(low[x],low[y]);
else if(ins[y]) cmin(low[x],dfn[y]);
if(dfn[x]==low[x]) {
int y;
scc++;
do {
y=st[tp--];
ins[y]=0;
bel[y]=scc;
sz[scc]++;
} while(y!=x);
}
}
int main() {
read(n);
read(k);
ans=k;
for(int i=1; i<n; i++) {
int x,y;
read(x);
read(y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1);
memset(mn,0x3f,sizeof(mn));
for(int i=1; i<=n; i++) {
scanf("%d",&c[i]);
cmin(mn[c[i]],dfn[i]);
}
for(int i=1; i<=n; i++)
if(dfn[i]>rr[idfn[mn[c[i]]]])
mn[c[i]]=0;
for(int i=1; i<=n; i++)
if(dfn[i]^mn[c[i]])
v[c[i]].push_back(c[f[i]]);
memset(dfn,0,sizeof(dfn));
clk=0;
for(int i=1; i<=k; i++)
if(!dfn[i]) tarjan(i);
for(int i=1; i<=k; i++)
for(int j=0; j<v[i].size(); j++)
if(bel[i]^bel[v[i][j]])
ban[bel[i]]=1;
for(int i=1; i<=scc; i++)
if(!ban[i]) ans=MIN(ans,sz[i]);
writeln(ans-1);
return 0;
}
Compilation message
capital_city.cpp:2: warning: ignoring '#pragma G ' [-Wunknown-pragmas]
2 | #pragma G++ optimize(3,"Ofast","inline")
|
capital_city.cpp: In function 'void dfs(int)':
capital_city.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0,y; i<e[x].size(); i++)
| ~^~~~~~~~~~~~
capital_city.cpp: In function 'void tarjan(int)':
capital_city.cpp:74:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i=0,y; i<v[x].size(); i++)
| ~^~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:117:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int j=0; j<v[i].size(); j++)
| ~^~~~~~~~~~~~
capital_city.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d",&c[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
5 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
5 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
4 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
4 ms |
15552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
5 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
5 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
4 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
4 ms |
15552 KB |
Output is correct |
11 |
Correct |
4 ms |
15704 KB |
Output is correct |
12 |
Correct |
5 ms |
15704 KB |
Output is correct |
13 |
Correct |
5 ms |
15708 KB |
Output is correct |
14 |
Correct |
4 ms |
15708 KB |
Output is correct |
15 |
Correct |
5 ms |
15708 KB |
Output is correct |
16 |
Correct |
5 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15708 KB |
Output is correct |
18 |
Correct |
4 ms |
15792 KB |
Output is correct |
19 |
Correct |
5 ms |
15708 KB |
Output is correct |
20 |
Correct |
5 ms |
15708 KB |
Output is correct |
21 |
Correct |
4 ms |
15704 KB |
Output is correct |
22 |
Correct |
5 ms |
15964 KB |
Output is correct |
23 |
Correct |
6 ms |
15708 KB |
Output is correct |
24 |
Correct |
4 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
15704 KB |
Output is correct |
26 |
Correct |
5 ms |
15708 KB |
Output is correct |
27 |
Correct |
4 ms |
15728 KB |
Output is correct |
28 |
Correct |
5 ms |
15964 KB |
Output is correct |
29 |
Correct |
5 ms |
15708 KB |
Output is correct |
30 |
Correct |
4 ms |
15704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
35664 KB |
Output is correct |
2 |
Correct |
55 ms |
35556 KB |
Output is correct |
3 |
Correct |
78 ms |
35240 KB |
Output is correct |
4 |
Correct |
48 ms |
35704 KB |
Output is correct |
5 |
Correct |
77 ms |
34132 KB |
Output is correct |
6 |
Correct |
50 ms |
35664 KB |
Output is correct |
7 |
Correct |
81 ms |
34128 KB |
Output is correct |
8 |
Correct |
52 ms |
35276 KB |
Output is correct |
9 |
Correct |
73 ms |
32336 KB |
Output is correct |
10 |
Correct |
79 ms |
31404 KB |
Output is correct |
11 |
Correct |
75 ms |
32596 KB |
Output is correct |
12 |
Correct |
75 ms |
33588 KB |
Output is correct |
13 |
Correct |
74 ms |
31312 KB |
Output is correct |
14 |
Correct |
72 ms |
33616 KB |
Output is correct |
15 |
Correct |
102 ms |
33536 KB |
Output is correct |
16 |
Correct |
96 ms |
31612 KB |
Output is correct |
17 |
Correct |
73 ms |
32084 KB |
Output is correct |
18 |
Correct |
83 ms |
32224 KB |
Output is correct |
19 |
Correct |
75 ms |
33360 KB |
Output is correct |
20 |
Correct |
79 ms |
34156 KB |
Output is correct |
21 |
Correct |
4 ms |
15708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
5 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
5 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
4 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
4 ms |
15552 KB |
Output is correct |
11 |
Correct |
4 ms |
15704 KB |
Output is correct |
12 |
Correct |
5 ms |
15704 KB |
Output is correct |
13 |
Correct |
5 ms |
15708 KB |
Output is correct |
14 |
Correct |
4 ms |
15708 KB |
Output is correct |
15 |
Correct |
5 ms |
15708 KB |
Output is correct |
16 |
Correct |
5 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15708 KB |
Output is correct |
18 |
Correct |
4 ms |
15792 KB |
Output is correct |
19 |
Correct |
5 ms |
15708 KB |
Output is correct |
20 |
Correct |
5 ms |
15708 KB |
Output is correct |
21 |
Correct |
4 ms |
15704 KB |
Output is correct |
22 |
Correct |
5 ms |
15964 KB |
Output is correct |
23 |
Correct |
6 ms |
15708 KB |
Output is correct |
24 |
Correct |
4 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
15704 KB |
Output is correct |
26 |
Correct |
5 ms |
15708 KB |
Output is correct |
27 |
Correct |
4 ms |
15728 KB |
Output is correct |
28 |
Correct |
5 ms |
15964 KB |
Output is correct |
29 |
Correct |
5 ms |
15708 KB |
Output is correct |
30 |
Correct |
4 ms |
15704 KB |
Output is correct |
31 |
Correct |
81 ms |
35664 KB |
Output is correct |
32 |
Correct |
55 ms |
35556 KB |
Output is correct |
33 |
Correct |
78 ms |
35240 KB |
Output is correct |
34 |
Correct |
48 ms |
35704 KB |
Output is correct |
35 |
Correct |
77 ms |
34132 KB |
Output is correct |
36 |
Correct |
50 ms |
35664 KB |
Output is correct |
37 |
Correct |
81 ms |
34128 KB |
Output is correct |
38 |
Correct |
52 ms |
35276 KB |
Output is correct |
39 |
Correct |
73 ms |
32336 KB |
Output is correct |
40 |
Correct |
79 ms |
31404 KB |
Output is correct |
41 |
Correct |
75 ms |
32596 KB |
Output is correct |
42 |
Correct |
75 ms |
33588 KB |
Output is correct |
43 |
Correct |
74 ms |
31312 KB |
Output is correct |
44 |
Correct |
72 ms |
33616 KB |
Output is correct |
45 |
Correct |
102 ms |
33536 KB |
Output is correct |
46 |
Correct |
96 ms |
31612 KB |
Output is correct |
47 |
Correct |
73 ms |
32084 KB |
Output is correct |
48 |
Correct |
83 ms |
32224 KB |
Output is correct |
49 |
Correct |
75 ms |
33360 KB |
Output is correct |
50 |
Correct |
79 ms |
34156 KB |
Output is correct |
51 |
Correct |
4 ms |
15708 KB |
Output is correct |
52 |
Correct |
96 ms |
28604 KB |
Output is correct |
53 |
Correct |
71 ms |
28748 KB |
Output is correct |
54 |
Correct |
75 ms |
28740 KB |
Output is correct |
55 |
Correct |
69 ms |
28772 KB |
Output is correct |
56 |
Correct |
91 ms |
28736 KB |
Output is correct |
57 |
Correct |
97 ms |
28636 KB |
Output is correct |
58 |
Correct |
68 ms |
29724 KB |
Output is correct |
59 |
Correct |
68 ms |
30032 KB |
Output is correct |
60 |
Correct |
88 ms |
30288 KB |
Output is correct |
61 |
Correct |
88 ms |
30084 KB |
Output is correct |
62 |
Correct |
50 ms |
35576 KB |
Output is correct |
63 |
Correct |
50 ms |
35468 KB |
Output is correct |
64 |
Correct |
61 ms |
35340 KB |
Output is correct |
65 |
Correct |
49 ms |
35564 KB |
Output is correct |
66 |
Correct |
65 ms |
31516 KB |
Output is correct |
67 |
Correct |
67 ms |
31432 KB |
Output is correct |
68 |
Correct |
69 ms |
31620 KB |
Output is correct |
69 |
Correct |
66 ms |
31652 KB |
Output is correct |
70 |
Correct |
99 ms |
31580 KB |
Output is correct |
71 |
Correct |
62 ms |
31420 KB |
Output is correct |
72 |
Correct |
69 ms |
31648 KB |
Output is correct |
73 |
Correct |
66 ms |
31176 KB |
Output is correct |
74 |
Correct |
67 ms |
31656 KB |
Output is correct |
75 |
Correct |
63 ms |
31560 KB |
Output is correct |
76 |
Correct |
73 ms |
32084 KB |
Output is correct |
77 |
Correct |
74 ms |
32128 KB |
Output is correct |
78 |
Correct |
101 ms |
32180 KB |
Output is correct |
79 |
Correct |
69 ms |
31040 KB |
Output is correct |
80 |
Correct |
82 ms |
34036 KB |
Output is correct |
81 |
Correct |
97 ms |
32552 KB |
Output is correct |
82 |
Correct |
86 ms |
32736 KB |
Output is correct |
83 |
Correct |
76 ms |
31304 KB |
Output is correct |
84 |
Correct |
88 ms |
33840 KB |
Output is correct |
85 |
Correct |
75 ms |
32848 KB |
Output is correct |
86 |
Correct |
77 ms |
31056 KB |
Output is correct |
87 |
Correct |
77 ms |
31872 KB |
Output is correct |
88 |
Correct |
73 ms |
32340 KB |
Output is correct |
89 |
Correct |
72 ms |
30900 KB |
Output is correct |
90 |
Correct |
69 ms |
30620 KB |
Output is correct |
91 |
Correct |
91 ms |
31792 KB |
Output is correct |
92 |
Correct |
84 ms |
31336 KB |
Output is correct |
93 |
Correct |
68 ms |
30988 KB |
Output is correct |
94 |
Correct |
76 ms |
30736 KB |
Output is correct |
95 |
Correct |
78 ms |
31452 KB |
Output is correct |
96 |
Correct |
75 ms |
30932 KB |
Output is correct |
97 |
Correct |
72 ms |
31572 KB |
Output is correct |