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];
int depth[200000],height[200000];
int doDFS(int u,int p,int d) {
int i;
depth[u] = d,height[u] = 0;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) height[u] = max(height[u],doDFS(v,u,d+1)+1);
}
return height[u];
}
vi path;
int c[200000],n = 0;
int push(int x) {
path.pb(x),c[C[x]]++;
if (c[C[x]] == 1) n++;
return 0;
}
int pop() {
c[C[path.back()]]--;
if (c[C[path.back()]] == 0) n--;
path.pop_back();
return 0;
}
int ans[200000];
int doDFS2(int u,int p) {
int i;
int m1 = 0,m2 = 0,mi = -1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
if (height[v]+1 >= m1) m2 = m1,m1 = height[v]+1,mi = i;
else if (height[v]+1 >= m2) m2 = height[v]+1;
}
}
if (mi != -1) swap(adjList[u][0],adjList[u][mi]);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
while (!path.empty() && (depth[path.back()] >= depth[u]-((i == 0) ? m2:m1))) pop();
push(u),doDFS2(v,u);
if (!path.empty() && (path.back() == u)) pop();
}
}
while (!path.empty() && (depth[path.back()] >= depth[u]-m1)) pop();
ans[u] = max(ans[u],n);
return 0;
}
int main() {
int i;
int N,M,A,B;
scanf("%d %d",&N,&M);
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]),C[i]--;
A = B = 0;
doDFS(0,-1,0);
for (i = 0; i < N; i++) {
if (depth[i] > depth[A]) A = i;
}
doDFS(A,-1,0),doDFS2(A,-1);
for (i = 0; i < N; i++) {
if (depth[i] > depth[B]) B = i;
}
doDFS(B,-1,0),doDFS2(B,-1);
for (i = 0; i < N; i++) printf("%d\n",ans[i]);
return 0;
}
Compilation message (stderr)
joi2019_ho_t5.cpp: In function 'int doDFS(int, int, int)':
joi2019_ho_t5.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int doDFS2(int, int)':
joi2019_ho_t5.cpp:87:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:95:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:117: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]),C[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... |