This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |