답안 #519083

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519083 2022-01-25T15:43:18 Z Koosha_mv Unique Cities (JOI19_ho_t5) C++14
100 / 100
1077 ms 59544 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;

int n,m,d,res,cent,s1,s2,a[N],ans[N],dp[N],pd[N],mark[N],fenwik[N];
vector<int> v,g[N];

void add(int x, int val){ for(x++;x<n;x+=x&-x) fenwik[x]+=val; }
int get(int x) { int res=0; for (x++;x>0;x-=x&-x) res+=fenwik[x]; return res; }

void Add(int x){
	if(++mark[x]==1) res++;
}
void del(int x){
	if(--mark[x]==0) res--;
}
void dfs1(int u,int p,int dist){
	if(dist>=d){
		d=dist;
		cent=u;
	}
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs1(v,u,dist+1);
	}
}
void find_cent(){
	cent=d=0;
	dfs1(1,0,0);
	s1=cent;
	cent=d=0;
	dfs1(s1,0,0);
	s2=cent;
}
void dfs2(int u,int p){
	dp[u]=0;
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs2(v,u);
		maxm(dp[u],dp[v]+1);
	}
}
void dfs3(int u,int p){
	set<pair<int,int> > s;
	for(auto v : g[u]){
		if(v==p) continue ;
		s.insert(mp(dp[v],v));
	}
	for(auto v : g[u]){
		if(v==p) continue ;
		s.erase(mp(dp[v],v));
		pd[v]=0;
		if(s.size()){
			maxm(pd[v],(*s.rbegin()).F+1);
		}
		dfs3(v,u);
		s.insert(mp(dp[v],v));
	}
}
void dfs4(int u,int p,int d){
	int x=-1,y=-1;
	v.pb(u);
	if(pd[u]>0){
		add(max(0,d-pd[u]-1),1);
		add(d-1,-1);
	}
	if(d-dp[u]-2>=0 && get(d-dp[u]-2)==0){
		x=a[v[d-dp[u]-2]];
		Add(x);
	}
	if(d-dp[u]-1>=0 && get(d-dp[u]-1)==0){
		y=a[v[d-dp[u]-1]];
		Add(y);
	}
	maxm(ans[u],res);
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs4(v,u,d+1);
	}
	if(x!=-1){
		del(x);
	}
	if(y!=-1){
		del(y);
	}
	if(pd[u]>0){
		add(max(0,d-pd[u]-1),-1);
		add(d-1,1);
	}
	v.pop_back();
}
void solve(int u){
	fill(mark,mark+N,0);
	fill(fenwik,fenwik+N,0);
	v.clear();
	pd[u]=res=0;
	
	dfs2(u,0);
	dfs3(u,0);
	/*f(i,1,n+1){
		cout<<i<<" : "<<dp[i]<<" "<<pd[i]<<endl;
	}*/
	dfs4(u,0,0);
	
}
	
int main(){
	cin>>n>>m;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	f(i,1,n+1) cin>>a[i];
	find_cent();
	//cout<<s1<<" "<<s2<<endl;
	solve(s1);
	solve(s2);
	f(i,1,n+1) cout<<ans[i]<<" ";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6540 KB Output is correct
2 Correct 6 ms 6604 KB Output is correct
3 Correct 5 ms 6800 KB Output is correct
4 Correct 6 ms 6784 KB Output is correct
5 Correct 6 ms 6700 KB Output is correct
6 Correct 6 ms 6988 KB Output is correct
7 Correct 5 ms 6840 KB Output is correct
8 Correct 6 ms 6612 KB Output is correct
9 Correct 6 ms 6732 KB Output is correct
10 Correct 6 ms 6732 KB Output is correct
11 Correct 6 ms 6672 KB Output is correct
12 Correct 7 ms 6676 KB Output is correct
13 Correct 7 ms 7060 KB Output is correct
14 Correct 6 ms 6860 KB Output is correct
15 Correct 7 ms 6844 KB Output is correct
16 Correct 6 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 6 ms 6804 KB Output is correct
19 Correct 6 ms 6604 KB Output is correct
20 Correct 6 ms 6988 KB Output is correct
21 Correct 6 ms 6860 KB Output is correct
22 Correct 6 ms 6676 KB Output is correct
23 Correct 7 ms 6732 KB Output is correct
24 Correct 7 ms 6676 KB Output is correct
25 Correct 7 ms 6680 KB Output is correct
26 Correct 7 ms 6732 KB Output is correct
27 Correct 7 ms 6988 KB Output is correct
28 Correct 7 ms 6932 KB Output is correct
29 Correct 7 ms 6796 KB Output is correct
30 Correct 8 ms 6800 KB Output is correct
31 Correct 7 ms 6988 KB Output is correct
32 Correct 7 ms 6800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 13832 KB Output is correct
2 Correct 282 ms 35884 KB Output is correct
3 Correct 43 ms 11076 KB Output is correct
4 Correct 506 ms 19564 KB Output is correct
5 Correct 573 ms 57784 KB Output is correct
6 Correct 561 ms 38580 KB Output is correct
7 Correct 552 ms 21176 KB Output is correct
8 Correct 536 ms 24488 KB Output is correct
9 Correct 548 ms 23208 KB Output is correct
10 Correct 513 ms 22956 KB Output is correct
11 Correct 794 ms 28944 KB Output is correct
12 Correct 600 ms 45688 KB Output is correct
13 Correct 623 ms 41680 KB Output is correct
14 Correct 591 ms 39616 KB Output is correct
15 Correct 839 ms 29324 KB Output is correct
16 Correct 595 ms 48704 KB Output is correct
17 Correct 614 ms 41064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 17292 KB Output is correct
2 Correct 602 ms 57136 KB Output is correct
3 Correct 55 ms 11708 KB Output is correct
4 Correct 531 ms 20344 KB Output is correct
5 Correct 608 ms 59544 KB Output is correct
6 Correct 571 ms 39760 KB Output is correct
7 Correct 619 ms 21700 KB Output is correct
8 Correct 583 ms 30260 KB Output is correct
9 Correct 563 ms 27288 KB Output is correct
10 Correct 606 ms 24344 KB Output is correct
11 Correct 654 ms 25276 KB Output is correct
12 Correct 643 ms 53172 KB Output is correct
13 Correct 659 ms 40480 KB Output is correct
14 Correct 663 ms 39996 KB Output is correct
15 Correct 877 ms 30516 KB Output is correct
16 Correct 625 ms 49624 KB Output is correct
17 Correct 647 ms 42248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6540 KB Output is correct
2 Correct 6 ms 6604 KB Output is correct
3 Correct 5 ms 6800 KB Output is correct
4 Correct 6 ms 6784 KB Output is correct
5 Correct 6 ms 6700 KB Output is correct
6 Correct 6 ms 6988 KB Output is correct
7 Correct 5 ms 6840 KB Output is correct
8 Correct 6 ms 6612 KB Output is correct
9 Correct 6 ms 6732 KB Output is correct
10 Correct 6 ms 6732 KB Output is correct
11 Correct 6 ms 6672 KB Output is correct
12 Correct 7 ms 6676 KB Output is correct
13 Correct 7 ms 7060 KB Output is correct
14 Correct 6 ms 6860 KB Output is correct
15 Correct 7 ms 6844 KB Output is correct
16 Correct 6 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 6 ms 6804 KB Output is correct
19 Correct 6 ms 6604 KB Output is correct
20 Correct 6 ms 6988 KB Output is correct
21 Correct 6 ms 6860 KB Output is correct
22 Correct 6 ms 6676 KB Output is correct
23 Correct 7 ms 6732 KB Output is correct
24 Correct 7 ms 6676 KB Output is correct
25 Correct 7 ms 6680 KB Output is correct
26 Correct 7 ms 6732 KB Output is correct
27 Correct 7 ms 6988 KB Output is correct
28 Correct 7 ms 6932 KB Output is correct
29 Correct 7 ms 6796 KB Output is correct
30 Correct 8 ms 6800 KB Output is correct
31 Correct 7 ms 6988 KB Output is correct
32 Correct 7 ms 6800 KB Output is correct
33 Correct 230 ms 13832 KB Output is correct
34 Correct 282 ms 35884 KB Output is correct
35 Correct 43 ms 11076 KB Output is correct
36 Correct 506 ms 19564 KB Output is correct
37 Correct 573 ms 57784 KB Output is correct
38 Correct 561 ms 38580 KB Output is correct
39 Correct 552 ms 21176 KB Output is correct
40 Correct 536 ms 24488 KB Output is correct
41 Correct 548 ms 23208 KB Output is correct
42 Correct 513 ms 22956 KB Output is correct
43 Correct 794 ms 28944 KB Output is correct
44 Correct 600 ms 45688 KB Output is correct
45 Correct 623 ms 41680 KB Output is correct
46 Correct 591 ms 39616 KB Output is correct
47 Correct 839 ms 29324 KB Output is correct
48 Correct 595 ms 48704 KB Output is correct
49 Correct 614 ms 41064 KB Output is correct
50 Correct 384 ms 17292 KB Output is correct
51 Correct 602 ms 57136 KB Output is correct
52 Correct 55 ms 11708 KB Output is correct
53 Correct 531 ms 20344 KB Output is correct
54 Correct 608 ms 59544 KB Output is correct
55 Correct 571 ms 39760 KB Output is correct
56 Correct 619 ms 21700 KB Output is correct
57 Correct 583 ms 30260 KB Output is correct
58 Correct 563 ms 27288 KB Output is correct
59 Correct 606 ms 24344 KB Output is correct
60 Correct 654 ms 25276 KB Output is correct
61 Correct 643 ms 53172 KB Output is correct
62 Correct 659 ms 40480 KB Output is correct
63 Correct 663 ms 39996 KB Output is correct
64 Correct 877 ms 30516 KB Output is correct
65 Correct 625 ms 49624 KB Output is correct
66 Correct 647 ms 42248 KB Output is correct
67 Correct 46 ms 8324 KB Output is correct
68 Correct 252 ms 29224 KB Output is correct
69 Correct 312 ms 26892 KB Output is correct
70 Correct 509 ms 19448 KB Output is correct
71 Correct 574 ms 57836 KB Output is correct
72 Correct 529 ms 38472 KB Output is correct
73 Correct 555 ms 20696 KB Output is correct
74 Correct 546 ms 28800 KB Output is correct
75 Correct 606 ms 24396 KB Output is correct
76 Correct 548 ms 23444 KB Output is correct
77 Correct 652 ms 25120 KB Output is correct
78 Correct 603 ms 49332 KB Output is correct
79 Correct 604 ms 45964 KB Output is correct
80 Correct 590 ms 37568 KB Output is correct
81 Correct 765 ms 29108 KB Output is correct
82 Correct 585 ms 48252 KB Output is correct
83 Correct 633 ms 41028 KB Output is correct
84 Correct 506 ms 19728 KB Output is correct
85 Correct 595 ms 58600 KB Output is correct
86 Correct 551 ms 39056 KB Output is correct
87 Correct 530 ms 20900 KB Output is correct
88 Correct 533 ms 29600 KB Output is correct
89 Correct 549 ms 25292 KB Output is correct
90 Correct 580 ms 24036 KB Output is correct
91 Correct 656 ms 25424 KB Output is correct
92 Correct 573 ms 57332 KB Output is correct
93 Correct 621 ms 36432 KB Output is correct
94 Correct 579 ms 32092 KB Output is correct
95 Correct 818 ms 29664 KB Output is correct
96 Correct 624 ms 49468 KB Output is correct
97 Correct 641 ms 41552 KB Output is correct
98 Correct 531 ms 20392 KB Output is correct
99 Correct 595 ms 58772 KB Output is correct
100 Correct 586 ms 39700 KB Output is correct
101 Correct 574 ms 22028 KB Output is correct
102 Correct 568 ms 28048 KB Output is correct
103 Correct 713 ms 25376 KB Output is correct
104 Correct 613 ms 24288 KB Output is correct
105 Correct 794 ms 26328 KB Output is correct
106 Correct 681 ms 44552 KB Output is correct
107 Correct 688 ms 46292 KB Output is correct
108 Correct 851 ms 34580 KB Output is correct
109 Correct 1077 ms 30288 KB Output is correct
110 Correct 739 ms 49876 KB Output is correct
111 Correct 713 ms 42112 KB Output is correct