Submission #347990

# Submission time Handle Problem Language Result Execution time Memory
347990 2021-01-14T02:28:03 Z YJU Unique Cities (JOI19_ho_t5) C++14
100 / 100
475 ms 52588 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=4e5+5;
const ll K=350;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll n,m,x,y,cnt[N],stk[N],res,ans[N],ed,h[N],d[N],son[N][2],rt,col[N];
vector<ll> v[N];

void add(ll k){
	if(cnt[k]==0)++res;
	++cnt[k];
}

void del(ll k){
	if(cnt[k]==1)--res;
	--cnt[k];
}

void DFS1(ll nd,ll pa){
	d[nd]=d[pa]+1;
	if(d[nd]>d[rt])rt=nd;
	for(auto i:v[nd]){
		if(i==pa)continue;
		DFS1(i,nd);
	}
}

void DFS2(ll nd,ll pa){
	d[nd]=d[pa]+1;
	son[nd][0]=son[nd][1]=0;
	for(auto i:v[nd]){
		if(i==pa)continue;
		DFS2(i,nd);
		for(ll j=0,tmp=i;j<2;j++){
			if(h[tmp]>=h[son[nd][j]])swap(tmp,son[nd][j]);
		}
	}
	h[nd]=h[son[nd][0]]+1;
}

void DFS3(ll nd,ll pa){
	if(pa){add(col[pa]);stk[++ed]=pa;}
	if(son[nd][0]){
		while(~ed&&d[stk[ed]]>=d[nd]-h[son[nd][1]])del(col[stk[ed]]),--ed;
		DFS3(son[nd][0],nd);
	}
	while (~ed&& d[stk[ed]] >= d[nd] - h[son[nd][0]])del(col[stk[ed]]),--ed;
	for(auto i:v[nd]){
		if(i==pa||i==son[nd][0])continue;
		DFS3(i,nd);
	}
	ans[nd]=max(ans[nd],res);
	if(stk[ed]==pa)del(col[stk[ed]]),--ed;
}

void work(ll nd){
	rt=nd;
	DFS1(nd,0);
	DFS2(rt,0);
	REP1(i,m)cnt[i]=0;
	res=0;
	ed=-1;
	DFS3(rt,0);
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;
	REP(i,n-1){
		cin>>x>>y;
		v[x].pb(y);v[y].pb(x);
	}
	REP1(i,n)cin>>col[i];
	//d[0]=-1;
	work(1);
	work(rt);
	REP1(i,n)cout<<ans[i]<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 9836 KB Output is correct
2 Correct 8 ms 9984 KB Output is correct
3 Correct 7 ms 9976 KB Output is correct
4 Correct 8 ms 10092 KB Output is correct
5 Correct 8 ms 9964 KB Output is correct
6 Correct 8 ms 10220 KB Output is correct
7 Correct 8 ms 10092 KB Output is correct
8 Correct 8 ms 9964 KB Output is correct
9 Correct 8 ms 9964 KB Output is correct
10 Correct 8 ms 9964 KB Output is correct
11 Correct 8 ms 9964 KB Output is correct
12 Correct 7 ms 9964 KB Output is correct
13 Correct 8 ms 10220 KB Output is correct
14 Correct 8 ms 10092 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 8 ms 9984 KB Output is correct
17 Correct 8 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 10240 KB Output is correct
21 Correct 8 ms 10092 KB Output is correct
22 Correct 8 ms 9964 KB Output is correct
23 Correct 8 ms 10092 KB Output is correct
24 Correct 8 ms 10092 KB Output is correct
25 Correct 8 ms 9964 KB Output is correct
26 Correct 8 ms 9964 KB Output is correct
27 Correct 8 ms 10220 KB Output is correct
28 Correct 8 ms 10092 KB Output is correct
29 Correct 8 ms 10092 KB Output is correct
30 Correct 8 ms 9964 KB Output is correct
31 Correct 8 ms 10092 KB Output is correct
32 Correct 8 ms 10092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 21228 KB Output is correct
2 Correct 217 ms 32364 KB Output is correct
3 Correct 32 ms 13952 KB Output is correct
4 Correct 409 ms 30004 KB Output is correct
5 Correct 449 ms 49132 KB Output is correct
6 Correct 432 ms 38892 KB Output is correct
7 Correct 390 ms 29924 KB Output is correct
8 Correct 406 ms 31980 KB Output is correct
9 Correct 414 ms 31212 KB Output is correct
10 Correct 410 ms 31084 KB Output is correct
11 Correct 230 ms 30300 KB Output is correct
12 Correct 439 ms 42092 KB Output is correct
13 Correct 389 ms 39396 KB Output is correct
14 Correct 403 ms 38372 KB Output is correct
15 Correct 211 ms 30264 KB Output is correct
16 Correct 384 ms 42936 KB Output is correct
17 Correct 391 ms 38884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 27500 KB Output is correct
2 Correct 444 ms 50540 KB Output is correct
3 Correct 36 ms 14828 KB Output is correct
4 Correct 406 ms 32492 KB Output is correct
5 Correct 469 ms 52588 KB Output is correct
6 Correct 448 ms 41580 KB Output is correct
7 Correct 390 ms 32232 KB Output is correct
8 Correct 431 ms 36576 KB Output is correct
9 Correct 412 ms 35052 KB Output is correct
10 Correct 415 ms 33772 KB Output is correct
11 Correct 329 ms 32352 KB Output is correct
12 Correct 470 ms 48620 KB Output is correct
13 Correct 414 ms 40804 KB Output is correct
14 Correct 425 ms 40676 KB Output is correct
15 Correct 217 ms 32988 KB Output is correct
16 Correct 399 ms 45920 KB Output is correct
17 Correct 410 ms 41572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9836 KB Output is correct
2 Correct 8 ms 9984 KB Output is correct
3 Correct 7 ms 9976 KB Output is correct
4 Correct 8 ms 10092 KB Output is correct
5 Correct 8 ms 9964 KB Output is correct
6 Correct 8 ms 10220 KB Output is correct
7 Correct 8 ms 10092 KB Output is correct
8 Correct 8 ms 9964 KB Output is correct
9 Correct 8 ms 9964 KB Output is correct
10 Correct 8 ms 9964 KB Output is correct
11 Correct 8 ms 9964 KB Output is correct
12 Correct 7 ms 9964 KB Output is correct
13 Correct 8 ms 10220 KB Output is correct
14 Correct 8 ms 10092 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 8 ms 9984 KB Output is correct
17 Correct 8 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 10240 KB Output is correct
21 Correct 8 ms 10092 KB Output is correct
22 Correct 8 ms 9964 KB Output is correct
23 Correct 8 ms 10092 KB Output is correct
24 Correct 8 ms 10092 KB Output is correct
25 Correct 8 ms 9964 KB Output is correct
26 Correct 8 ms 9964 KB Output is correct
27 Correct 8 ms 10220 KB Output is correct
28 Correct 8 ms 10092 KB Output is correct
29 Correct 8 ms 10092 KB Output is correct
30 Correct 8 ms 9964 KB Output is correct
31 Correct 8 ms 10092 KB Output is correct
32 Correct 8 ms 10092 KB Output is correct
33 Correct 193 ms 21228 KB Output is correct
34 Correct 217 ms 32364 KB Output is correct
35 Correct 32 ms 13952 KB Output is correct
36 Correct 409 ms 30004 KB Output is correct
37 Correct 449 ms 49132 KB Output is correct
38 Correct 432 ms 38892 KB Output is correct
39 Correct 390 ms 29924 KB Output is correct
40 Correct 406 ms 31980 KB Output is correct
41 Correct 414 ms 31212 KB Output is correct
42 Correct 410 ms 31084 KB Output is correct
43 Correct 230 ms 30300 KB Output is correct
44 Correct 439 ms 42092 KB Output is correct
45 Correct 389 ms 39396 KB Output is correct
46 Correct 403 ms 38372 KB Output is correct
47 Correct 211 ms 30264 KB Output is correct
48 Correct 384 ms 42936 KB Output is correct
49 Correct 391 ms 38884 KB Output is correct
50 Correct 304 ms 27500 KB Output is correct
51 Correct 444 ms 50540 KB Output is correct
52 Correct 36 ms 14828 KB Output is correct
53 Correct 406 ms 32492 KB Output is correct
54 Correct 469 ms 52588 KB Output is correct
55 Correct 448 ms 41580 KB Output is correct
56 Correct 390 ms 32232 KB Output is correct
57 Correct 431 ms 36576 KB Output is correct
58 Correct 412 ms 35052 KB Output is correct
59 Correct 415 ms 33772 KB Output is correct
60 Correct 329 ms 32352 KB Output is correct
61 Correct 470 ms 48620 KB Output is correct
62 Correct 414 ms 40804 KB Output is correct
63 Correct 425 ms 40676 KB Output is correct
64 Correct 217 ms 32988 KB Output is correct
65 Correct 399 ms 45920 KB Output is correct
66 Correct 410 ms 41572 KB Output is correct
67 Correct 29 ms 12780 KB Output is correct
68 Correct 144 ms 27628 KB Output is correct
69 Correct 244 ms 28524 KB Output is correct
70 Correct 404 ms 29804 KB Output is correct
71 Correct 456 ms 49172 KB Output is correct
72 Correct 444 ms 38892 KB Output is correct
73 Correct 383 ms 29928 KB Output is correct
74 Correct 428 ms 33572 KB Output is correct
75 Correct 396 ms 31724 KB Output is correct
76 Correct 423 ms 31340 KB Output is correct
77 Correct 314 ms 29892 KB Output is correct
78 Correct 464 ms 44288 KB Output is correct
79 Correct 406 ms 41444 KB Output is correct
80 Correct 400 ms 37220 KB Output is correct
81 Correct 212 ms 30044 KB Output is correct
82 Correct 391 ms 42976 KB Output is correct
83 Correct 404 ms 39012 KB Output is correct
84 Correct 407 ms 30188 KB Output is correct
85 Correct 474 ms 49772 KB Output is correct
86 Correct 441 ms 39300 KB Output is correct
87 Correct 388 ms 30184 KB Output is correct
88 Correct 418 ms 34284 KB Output is correct
89 Correct 409 ms 32492 KB Output is correct
90 Correct 407 ms 31724 KB Output is correct
91 Correct 306 ms 30432 KB Output is correct
92 Correct 454 ms 49260 KB Output is correct
93 Correct 407 ms 36836 KB Output is correct
94 Correct 403 ms 35048 KB Output is correct
95 Correct 217 ms 30556 KB Output is correct
96 Correct 392 ms 43560 KB Output is correct
97 Correct 402 ms 39676 KB Output is correct
98 Correct 408 ms 32364 KB Output is correct
99 Correct 475 ms 50412 KB Output is correct
100 Correct 440 ms 41196 KB Output is correct
101 Correct 374 ms 30820 KB Output is correct
102 Correct 406 ms 34140 KB Output is correct
103 Correct 417 ms 32876 KB Output is correct
104 Correct 435 ms 32392 KB Output is correct
105 Correct 313 ms 30816 KB Output is correct
106 Correct 464 ms 42732 KB Output is correct
107 Correct 425 ms 42472 KB Output is correct
108 Correct 433 ms 38068 KB Output is correct
109 Correct 220 ms 32024 KB Output is correct
110 Correct 418 ms 44384 KB Output is correct
111 Correct 417 ms 40732 KB Output is correct