답안 #518933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
518933 2022-01-25T08:31:25 Z hivakarami Unique Cities (JOI19_ho_t5) C++14
100 / 100
460 ms 49044 KB
#include<bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef long double ld;
 
#define f first
#define s second
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define SZ(v) int(v.size())
#define all(v) (v).begin(),(v).end()
 
const int N = 2e5 + 100;
const ll inf = 1e16;
const ll base = 313;
const ll mod = 1009;
 
int h[N], mx[N], mx2[N];
vector<int> adj[N];
set<pii> st[N];
int ans[N], c[N], n, cnt = 0, Cnt[N];
deque<int> q;  
 
inline void add(int x)
{
	//cout << "add " << x << endl;
	cnt += (Cnt[c[x]] == 0); 
	Cnt[c[x]]++;
	q.push_back(x);
} 
 
 
inline void del()
{
	int u = q.back();
	//cout << "del " << u << endl;
	Cnt[c[u]]--;
	cnt -= (Cnt[c[u]] == 0);
	q.pop_back();
} 
 
void dfs(int u, int p)
{
	mx[u] = 0; mx2[u] = -1;
	for(auto x : adj[u])
	{
		if(x != p)
		{
			h[x] = h[u] + 1;
			dfs(x, u);
			if(mx[x] + 1 >= mx[u])
			{
				mx2[u] = mx[u];
				mx[u] = mx[x]+1;
			}
			else if(mx[x] + 1 >= mx2[u])
				mx2[u] = mx[x]+1;
		}
	}
}
 
inline pii find_root()
{
	dfs(0, -1);
	int r1 = 0, r2 = 0;
	for(int i = 0; i < n; i++)
	{
		if(h[i] > h[r1])
			r1 = i;
	}
	h[r1] = 0;
	dfs(r1, -1);
	for(int i = 0; i < n; i++)
	{
		if(h[i] > h[r2])
			r2 = i;
	//	cout << h[i] << ' ' << r2 << '\n';
	}
	
	return {r1, r2};
	
	
} 


void solve(int u, int p = -1)
{
		
	for(auto x : adj[u])
	{
		if(x != p && mx[x] + 1 == mx[u])
		{
			while(q.size() && h[u] - h[q.back()] <= mx2[u])
				del();
			
			add(u);
			solve(x, u);
		}
	}

	for(auto x : adj[u])
	{
		if(x != p && mx[x] + 1 != mx[u])
		{
			while(q.size() && h[u] - h[q.back()] <= mx[u])
				del();
			
			add(u);
			solve(x, u);
		}
	}
	
	while(q.size() && h[u] - h[q.back()] <= mx[u])
		del();
	
	/*		
	cout << "u " << u<< endl;
	for(int i = 0; i < 5; i++)
		cout << Cnt[i] << ' ';
	cout << endl;	
	cout << "/////////////////////////" << endl;		
	*/		
	ans[u] = max(ans[u], cnt);
		
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);	


	int m;
	cin >> n >> m;
	for(int i = 0; i < n-1; i++)
	{
		int x, y;
		cin >> x >> y;
		x--; y--;
		adj[x].pb(y);
		adj[y].pb(x);
	}

	for(int i = 0; i < n; i++)
		cin >> c[i];

 	pii r = find_root();	
	
	
	//cout << r.f << ' ' << r.s << endl;
	
	h[r.f] = 0;
	dfs(r.f, -1);
	solve(r.f);
	
	h[r.s] = 0;
	dfs(r.s, -1);
	solve(r.s);
	
	
	for(int i = 0; i < n; i++)
		cout << ans[i] << '\n';
	
	
	
	
	return 0;
}
 
 
 
 
/*
5 4
1 2
2 3
3 4
3 5
1 2 1 2 4


*/
 
 

 
 
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14412 KB Output is correct
2 Correct 9 ms 14476 KB Output is correct
3 Correct 8 ms 14548 KB Output is correct
4 Correct 9 ms 14608 KB Output is correct
5 Correct 9 ms 14540 KB Output is correct
6 Correct 9 ms 14684 KB Output is correct
7 Correct 9 ms 14604 KB Output is correct
8 Correct 9 ms 14572 KB Output is correct
9 Correct 9 ms 14564 KB Output is correct
10 Correct 9 ms 14560 KB Output is correct
11 Correct 9 ms 14564 KB Output is correct
12 Correct 9 ms 14540 KB Output is correct
13 Correct 9 ms 14696 KB Output is correct
14 Correct 9 ms 14568 KB Output is correct
15 Correct 11 ms 14540 KB Output is correct
16 Correct 9 ms 14540 KB Output is correct
17 Correct 10 ms 14600 KB Output is correct
18 Correct 9 ms 14592 KB Output is correct
19 Correct 10 ms 14660 KB Output is correct
20 Correct 9 ms 14668 KB Output is correct
21 Correct 9 ms 14560 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 12 ms 14568 KB Output is correct
24 Correct 9 ms 14544 KB Output is correct
25 Correct 9 ms 14540 KB Output is correct
26 Correct 8 ms 14472 KB Output is correct
27 Correct 9 ms 14644 KB Output is correct
28 Correct 10 ms 14796 KB Output is correct
29 Correct 9 ms 14532 KB Output is correct
30 Correct 10 ms 14472 KB Output is correct
31 Correct 9 ms 14692 KB Output is correct
32 Correct 9 ms 14580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 21316 KB Output is correct
2 Correct 189 ms 33360 KB Output is correct
3 Correct 29 ms 17636 KB Output is correct
4 Correct 310 ms 27984 KB Output is correct
5 Correct 429 ms 47260 KB Output is correct
6 Correct 406 ms 37496 KB Output is correct
7 Correct 310 ms 28064 KB Output is correct
8 Correct 337 ms 29888 KB Output is correct
9 Correct 320 ms 29272 KB Output is correct
10 Correct 351 ms 29124 KB Output is correct
11 Correct 164 ms 28720 KB Output is correct
12 Correct 415 ms 39820 KB Output is correct
13 Correct 374 ms 37496 KB Output is correct
14 Correct 347 ms 36728 KB Output is correct
15 Correct 179 ms 28512 KB Output is correct
16 Correct 351 ms 41176 KB Output is correct
17 Correct 374 ms 37664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 24876 KB Output is correct
2 Correct 407 ms 47512 KB Output is correct
3 Correct 32 ms 18252 KB Output is correct
4 Correct 328 ms 28788 KB Output is correct
5 Correct 442 ms 49044 KB Output is correct
6 Correct 419 ms 38632 KB Output is correct
7 Correct 349 ms 28876 KB Output is correct
8 Correct 338 ms 32912 KB Output is correct
9 Correct 357 ms 31564 KB Output is correct
10 Correct 384 ms 30456 KB Output is correct
11 Correct 242 ms 29084 KB Output is correct
12 Correct 427 ms 44992 KB Output is correct
13 Correct 351 ms 37424 KB Output is correct
14 Correct 354 ms 37416 KB Output is correct
15 Correct 140 ms 28596 KB Output is correct
16 Correct 363 ms 42620 KB Output is correct
17 Correct 380 ms 38664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14412 KB Output is correct
2 Correct 9 ms 14476 KB Output is correct
3 Correct 8 ms 14548 KB Output is correct
4 Correct 9 ms 14608 KB Output is correct
5 Correct 9 ms 14540 KB Output is correct
6 Correct 9 ms 14684 KB Output is correct
7 Correct 9 ms 14604 KB Output is correct
8 Correct 9 ms 14572 KB Output is correct
9 Correct 9 ms 14564 KB Output is correct
10 Correct 9 ms 14560 KB Output is correct
11 Correct 9 ms 14564 KB Output is correct
12 Correct 9 ms 14540 KB Output is correct
13 Correct 9 ms 14696 KB Output is correct
14 Correct 9 ms 14568 KB Output is correct
15 Correct 11 ms 14540 KB Output is correct
16 Correct 9 ms 14540 KB Output is correct
17 Correct 10 ms 14600 KB Output is correct
18 Correct 9 ms 14592 KB Output is correct
19 Correct 10 ms 14660 KB Output is correct
20 Correct 9 ms 14668 KB Output is correct
21 Correct 9 ms 14560 KB Output is correct
22 Correct 9 ms 14540 KB Output is correct
23 Correct 12 ms 14568 KB Output is correct
24 Correct 9 ms 14544 KB Output is correct
25 Correct 9 ms 14540 KB Output is correct
26 Correct 8 ms 14472 KB Output is correct
27 Correct 9 ms 14644 KB Output is correct
28 Correct 10 ms 14796 KB Output is correct
29 Correct 9 ms 14532 KB Output is correct
30 Correct 10 ms 14472 KB Output is correct
31 Correct 9 ms 14692 KB Output is correct
32 Correct 9 ms 14580 KB Output is correct
33 Correct 183 ms 21316 KB Output is correct
34 Correct 189 ms 33360 KB Output is correct
35 Correct 29 ms 17636 KB Output is correct
36 Correct 310 ms 27984 KB Output is correct
37 Correct 429 ms 47260 KB Output is correct
38 Correct 406 ms 37496 KB Output is correct
39 Correct 310 ms 28064 KB Output is correct
40 Correct 337 ms 29888 KB Output is correct
41 Correct 320 ms 29272 KB Output is correct
42 Correct 351 ms 29124 KB Output is correct
43 Correct 164 ms 28720 KB Output is correct
44 Correct 415 ms 39820 KB Output is correct
45 Correct 374 ms 37496 KB Output is correct
46 Correct 347 ms 36728 KB Output is correct
47 Correct 179 ms 28512 KB Output is correct
48 Correct 351 ms 41176 KB Output is correct
49 Correct 374 ms 37664 KB Output is correct
50 Correct 291 ms 24876 KB Output is correct
51 Correct 407 ms 47512 KB Output is correct
52 Correct 32 ms 18252 KB Output is correct
53 Correct 328 ms 28788 KB Output is correct
54 Correct 442 ms 49044 KB Output is correct
55 Correct 419 ms 38632 KB Output is correct
56 Correct 349 ms 28876 KB Output is correct
57 Correct 338 ms 32912 KB Output is correct
58 Correct 357 ms 31564 KB Output is correct
59 Correct 384 ms 30456 KB Output is correct
60 Correct 242 ms 29084 KB Output is correct
61 Correct 427 ms 44992 KB Output is correct
62 Correct 351 ms 37424 KB Output is correct
63 Correct 354 ms 37416 KB Output is correct
64 Correct 140 ms 28596 KB Output is correct
65 Correct 363 ms 42620 KB Output is correct
66 Correct 380 ms 38664 KB Output is correct
67 Correct 26 ms 16324 KB Output is correct
68 Correct 137 ms 29352 KB Output is correct
69 Correct 193 ms 29288 KB Output is correct
70 Correct 320 ms 28000 KB Output is correct
71 Correct 442 ms 47172 KB Output is correct
72 Correct 375 ms 37396 KB Output is correct
73 Correct 376 ms 27972 KB Output is correct
74 Correct 387 ms 31620 KB Output is correct
75 Correct 350 ms 29656 KB Output is correct
76 Correct 351 ms 29380 KB Output is correct
77 Correct 242 ms 28220 KB Output is correct
78 Correct 386 ms 42004 KB Output is correct
79 Correct 356 ms 39692 KB Output is correct
80 Correct 336 ms 35524 KB Output is correct
81 Correct 142 ms 28348 KB Output is correct
82 Correct 345 ms 41148 KB Output is correct
83 Correct 365 ms 37536 KB Output is correct
84 Correct 318 ms 27844 KB Output is correct
85 Correct 431 ms 47540 KB Output is correct
86 Correct 399 ms 37460 KB Output is correct
87 Correct 305 ms 27936 KB Output is correct
88 Correct 370 ms 32072 KB Output is correct
89 Correct 323 ms 30128 KB Output is correct
90 Correct 326 ms 29388 KB Output is correct
91 Correct 235 ms 28236 KB Output is correct
92 Correct 460 ms 46764 KB Output is correct
93 Correct 372 ms 34588 KB Output is correct
94 Correct 368 ms 32836 KB Output is correct
95 Correct 159 ms 28452 KB Output is correct
96 Correct 340 ms 41348 KB Output is correct
97 Correct 351 ms 37592 KB Output is correct
98 Correct 326 ms 28632 KB Output is correct
99 Correct 431 ms 47536 KB Output is correct
100 Correct 400 ms 37968 KB Output is correct
101 Correct 324 ms 27844 KB Output is correct
102 Correct 327 ms 30916 KB Output is correct
103 Correct 372 ms 29760 KB Output is correct
104 Correct 332 ms 29544 KB Output is correct
105 Correct 242 ms 28212 KB Output is correct
106 Correct 411 ms 39276 KB Output is correct
107 Correct 346 ms 39716 KB Output is correct
108 Correct 370 ms 34372 KB Output is correct
109 Correct 156 ms 28456 KB Output is correct
110 Correct 411 ms 41428 KB Output is correct
111 Correct 383 ms 37968 KB Output is correct