Submission #542619

# Submission time Handle Problem Language Result Execution time Memory
542619 2022-03-27T07:33:35 Z alvingogo Unique Cities (JOI19_ho_t5) C++14
100 / 100
449 ms 39600 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define cd complex<double>
#define p_q priority_queue
using namespace std;

struct no{
	vector<int> ch;
	int co;
	int dis;
	int hh=0;
	int ans=0;
};
vector<no> v;
vector<int> cnt;
int cz=0;
int t=0;
void dfs1(int r,int f,int dis){
	if(t==-1 || dis>v[t].dis){
		t=r;
	}
	v[r].dis=dis;
	for(auto y:v[r].ch){
		if(y!=f){
			dfs1(y,r,dis+1);
		}
	}
}
stack<int> s;
void dfs2(int r,int f,int dis){
	v[r].hh=v[r].dis=dis;
	for(auto y:v[r].ch){
		if(y!=f){
			dfs2(y,r,dis+1);
			v[r].hh=max(v[r].hh,v[y].hh);
		}
	}
}
void add(int r){
	s.push(r);
	cnt[v[r].co]++;
	if(cnt[v[r].co]==1){
		cz++;
	}
}
void del(){
	int r=s.top();
	s.pop();
	cnt[v[r].co]--;
	if(cnt[v[r].co]==0){
		cz--;
	}
}
void dfs3(int r,int f){
	while(s.size() && v[s.top()].dis>v[r].dis){
		del();
	}
	int mx=0,mn=0;
	for(auto y:v[r].ch){
		if(y!=f){
			if(v[y].hh>mx){
				mn=mx;
				mx=v[y].hh;
			}
			else if(v[y].hh>mn){
				mn=v[y].hh;
			}
		}
	}
	int z=-1;
	for(auto y:v[r].ch){
		if(y!=f){
			if(v[y].hh==mx){
				z=y;
				while(s.size() && v[s.top()].dis>=v[r].dis-(mn-v[r].dis)){
					del();
				}
				add(r);
				dfs3(y,r);
				break;
			}
		}
	}
	while(s.size() && v[s.top()].dis>=v[r].dis-(mx-v[r].dis)){
		del();
	}
	for(auto y:v[r].ch){
		if(y!=f && y!=z){
			if(!s.size() || s.top()!=r){
				add(r);
			}
			dfs3(y,r);
		}
	}
	if(s.size() && s.top()==r){
		del();
	}
	v[r].ans=max(v[r].ans,cz);
}
int main(){
	AquA;
	int n,m;
	cin >> n >> m;
	v.resize(n);
	cnt.resize(m+1);
	for(int i=1;i<n;i++){
		int a,b;
		cin >> a >> b;
		a--;
		b--;
		v[a].ch.push_back(b);
		v[b].ch.push_back(a);
	}
	for(int i=0;i<n;i++){
		cin >> v[i].co;
	}
	int y=0;
	dfs1(y,y,0);
	int a=t;
	t=-1;
	dfs1(a,a,0);
	int b=t;
	for(int i=0;i<2;i++){
		dfs2(a,a,0);
		dfs3(a,a);
		a=b;
	}
	for(int i=0;i<n;i++){
		cout << v[i].ans << "\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 464 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 10336 KB Output is correct
2 Correct 189 ms 21404 KB Output is correct
3 Correct 26 ms 4108 KB Output is correct
4 Correct 312 ms 17728 KB Output is correct
5 Correct 441 ms 37092 KB Output is correct
6 Correct 449 ms 27392 KB Output is correct
7 Correct 298 ms 17884 KB Output is correct
8 Correct 339 ms 19728 KB Output is correct
9 Correct 286 ms 19116 KB Output is correct
10 Correct 294 ms 18988 KB Output is correct
11 Correct 200 ms 18548 KB Output is correct
12 Correct 366 ms 29756 KB Output is correct
13 Correct 340 ms 27148 KB Output is correct
14 Correct 338 ms 26568 KB Output is correct
15 Correct 197 ms 18324 KB Output is correct
16 Correct 369 ms 31088 KB Output is correct
17 Correct 362 ms 27492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 15460 KB Output is correct
2 Correct 388 ms 37896 KB Output is correct
3 Correct 29 ms 4792 KB Output is correct
4 Correct 304 ms 19504 KB Output is correct
5 Correct 434 ms 39600 KB Output is correct
6 Correct 381 ms 29244 KB Output is correct
7 Correct 332 ms 19464 KB Output is correct
8 Correct 334 ms 23568 KB Output is correct
9 Correct 364 ms 22160 KB Output is correct
10 Correct 302 ms 21012 KB Output is correct
11 Correct 254 ms 19756 KB Output is correct
12 Correct 401 ms 35652 KB Output is correct
13 Correct 316 ms 28044 KB Output is correct
14 Correct 386 ms 28148 KB Output is correct
15 Correct 136 ms 20272 KB Output is correct
16 Correct 335 ms 33308 KB Output is correct
17 Correct 343 ms 29392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 464 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 174 ms 10336 KB Output is correct
34 Correct 189 ms 21404 KB Output is correct
35 Correct 26 ms 4108 KB Output is correct
36 Correct 312 ms 17728 KB Output is correct
37 Correct 441 ms 37092 KB Output is correct
38 Correct 449 ms 27392 KB Output is correct
39 Correct 298 ms 17884 KB Output is correct
40 Correct 339 ms 19728 KB Output is correct
41 Correct 286 ms 19116 KB Output is correct
42 Correct 294 ms 18988 KB Output is correct
43 Correct 200 ms 18548 KB Output is correct
44 Correct 366 ms 29756 KB Output is correct
45 Correct 340 ms 27148 KB Output is correct
46 Correct 338 ms 26568 KB Output is correct
47 Correct 197 ms 18324 KB Output is correct
48 Correct 369 ms 31088 KB Output is correct
49 Correct 362 ms 27492 KB Output is correct
50 Correct 277 ms 15460 KB Output is correct
51 Correct 388 ms 37896 KB Output is correct
52 Correct 29 ms 4792 KB Output is correct
53 Correct 304 ms 19504 KB Output is correct
54 Correct 434 ms 39600 KB Output is correct
55 Correct 381 ms 29244 KB Output is correct
56 Correct 332 ms 19464 KB Output is correct
57 Correct 334 ms 23568 KB Output is correct
58 Correct 364 ms 22160 KB Output is correct
59 Correct 302 ms 21012 KB Output is correct
60 Correct 254 ms 19756 KB Output is correct
61 Correct 401 ms 35652 KB Output is correct
62 Correct 316 ms 28044 KB Output is correct
63 Correct 386 ms 28148 KB Output is correct
64 Correct 136 ms 20272 KB Output is correct
65 Correct 335 ms 33308 KB Output is correct
66 Correct 343 ms 29392 KB Output is correct
67 Correct 23 ms 2764 KB Output is correct
68 Correct 143 ms 16888 KB Output is correct
69 Correct 181 ms 17620 KB Output is correct
70 Correct 312 ms 17768 KB Output is correct
71 Correct 427 ms 37124 KB Output is correct
72 Correct 387 ms 27328 KB Output is correct
73 Correct 298 ms 17888 KB Output is correct
74 Correct 316 ms 21452 KB Output is correct
75 Correct 298 ms 19624 KB Output is correct
76 Correct 299 ms 19256 KB Output is correct
77 Correct 202 ms 18164 KB Output is correct
78 Correct 380 ms 31800 KB Output is correct
79 Correct 317 ms 29640 KB Output is correct
80 Correct 336 ms 25508 KB Output is correct
81 Correct 135 ms 18116 KB Output is correct
82 Correct 310 ms 31132 KB Output is correct
83 Correct 361 ms 27436 KB Output is correct
84 Correct 311 ms 18028 KB Output is correct
85 Correct 404 ms 37744 KB Output is correct
86 Correct 367 ms 27744 KB Output is correct
87 Correct 310 ms 18044 KB Output is correct
88 Correct 330 ms 22280 KB Output is correct
89 Correct 303 ms 20276 KB Output is correct
90 Correct 308 ms 19628 KB Output is correct
91 Correct 231 ms 18692 KB Output is correct
92 Correct 388 ms 37104 KB Output is correct
93 Correct 330 ms 24820 KB Output is correct
94 Correct 353 ms 23040 KB Output is correct
95 Correct 137 ms 18760 KB Output is correct
96 Correct 318 ms 31572 KB Output is correct
97 Correct 370 ms 27976 KB Output is correct
98 Correct 290 ms 19488 KB Output is correct
99 Correct 424 ms 38216 KB Output is correct
100 Correct 403 ms 28936 KB Output is correct
101 Correct 288 ms 18740 KB Output is correct
102 Correct 354 ms 21832 KB Output is correct
103 Correct 299 ms 20620 KB Output is correct
104 Correct 296 ms 20224 KB Output is correct
105 Correct 218 ms 19016 KB Output is correct
106 Correct 350 ms 30024 KB Output is correct
107 Correct 320 ms 30368 KB Output is correct
108 Correct 341 ms 25444 KB Output is correct
109 Correct 133 ms 19768 KB Output is correct
110 Correct 347 ms 32452 KB Output is correct
111 Correct 345 ms 28916 KB Output is correct