#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
vi adjList[200000];
int C[200000];
vi vv[200000];
int parent[200000],size[200000],depth[200000],heavy[200000];
int chain[200000],head[200000],disc[200000],csize[200000],order[200000],num = 0,c = 0;
int doDFS(int u,int p,int d) {
int i;
parent[u] = p,size[u] = 1,depth[u] = d,heavy[u] = -1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
size[u] += doDFS(v,u,d+1);
if ((heavy[u] == -1) || (size[v] > size[heavy[u]])) heavy[u] = v;
}
}
return size[u];
}
int doHLD(int u,int p) {
int i;
chain[u] = c,disc[u] = num++,order[num-1] = u;
if (csize[c] == 0) head[c] = u;
csize[c]++;
if (heavy[u] != -1) doHLD(heavy[u],u);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if ((v != p) && (v != heavy[u])) c++,doHLD(v,u);
}
return 0;
}
vi adjList2[1000000];
int m = 0;
int build(int s,int e,int i,int K) {
if (s == e) {
adjList2[i+K].pb(C[order[s]]);
m = max(m,i+K+1);
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i+1,K),build(mid+1,e,2*i+2,K);
adjList2[i+K].pb(2*i+1+K),adjList2[i+K].pb(2*i+2+K);
return 0;
}
int add(int s,int e,int as,int ae,int i,int u,int K) {
if ((s > ae) || (e < as)) return 0;
else if ((s >= as) && (e <= ae)) {
adjList2[u].pb(i+K);
return 0;
}
int mid = (s+e) / 2;
add(s,mid,as,ae,2*i+1,u,K),add(mid+1,e,as,ae,2*i+2,u,K);
return 0;
}
int disc2[1000000],low[1000000],inStack[1000000],com[1000000],val[1000000];
vi S;
int doDFS2(int u) {
int i;
disc2[u] = low[u] = num++;
S.pb(u),inStack[u] = 1;
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (disc2[v] == -1) doDFS2(v),low[u] = min(low[u],low[v]);
else if (inStack[v]) low[u] = min(low[u],disc2[v]);
}
if (disc2[u] == low[u]) {
while (1) {
int v = S.back();
S.pop_back(),inStack[v] = 0;
com[v] = c;
if (u == v) break;
}
c++;
}
return 0;
}
int main() {
int i;
int N,K,A,B;
scanf("%d %d",&N,&K);
for (i = 0; i < N-1; i++) {
scanf("%d %d",&A,&B);
A--,B--;
adjList[A].pb(B);
adjList[B].pb(A);
}
for (i = 0; i < N; i++) scanf("%d",&C[i]),vv[--C[i]].pb(i);
int j;
doDFS(0,-1,0),doHLD(0,-1);
build(0,N-1,0,K);
for (i = 0; i < K; i++) {
for (j = 1; j < vv[i].size(); j++) {
int u = vv[i][j-1],v = vv[i][j];
while (chain[u] != chain[v]) {
if (depth[head[chain[u]]] < depth[head[chain[v]]]) swap(u,v);
add(0,N-1,disc[head[chain[u]]],disc[u],0,i,K);
u = parent[head[chain[u]]];
}
if (depth[u] > depth[v]) swap(u,v);
add(0,N-1,disc[u],disc[v],0,i,K);
}
}
num = c = 0;
for (i = 0; i < m; i++) disc2[i] = low[i] = -1;
for (i = 0; i < m; i++) {
if (disc2[i] == -1) doDFS2(i);
}
int ans = K;
for (i = 0; i < m; i++) {
if (i < K) val[com[i]]++;
for (j = 0; j < adjList2[i].size(); j++) {
if (com[adjList2[i][j]] != com[i]) val[com[i]] = 1e9;
}
}
for (i = 0; i < c; i++) {
if (val[i] > 0) ans = min(ans,val[i]);
}
printf("%d\n",ans-1);
return 0;
}
Compilation message
capital_city.cpp: In function 'int doDFS(int, int, int)':
capital_city.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int doHLD(int, int)':
capital_city.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int doDFS2(int)':
capital_city.cpp:118:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList2[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:150:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 1; j < vv[i].size(); j++) {
~~^~~~~~~~~~~~~~
capital_city.cpp:169:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < adjList2[i].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~
capital_city.cpp:137:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:144:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&C[i]),vv[--C[i]].pb(i);
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
33280 KB |
Output is correct |
2 |
Correct |
24 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33232 KB |
Output is correct |
5 |
Correct |
24 ms |
33280 KB |
Output is correct |
6 |
Correct |
25 ms |
33272 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
30 ms |
33272 KB |
Output is correct |
10 |
Correct |
24 ms |
33280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
33280 KB |
Output is correct |
2 |
Correct |
24 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33232 KB |
Output is correct |
5 |
Correct |
24 ms |
33280 KB |
Output is correct |
6 |
Correct |
25 ms |
33272 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
30 ms |
33272 KB |
Output is correct |
10 |
Correct |
24 ms |
33280 KB |
Output is correct |
11 |
Correct |
30 ms |
33792 KB |
Output is correct |
12 |
Correct |
28 ms |
33792 KB |
Output is correct |
13 |
Correct |
28 ms |
33792 KB |
Output is correct |
14 |
Correct |
28 ms |
33792 KB |
Output is correct |
15 |
Correct |
27 ms |
33792 KB |
Output is correct |
16 |
Correct |
27 ms |
33792 KB |
Output is correct |
17 |
Correct |
27 ms |
33792 KB |
Output is correct |
18 |
Correct |
27 ms |
33792 KB |
Output is correct |
19 |
Correct |
27 ms |
33784 KB |
Output is correct |
20 |
Correct |
26 ms |
33792 KB |
Output is correct |
21 |
Correct |
29 ms |
33792 KB |
Output is correct |
22 |
Correct |
27 ms |
33920 KB |
Output is correct |
23 |
Correct |
27 ms |
33912 KB |
Output is correct |
24 |
Correct |
26 ms |
33792 KB |
Output is correct |
25 |
Correct |
26 ms |
33792 KB |
Output is correct |
26 |
Correct |
26 ms |
33920 KB |
Output is correct |
27 |
Correct |
26 ms |
33792 KB |
Output is correct |
28 |
Correct |
28 ms |
33792 KB |
Output is correct |
29 |
Correct |
27 ms |
33792 KB |
Output is correct |
30 |
Correct |
26 ms |
33792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
454 ms |
101496 KB |
Output is correct |
2 |
Correct |
314 ms |
102028 KB |
Output is correct |
3 |
Correct |
431 ms |
101244 KB |
Output is correct |
4 |
Correct |
323 ms |
102012 KB |
Output is correct |
5 |
Correct |
449 ms |
98416 KB |
Output is correct |
6 |
Correct |
315 ms |
101836 KB |
Output is correct |
7 |
Correct |
424 ms |
98536 KB |
Output is correct |
8 |
Correct |
296 ms |
99700 KB |
Output is correct |
9 |
Correct |
423 ms |
93164 KB |
Output is correct |
10 |
Correct |
429 ms |
90896 KB |
Output is correct |
11 |
Correct |
412 ms |
93984 KB |
Output is correct |
12 |
Correct |
420 ms |
96104 KB |
Output is correct |
13 |
Correct |
403 ms |
90080 KB |
Output is correct |
14 |
Correct |
413 ms |
96344 KB |
Output is correct |
15 |
Correct |
423 ms |
95972 KB |
Output is correct |
16 |
Correct |
409 ms |
92272 KB |
Output is correct |
17 |
Correct |
404 ms |
91856 KB |
Output is correct |
18 |
Correct |
411 ms |
93408 KB |
Output is correct |
19 |
Correct |
417 ms |
95196 KB |
Output is correct |
20 |
Correct |
410 ms |
97004 KB |
Output is correct |
21 |
Correct |
24 ms |
33280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
33280 KB |
Output is correct |
2 |
Correct |
24 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33232 KB |
Output is correct |
5 |
Correct |
24 ms |
33280 KB |
Output is correct |
6 |
Correct |
25 ms |
33272 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
30 ms |
33272 KB |
Output is correct |
10 |
Correct |
24 ms |
33280 KB |
Output is correct |
11 |
Correct |
30 ms |
33792 KB |
Output is correct |
12 |
Correct |
28 ms |
33792 KB |
Output is correct |
13 |
Correct |
28 ms |
33792 KB |
Output is correct |
14 |
Correct |
28 ms |
33792 KB |
Output is correct |
15 |
Correct |
27 ms |
33792 KB |
Output is correct |
16 |
Correct |
27 ms |
33792 KB |
Output is correct |
17 |
Correct |
27 ms |
33792 KB |
Output is correct |
18 |
Correct |
27 ms |
33792 KB |
Output is correct |
19 |
Correct |
27 ms |
33784 KB |
Output is correct |
20 |
Correct |
26 ms |
33792 KB |
Output is correct |
21 |
Correct |
29 ms |
33792 KB |
Output is correct |
22 |
Correct |
27 ms |
33920 KB |
Output is correct |
23 |
Correct |
27 ms |
33912 KB |
Output is correct |
24 |
Correct |
26 ms |
33792 KB |
Output is correct |
25 |
Correct |
26 ms |
33792 KB |
Output is correct |
26 |
Correct |
26 ms |
33920 KB |
Output is correct |
27 |
Correct |
26 ms |
33792 KB |
Output is correct |
28 |
Correct |
28 ms |
33792 KB |
Output is correct |
29 |
Correct |
27 ms |
33792 KB |
Output is correct |
30 |
Correct |
26 ms |
33792 KB |
Output is correct |
31 |
Correct |
454 ms |
101496 KB |
Output is correct |
32 |
Correct |
314 ms |
102028 KB |
Output is correct |
33 |
Correct |
431 ms |
101244 KB |
Output is correct |
34 |
Correct |
323 ms |
102012 KB |
Output is correct |
35 |
Correct |
449 ms |
98416 KB |
Output is correct |
36 |
Correct |
315 ms |
101836 KB |
Output is correct |
37 |
Correct |
424 ms |
98536 KB |
Output is correct |
38 |
Correct |
296 ms |
99700 KB |
Output is correct |
39 |
Correct |
423 ms |
93164 KB |
Output is correct |
40 |
Correct |
429 ms |
90896 KB |
Output is correct |
41 |
Correct |
412 ms |
93984 KB |
Output is correct |
42 |
Correct |
420 ms |
96104 KB |
Output is correct |
43 |
Correct |
403 ms |
90080 KB |
Output is correct |
44 |
Correct |
413 ms |
96344 KB |
Output is correct |
45 |
Correct |
423 ms |
95972 KB |
Output is correct |
46 |
Correct |
409 ms |
92272 KB |
Output is correct |
47 |
Correct |
404 ms |
91856 KB |
Output is correct |
48 |
Correct |
411 ms |
93408 KB |
Output is correct |
49 |
Correct |
417 ms |
95196 KB |
Output is correct |
50 |
Correct |
410 ms |
97004 KB |
Output is correct |
51 |
Correct |
24 ms |
33280 KB |
Output is correct |
52 |
Correct |
935 ms |
91620 KB |
Output is correct |
53 |
Correct |
961 ms |
92264 KB |
Output is correct |
54 |
Correct |
901 ms |
92320 KB |
Output is correct |
55 |
Correct |
940 ms |
92048 KB |
Output is correct |
56 |
Correct |
947 ms |
92076 KB |
Output is correct |
57 |
Correct |
931 ms |
91768 KB |
Output is correct |
58 |
Correct |
479 ms |
87408 KB |
Output is correct |
59 |
Correct |
477 ms |
88308 KB |
Output is correct |
60 |
Correct |
578 ms |
90484 KB |
Output is correct |
61 |
Correct |
557 ms |
91640 KB |
Output is correct |
62 |
Correct |
316 ms |
102012 KB |
Output is correct |
63 |
Correct |
307 ms |
102008 KB |
Output is correct |
64 |
Correct |
309 ms |
100212 KB |
Output is correct |
65 |
Correct |
315 ms |
101864 KB |
Output is correct |
66 |
Correct |
414 ms |
94420 KB |
Output is correct |
67 |
Correct |
445 ms |
94428 KB |
Output is correct |
68 |
Correct |
434 ms |
94420 KB |
Output is correct |
69 |
Correct |
417 ms |
94420 KB |
Output is correct |
70 |
Correct |
428 ms |
94416 KB |
Output is correct |
71 |
Correct |
426 ms |
94292 KB |
Output is correct |
72 |
Correct |
414 ms |
94420 KB |
Output is correct |
73 |
Correct |
426 ms |
93652 KB |
Output is correct |
74 |
Correct |
433 ms |
94480 KB |
Output is correct |
75 |
Correct |
416 ms |
94420 KB |
Output is correct |
76 |
Correct |
394 ms |
86000 KB |
Output is correct |
77 |
Correct |
418 ms |
84208 KB |
Output is correct |
78 |
Correct |
410 ms |
92000 KB |
Output is correct |
79 |
Correct |
419 ms |
89484 KB |
Output is correct |
80 |
Correct |
403 ms |
97764 KB |
Output is correct |
81 |
Correct |
415 ms |
93800 KB |
Output is correct |
82 |
Correct |
420 ms |
93784 KB |
Output is correct |
83 |
Correct |
399 ms |
91240 KB |
Output is correct |
84 |
Correct |
414 ms |
96368 KB |
Output is correct |
85 |
Correct |
446 ms |
95696 KB |
Output is correct |
86 |
Correct |
396 ms |
89304 KB |
Output is correct |
87 |
Correct |
413 ms |
91620 KB |
Output is correct |
88 |
Correct |
409 ms |
93868 KB |
Output is correct |
89 |
Correct |
416 ms |
90176 KB |
Output is correct |
90 |
Correct |
406 ms |
90980 KB |
Output is correct |
91 |
Correct |
431 ms |
93544 KB |
Output is correct |
92 |
Correct |
436 ms |
90840 KB |
Output is correct |
93 |
Correct |
421 ms |
90328 KB |
Output is correct |
94 |
Correct |
414 ms |
89620 KB |
Output is correct |
95 |
Correct |
420 ms |
91360 KB |
Output is correct |
96 |
Correct |
428 ms |
90552 KB |
Output is correct |
97 |
Correct |
440 ms |
93788 KB |
Output is correct |