Submission #489526

# Submission time Handle Problem Language Result Execution time Memory
489526 2021-11-23T05:02:33 Z kiomi Unique Cities (JOI19_ho_t5) C++17
100 / 100
458 ms 56432 KB
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("Ofast")
//#pragma GCC optimization ("unroll-loops")

//#pragma comment(linker,"/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define full(x,n) x,x+n+1
#define full(x) x.begin(),x.end()
#define finish return 0

#define putb push_back
#define f first
#define s second

//logx(a^n)=loga(a^n)/logx(a)
//logx(a*b)=logx(a)+logx(b)
//logx(y)=log(y)/log(x)
//logb(n)=loga(n)/loga(b)

#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define putf push_front
#define gainb pop_back

//(a+b)^n=sum of C(n,i)*a^i*b^(n-i) 0<=i<=n
//(a-b)^n=sum of C(n,i)*a^i*b^(n-i) for even i-for odd i

#define gainf pop_front
#define len(x) (int)x.size()

// 1/b%mod=b^(m-2)%mod
// (a>>x)&1==0
// a^b=(a+b)-2(a&b)

typedef double db;
typedef long long ll;

//sum of squares n*(n+1)*(2n+1)/6
//sum of cubes [n*(n+1)/2]^2
//sum of squares for odds n*(4*n*n-1)/3
//sum of cubes for odds n*n*(2*n*n-1)

const int ary=1e6+5;
const ll mod=1e9+7;
const ll inf=1e18;

using namespace std;
using namespace __gnu_pbds;
int n,m,c[ary],d[ary],mx1[ary],mx2[ary],ans[ary],res,cnt[ary];
vector<int> g[ary],q;
void dfs(int v,int pr=0){
	d[v]=d[pr]+1;
	mx1[v]=mx2[v]=v;
	for(int i=0;i<len(g[v]);i++){
		int to=g[v][i];
		if(to==pr){
			continue;
		}
		dfs(to,v);
		if(d[mx2[v]]<d[mx1[to]]){
			mx2[v]=mx1[to];
		}
		if(d[mx1[v]]<d[mx2[v]]){
			swap(mx1[v],mx2[v]);
		}
	}
}
void del(){
	res-=--cnt[c[q.back()]]==0;
	q.pop_back();
}
void add(int v){
	q.push_back(v);
	res+=cnt[c[v]]++==0;
}
void go(int v,int pr=0){
		for(int i=0;i<len(g[v]);i++){
			int to=g[v][i];
			if(to==pr||mx1[to]!=mx1[v]){
				continue;
			}
			while(len(q)&&d[mx2[v]]-d[v]>=d[v]-d[q.back()]){
				del();
			}
			add(v);
			go(to,v);
		}
		for(int i=0;i<len(g[v]);i++){
			int to=g[v][i];
			if(to==pr||mx1[to]==mx1[v]){
				continue;
			}
			while(len(q)&&d[mx1[v]]-d[v]>=d[v]-d[q.back()]){
				del();
			}
			add(v);
			go(to,v);
		}
	while(len(q)&&d[mx1[v]]-d[v]>=d[v]-d[q.back()]){
		del();
	}
	ans[v]=max(ans[v],res);
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}
	dfs(1);
	int A=mx1[1];
	d[A]=0;
	dfs(A);
	go(A);
	while(len(q)){
		del();
	}
	int B=mx1[A];
	d[B]=0;
	dfs(B);
	go(B);
	for(int i=1;i<=n;i++){
		cout<<ans[i]<<"\n";
	}
}

Compilation message

joi2019_ho_t5.cpp:15: warning: "full" redefined
   15 | #define full(x) x.begin(),x.end()
      | 
joi2019_ho_t5.cpp:14: note: this is the location of the previous definition
   14 | #define full(x,n) x,x+n+1
      |
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23760 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 14 ms 23888 KB Output is correct
4 Correct 14 ms 23920 KB Output is correct
5 Correct 14 ms 23948 KB Output is correct
6 Correct 15 ms 24040 KB Output is correct
7 Correct 14 ms 24016 KB Output is correct
8 Correct 14 ms 23888 KB Output is correct
9 Correct 18 ms 23940 KB Output is correct
10 Correct 14 ms 23888 KB Output is correct
11 Correct 16 ms 23988 KB Output is correct
12 Correct 14 ms 23988 KB Output is correct
13 Correct 14 ms 24028 KB Output is correct
14 Correct 13 ms 23952 KB Output is correct
15 Correct 13 ms 23968 KB Output is correct
16 Correct 14 ms 23888 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 14 ms 24016 KB Output is correct
19 Correct 13 ms 23868 KB Output is correct
20 Correct 15 ms 24164 KB Output is correct
21 Correct 15 ms 23944 KB Output is correct
22 Correct 16 ms 23904 KB Output is correct
23 Correct 14 ms 23884 KB Output is correct
24 Correct 15 ms 23944 KB Output is correct
25 Correct 15 ms 23980 KB Output is correct
26 Correct 14 ms 23916 KB Output is correct
27 Correct 14 ms 24088 KB Output is correct
28 Correct 14 ms 24120 KB Output is correct
29 Correct 16 ms 24016 KB Output is correct
30 Correct 14 ms 23948 KB Output is correct
31 Correct 14 ms 24080 KB Output is correct
32 Correct 17 ms 23980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 31632 KB Output is correct
2 Correct 217 ms 40976 KB Output is correct
3 Correct 43 ms 26824 KB Output is correct
4 Correct 318 ms 37400 KB Output is correct
5 Correct 416 ms 53880 KB Output is correct
6 Correct 437 ms 45464 KB Output is correct
7 Correct 324 ms 37496 KB Output is correct
8 Correct 380 ms 38964 KB Output is correct
9 Correct 300 ms 38516 KB Output is correct
10 Correct 318 ms 38392 KB Output is correct
11 Correct 211 ms 38152 KB Output is correct
12 Correct 377 ms 47488 KB Output is correct
13 Correct 393 ms 45384 KB Output is correct
14 Correct 351 ms 44948 KB Output is correct
15 Correct 146 ms 37932 KB Output is correct
16 Correct 356 ms 48712 KB Output is correct
17 Correct 397 ms 45768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 260 ms 35772 KB Output is correct
2 Correct 395 ms 54880 KB Output is correct
3 Correct 35 ms 27420 KB Output is correct
4 Correct 349 ms 39140 KB Output is correct
5 Correct 434 ms 56432 KB Output is correct
6 Correct 416 ms 47512 KB Output is correct
7 Correct 336 ms 39128 KB Output is correct
8 Correct 363 ms 42592 KB Output is correct
9 Correct 352 ms 41404 KB Output is correct
10 Correct 335 ms 40376 KB Output is correct
11 Correct 272 ms 39380 KB Output is correct
12 Correct 442 ms 52840 KB Output is correct
13 Correct 392 ms 46492 KB Output is correct
14 Correct 373 ms 46616 KB Output is correct
15 Correct 144 ms 39088 KB Output is correct
16 Correct 400 ms 50872 KB Output is correct
17 Correct 458 ms 47676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23760 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 14 ms 23888 KB Output is correct
4 Correct 14 ms 23920 KB Output is correct
5 Correct 14 ms 23948 KB Output is correct
6 Correct 15 ms 24040 KB Output is correct
7 Correct 14 ms 24016 KB Output is correct
8 Correct 14 ms 23888 KB Output is correct
9 Correct 18 ms 23940 KB Output is correct
10 Correct 14 ms 23888 KB Output is correct
11 Correct 16 ms 23988 KB Output is correct
12 Correct 14 ms 23988 KB Output is correct
13 Correct 14 ms 24028 KB Output is correct
14 Correct 13 ms 23952 KB Output is correct
15 Correct 13 ms 23968 KB Output is correct
16 Correct 14 ms 23888 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 14 ms 24016 KB Output is correct
19 Correct 13 ms 23868 KB Output is correct
20 Correct 15 ms 24164 KB Output is correct
21 Correct 15 ms 23944 KB Output is correct
22 Correct 16 ms 23904 KB Output is correct
23 Correct 14 ms 23884 KB Output is correct
24 Correct 15 ms 23944 KB Output is correct
25 Correct 15 ms 23980 KB Output is correct
26 Correct 14 ms 23916 KB Output is correct
27 Correct 14 ms 24088 KB Output is correct
28 Correct 14 ms 24120 KB Output is correct
29 Correct 16 ms 24016 KB Output is correct
30 Correct 14 ms 23948 KB Output is correct
31 Correct 14 ms 24080 KB Output is correct
32 Correct 17 ms 23980 KB Output is correct
33 Correct 137 ms 31632 KB Output is correct
34 Correct 217 ms 40976 KB Output is correct
35 Correct 43 ms 26824 KB Output is correct
36 Correct 318 ms 37400 KB Output is correct
37 Correct 416 ms 53880 KB Output is correct
38 Correct 437 ms 45464 KB Output is correct
39 Correct 324 ms 37496 KB Output is correct
40 Correct 380 ms 38964 KB Output is correct
41 Correct 300 ms 38516 KB Output is correct
42 Correct 318 ms 38392 KB Output is correct
43 Correct 211 ms 38152 KB Output is correct
44 Correct 377 ms 47488 KB Output is correct
45 Correct 393 ms 45384 KB Output is correct
46 Correct 351 ms 44948 KB Output is correct
47 Correct 146 ms 37932 KB Output is correct
48 Correct 356 ms 48712 KB Output is correct
49 Correct 397 ms 45768 KB Output is correct
50 Correct 260 ms 35772 KB Output is correct
51 Correct 395 ms 54880 KB Output is correct
52 Correct 35 ms 27420 KB Output is correct
53 Correct 349 ms 39140 KB Output is correct
54 Correct 434 ms 56432 KB Output is correct
55 Correct 416 ms 47512 KB Output is correct
56 Correct 336 ms 39128 KB Output is correct
57 Correct 363 ms 42592 KB Output is correct
58 Correct 352 ms 41404 KB Output is correct
59 Correct 335 ms 40376 KB Output is correct
60 Correct 272 ms 39380 KB Output is correct
61 Correct 442 ms 52840 KB Output is correct
62 Correct 392 ms 46492 KB Output is correct
63 Correct 373 ms 46616 KB Output is correct
64 Correct 144 ms 39088 KB Output is correct
65 Correct 400 ms 50872 KB Output is correct
66 Correct 458 ms 47676 KB Output is correct
67 Correct 30 ms 25764 KB Output is correct
68 Correct 152 ms 37476 KB Output is correct
69 Correct 250 ms 37788 KB Output is correct
70 Correct 302 ms 37444 KB Output is correct
71 Correct 431 ms 53960 KB Output is correct
72 Correct 395 ms 45548 KB Output is correct
73 Correct 290 ms 37476 KB Output is correct
74 Correct 344 ms 40464 KB Output is correct
75 Correct 374 ms 38760 KB Output is correct
76 Correct 348 ms 38712 KB Output is correct
77 Correct 235 ms 37836 KB Output is correct
78 Correct 431 ms 49364 KB Output is correct
79 Correct 321 ms 47556 KB Output is correct
80 Correct 340 ms 44000 KB Output is correct
81 Correct 152 ms 37800 KB Output is correct
82 Correct 375 ms 48620 KB Output is correct
83 Correct 342 ms 45736 KB Output is correct
84 Correct 380 ms 37904 KB Output is correct
85 Correct 431 ms 54532 KB Output is correct
86 Correct 402 ms 45896 KB Output is correct
87 Correct 317 ms 37864 KB Output is correct
88 Correct 373 ms 41176 KB Output is correct
89 Correct 335 ms 39568 KB Output is correct
90 Correct 320 ms 39068 KB Output is correct
91 Correct 224 ms 38152 KB Output is correct
92 Correct 407 ms 53992 KB Output is correct
93 Correct 323 ms 43564 KB Output is correct
94 Correct 323 ms 42008 KB Output is correct
95 Correct 175 ms 38308 KB Output is correct
96 Correct 335 ms 49216 KB Output is correct
97 Correct 362 ms 46292 KB Output is correct
98 Correct 299 ms 39104 KB Output is correct
99 Correct 437 ms 54960 KB Output is correct
100 Correct 379 ms 47200 KB Output is correct
101 Correct 293 ms 38352 KB Output is correct
102 Correct 316 ms 40960 KB Output is correct
103 Correct 326 ms 39900 KB Output is correct
104 Correct 322 ms 39496 KB Output is correct
105 Correct 221 ms 38464 KB Output is correct
106 Correct 370 ms 48048 KB Output is correct
107 Correct 335 ms 48324 KB Output is correct
108 Correct 352 ms 44116 KB Output is correct
109 Correct 157 ms 38980 KB Output is correct
110 Correct 340 ms 50080 KB Output is correct
111 Correct 378 ms 47044 KB Output is correct