Submission #671183

# Submission time Handle Problem Language Result Execution time Memory
671183 2022-12-12T10:05:52 Z dozer Unique Cities (JOI19_ho_t5) C++14
100 / 100
408 ms 87808 KB
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w",  stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0) 
#define pb push_back 
#define st first 
#define nd second 
#define sp " " 
#define endl "\n" 
#define pii pair<int, int> 
#define N 2000005 
 
 
const int modulo = 1e9 + 7; 
int dist[N], ans[N], dep[N], arr[N], cnt[N], res; 
vector<int> adj[N]; 
vector<int> s;

void del(int x)
{
	while(!s.empty() && dist[s.back()] >= x)
	{
		int curr = s.back();
		cnt[arr[curr]]--;
		if (cnt[arr[curr]] == 0) res--;
		s.pop_back();
	}
	//for (auto i : s) cerr<<arr[i]<<sp;
	//cerr<<"->"<<res<<endl;
}


void add(int node)
{
	if (s.empty() || s.back() != node)
	{
		s.pb(node);
		cnt[arr[node]]++;
		if (cnt[arr[node]] == 1) res++;
	}
	//for (auto i : s) cerr<<arr[i]<<sp;
	//cerr<<"->"<<res<<endl;
}

void dfs(int node, int root, int d) 
{ 
	dist[node] = d; 
	dep[node] = d; 
	for (auto i : adj[node])  
	{ 
		if (i == root) continue;
		dfs(i, node, d + 1); 
		dep[node] = max(dep[node], dep[i]); 
	} 
} 
 
 
void dfs2(int node, int root) 
{ 
	pair<pii, pii> maks = {{0, 0}, {0, 0}};
	for (int i = 0; i < adj[node].size(); i++) 
	{ 
		int curr = adj[node][i];
		if (curr == root) continue;
		if (dep[curr] > maks.st.st)
		{
			maks.nd = maks.st;
			maks.st = {dep[curr], curr};
		}
		else maks.nd = max(maks.nd, {dep[curr], curr});
	} 
	
 	int diff = maks.nd.st - dist[node];
 	int to = max(0, dist[node] - diff);
 	del(to);
 	int top = maks.st.nd;
 	add(node);
 	vector<int> ord;
 	if (top != 0) ord.pb(top), dfs2(top, node);
 	diff = maks.st.st - dist[node];
 	to = max(0, dist[node] - diff);
 	del(to);
	for (int i = 0; i < adj[node].size(); i++) 
	{
		int curr = adj[node][i]; 
		if (curr != root && curr != top) 
		{
			ord.pb(curr);
			add(node);
			dfs2(curr, node);
		}
	} 
	del(dist[node]);
	ans[node] = max(ans[node], res); 
	//cout<<node<<" : ";
	////for (auto i : ord) cout<<i<<sp;
	////cout<<"||| ";
	//for (auto i : s) cout<<i<<sp;
	//cout<<endl;
} 
 


int32_t main() 
{ 
	fastio(); 
 
	int n, m; 
	cin>>n>>m; 
	for (int i = 1; i < n; i++) 
	{ 
		int u, v; 
		cin>>u>>v; 
		adj[u].pb(v); 
		adj[v].pb(u); 
	} 
 
	for (int i = 1; i <= n; i++) 
		cin>>arr[i]; 
 
	dfs(1, 0, 1); 
	 
	pii maks = {0, 0}; 
	for (int i = 1; i <= n; i++) maks = max(maks, {dist[i], i}); 
	int root = maks.nd; 

	dfs(root, 0, 1); 
	dfs2(root, 0); 
 	
	maks = {0, 0}; 
	for (int i = 1; i <= n; i++) maks = max(maks, {dist[i], i});
	for (int i = 1; i <= m; i++) cnt[i] = 0; 
	s.clear();
	res = 0;
	root = maks.nd; 
	dfs(root, 0, 1); 
	dfs2(root, 0); 
	for (int i = 1; i <= n; i++) 
		cout<<ans[i]<<endl; 

	cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n"; 
}

Compilation message

joi2019_ho_t5.cpp: In function 'void dfs2(int, int)':
joi2019_ho_t5.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |  for (int i = 0; i < adj[node].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |  for (int i = 0; i < adj[node].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47308 KB Output is correct
2 Correct 27 ms 47424 KB Output is correct
3 Correct 23 ms 47436 KB Output is correct
4 Correct 26 ms 47572 KB Output is correct
5 Correct 26 ms 47420 KB Output is correct
6 Correct 25 ms 47692 KB Output is correct
7 Correct 25 ms 47564 KB Output is correct
8 Correct 26 ms 47348 KB Output is correct
9 Correct 25 ms 47388 KB Output is correct
10 Correct 24 ms 47444 KB Output is correct
11 Correct 25 ms 47444 KB Output is correct
12 Correct 25 ms 47376 KB Output is correct
13 Correct 25 ms 47692 KB Output is correct
14 Correct 27 ms 47416 KB Output is correct
15 Correct 24 ms 47432 KB Output is correct
16 Correct 26 ms 47424 KB Output is correct
17 Correct 25 ms 47572 KB Output is correct
18 Correct 25 ms 47572 KB Output is correct
19 Correct 27 ms 47444 KB Output is correct
20 Correct 26 ms 47696 KB Output is correct
21 Correct 26 ms 47528 KB Output is correct
22 Correct 25 ms 47432 KB Output is correct
23 Correct 25 ms 47488 KB Output is correct
24 Correct 25 ms 47428 KB Output is correct
25 Correct 32 ms 47492 KB Output is correct
26 Correct 25 ms 47488 KB Output is correct
27 Correct 24 ms 47672 KB Output is correct
28 Correct 24 ms 47564 KB Output is correct
29 Correct 24 ms 47572 KB Output is correct
30 Correct 24 ms 47444 KB Output is correct
31 Correct 25 ms 47560 KB Output is correct
32 Correct 24 ms 47516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 53452 KB Output is correct
2 Correct 193 ms 69620 KB Output is correct
3 Correct 48 ms 50888 KB Output is correct
4 Correct 285 ms 57368 KB Output is correct
5 Correct 386 ms 86204 KB Output is correct
6 Correct 370 ms 71532 KB Output is correct
7 Correct 290 ms 57544 KB Output is correct
8 Correct 291 ms 60200 KB Output is correct
9 Correct 305 ms 59212 KB Output is correct
10 Correct 290 ms 59164 KB Output is correct
11 Correct 170 ms 59328 KB Output is correct
12 Correct 375 ms 75076 KB Output is correct
13 Correct 328 ms 71352 KB Output is correct
14 Correct 321 ms 70532 KB Output is correct
15 Correct 154 ms 59416 KB Output is correct
16 Correct 342 ms 76836 KB Output is correct
17 Correct 332 ms 71772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 56232 KB Output is correct
2 Correct 407 ms 86072 KB Output is correct
3 Correct 48 ms 51404 KB Output is correct
4 Correct 341 ms 58072 KB Output is correct
5 Correct 396 ms 87808 KB Output is correct
6 Correct 375 ms 72648 KB Output is correct
7 Correct 305 ms 58208 KB Output is correct
8 Correct 328 ms 63948 KB Output is correct
9 Correct 323 ms 61928 KB Output is correct
10 Correct 319 ms 60444 KB Output is correct
11 Correct 251 ms 58808 KB Output is correct
12 Correct 400 ms 81940 KB Output is correct
13 Correct 323 ms 70624 KB Output is correct
14 Correct 352 ms 71036 KB Output is correct
15 Correct 159 ms 60208 KB Output is correct
16 Correct 326 ms 78112 KB Output is correct
17 Correct 349 ms 73036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47308 KB Output is correct
2 Correct 27 ms 47424 KB Output is correct
3 Correct 23 ms 47436 KB Output is correct
4 Correct 26 ms 47572 KB Output is correct
5 Correct 26 ms 47420 KB Output is correct
6 Correct 25 ms 47692 KB Output is correct
7 Correct 25 ms 47564 KB Output is correct
8 Correct 26 ms 47348 KB Output is correct
9 Correct 25 ms 47388 KB Output is correct
10 Correct 24 ms 47444 KB Output is correct
11 Correct 25 ms 47444 KB Output is correct
12 Correct 25 ms 47376 KB Output is correct
13 Correct 25 ms 47692 KB Output is correct
14 Correct 27 ms 47416 KB Output is correct
15 Correct 24 ms 47432 KB Output is correct
16 Correct 26 ms 47424 KB Output is correct
17 Correct 25 ms 47572 KB Output is correct
18 Correct 25 ms 47572 KB Output is correct
19 Correct 27 ms 47444 KB Output is correct
20 Correct 26 ms 47696 KB Output is correct
21 Correct 26 ms 47528 KB Output is correct
22 Correct 25 ms 47432 KB Output is correct
23 Correct 25 ms 47488 KB Output is correct
24 Correct 25 ms 47428 KB Output is correct
25 Correct 32 ms 47492 KB Output is correct
26 Correct 25 ms 47488 KB Output is correct
27 Correct 24 ms 47672 KB Output is correct
28 Correct 24 ms 47564 KB Output is correct
29 Correct 24 ms 47572 KB Output is correct
30 Correct 24 ms 47444 KB Output is correct
31 Correct 25 ms 47560 KB Output is correct
32 Correct 24 ms 47516 KB Output is correct
33 Correct 146 ms 53452 KB Output is correct
34 Correct 193 ms 69620 KB Output is correct
35 Correct 48 ms 50888 KB Output is correct
36 Correct 285 ms 57368 KB Output is correct
37 Correct 386 ms 86204 KB Output is correct
38 Correct 370 ms 71532 KB Output is correct
39 Correct 290 ms 57544 KB Output is correct
40 Correct 291 ms 60200 KB Output is correct
41 Correct 305 ms 59212 KB Output is correct
42 Correct 290 ms 59164 KB Output is correct
43 Correct 170 ms 59328 KB Output is correct
44 Correct 375 ms 75076 KB Output is correct
45 Correct 328 ms 71352 KB Output is correct
46 Correct 321 ms 70532 KB Output is correct
47 Correct 154 ms 59416 KB Output is correct
48 Correct 342 ms 76836 KB Output is correct
49 Correct 332 ms 71772 KB Output is correct
50 Correct 232 ms 56232 KB Output is correct
51 Correct 407 ms 86072 KB Output is correct
52 Correct 48 ms 51404 KB Output is correct
53 Correct 341 ms 58072 KB Output is correct
54 Correct 396 ms 87808 KB Output is correct
55 Correct 375 ms 72648 KB Output is correct
56 Correct 305 ms 58208 KB Output is correct
57 Correct 328 ms 63948 KB Output is correct
58 Correct 323 ms 61928 KB Output is correct
59 Correct 319 ms 60444 KB Output is correct
60 Correct 251 ms 58808 KB Output is correct
61 Correct 400 ms 81940 KB Output is correct
62 Correct 323 ms 70624 KB Output is correct
63 Correct 352 ms 71036 KB Output is correct
64 Correct 159 ms 60208 KB Output is correct
65 Correct 326 ms 78112 KB Output is correct
66 Correct 349 ms 73036 KB Output is correct
67 Correct 41 ms 48964 KB Output is correct
68 Correct 132 ms 64796 KB Output is correct
69 Correct 227 ms 62728 KB Output is correct
70 Correct 294 ms 57496 KB Output is correct
71 Correct 391 ms 86384 KB Output is correct
72 Correct 361 ms 71620 KB Output is correct
73 Correct 275 ms 57780 KB Output is correct
74 Correct 312 ms 62816 KB Output is correct
75 Correct 292 ms 60020 KB Output is correct
76 Correct 294 ms 59540 KB Output is correct
77 Correct 218 ms 58268 KB Output is correct
78 Correct 383 ms 78404 KB Output is correct
79 Correct 316 ms 74844 KB Output is correct
80 Correct 340 ms 69108 KB Output is correct
81 Correct 139 ms 59500 KB Output is correct
82 Correct 316 ms 76832 KB Output is correct
83 Correct 337 ms 72072 KB Output is correct
84 Correct 284 ms 57404 KB Output is correct
85 Correct 408 ms 86680 KB Output is correct
86 Correct 366 ms 71772 KB Output is correct
87 Correct 276 ms 57584 KB Output is correct
88 Correct 313 ms 63504 KB Output is correct
89 Correct 313 ms 60548 KB Output is correct
90 Correct 318 ms 59800 KB Output is correct
91 Correct 234 ms 58400 KB Output is correct
92 Correct 385 ms 85628 KB Output is correct
93 Correct 328 ms 66896 KB Output is correct
94 Correct 307 ms 64484 KB Output is correct
95 Correct 137 ms 59436 KB Output is correct
96 Correct 331 ms 77244 KB Output is correct
97 Correct 328 ms 72144 KB Output is correct
98 Correct 307 ms 58272 KB Output is correct
99 Correct 403 ms 86776 KB Output is correct
100 Correct 387 ms 72344 KB Output is correct
101 Correct 274 ms 57560 KB Output is correct
102 Correct 315 ms 62128 KB Output is correct
103 Correct 319 ms 60324 KB Output is correct
104 Correct 312 ms 59880 KB Output is correct
105 Correct 210 ms 58280 KB Output is correct
106 Correct 361 ms 73920 KB Output is correct
107 Correct 329 ms 74684 KB Output is correct
108 Correct 333 ms 66796 KB Output is correct
109 Correct 160 ms 59808 KB Output is correct
110 Correct 333 ms 77296 KB Output is correct
111 Correct 343 ms 72412 KB Output is correct