답안 #790865

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790865 2023-07-23T09:05:56 Z kshitij_sodani Unique Cities (JOI19_ho_t5) C++14
100 / 100
752 ms 60888 KB
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#define endl '\n'
 
int it[200001];
vector<int> adj[200001];
vector<int> adj2[200001];
int vis[200001];
int val[200001];
int ans[200001];
pair<int,int> ma={-1,-1};
int par2[200001];
void dfs(int no,int par=-1,int levv=0){
	ma=max(ma,{levv,no});
	par2[no]=par;
	for(auto j:adj[no]){
		if(j!=par){
			dfs(j,no,levv+1);
		}
	}
}
void dfs2(int no,int par=-1,int levv=0){
	ma=max(ma,{levv,no});
	par2[no]=par;
	for(auto j:adj2[no]){
		if(j!=par){
			dfs2(j,no,levv+1);
		}
	}
}
int pp;
/*void dfs3(int no,int par=-1,int levv=0){
	ma=max(ma,{levv,no});
	par2[no]=par;
	ans[no]=pp;
	for(auto j:adj2[no]){
		if(j!=par){
			dfs3(j,no,levv+1);
		}
	}
}*/
 
map<int,int> ee;
int co=0;
void add(int i){
	ee[it[i]]++;
	if(ee[it[i]]==1){
		co++;
	}
}
void remove(int i){
	ee[it[i]]--;
	if(ee[it[i]]==0){
		co--;
	}
}
//set<pair<int,int>> zz;
int ma2[200001];
void dfs4(int no,int par=-1,int lev=0){
	ma2[no]=0;
	for(auto j:adj2[no]){
		if(j!=par){
			dfs4(j,no,lev+1);
			ma2[no]=max(ma2[no],ma2[j]+1);
		}
	}
}
 
set<pair<int,int>> zz;
int ne[200001];
vector<pair<int,int>> pre2[200001];
void dfs5(int no,int par=-1,int lev=0,int zz2=1){
	vector<pair<int,int>> mm;
	//cout<<no<<";";
 
	for(auto j:adj2[no]){
		if(j!=par){
			mm.pb({ma2[j],j});
		}
	}
	sort(mm.begin(),mm.end());
	reverse(mm.begin(),mm.end());
	vector<pair<int,int>> kk;
	add(no);
	zz.insert({lev,no});
	int ind3=-1;
	for(auto j:mm){
		if(zz.find({lev,no})==zz.end()){
			zz.insert({lev,no});
			add(no);
		}
		ind3++;
		int ne=-1;
		if(ind3==0){
			if(mm.size()>=2){
				ne=mm[1].a;
			}
		}
		else if(ind3==1){
			ne=mm[0].a;;
		}
		if(ne>=0){
			ne++;
			auto jj=zz.lower_bound({lev-ne,-1});
			vector<pair<int,int>> ll;
			while(jj!=zz.end()){
				if((*jj).a>=lev){
					break;
				}
				remove((*jj).b);
				ll.pb(*jj);
				jj++;
			}
			for(auto ii:ll){
				kk.pb(ii);
				zz.erase(ii);
			}
		}
		dfs5(j.b,no,lev+1,ind3==0);
	}
	if(mm.size()){
		ne[no]=mm[0].b;

	}
	else{
		ne[no]=-1;
	}
	if(mm.size()==1){
		int ne=mm[0].a+1;
		auto jj=zz.lower_bound({lev-ne,-1});
		vector<pair<int,int>> ll;
		while(jj!=zz.end()){
			if((*jj).a>=lev){
				break;
			}
			remove((*jj).b);
			ll.pb(*jj);
			jj++;
		}
		for(auto ii:ll){
			kk.pb(ii);
			zz.erase(ii);
		}
	}
	
	pre2[no]=kk;
	if(zz.find({lev,no})!=zz.end()){
		zz.erase({lev,no});
		remove(no);
	}
	
 
	ans[no]=co;
 
	//lev-1 to lev-ma[no]
	/*if(zz2==1){
		int cur=no;
		while(cur>=0){
			for(auto j:pre2[no]){
				if(j.a<lev){
					zz.insert(j);
					add(j.b);
				}
			}
			break;
			cur=ne[cur];
		}
	}*/
}
int ha=0;
 
void solve(vector<int> ss){
	set<pair<int,int>> tt;
	ee.clear();
	co=0;
 
	//map of present colours
	int ind=ss.size();
	for(int i=0;i<ss.size();i++){
		tt.insert({i,ss[i]});
	}
	for(int i=ss.size()-1;i>=0;i--){
 
		auto j=tt.lower_bound({i+1,-1});
		vector<pair<int,int>> kk;
		while(j!=tt.end()){
			if((*j).a>i+val[ss[i]]){
				break;
			}
			if((*j).a>=ind){
				remove((*j).b);
			}
			kk.pb(*j);
			j++;
			/*if(ss[0]==1){
				cout<<(*j).b<<"?"<<endl;
 
			}*/
		}
		for(auto j:kk){
			tt.erase(j);
		}
		if(i<(int)(ss.size())-i-1){
			int ind2=2*i+1;
			while(ind>ind2){
				ind--;
				if(tt.find({ind,ss[ind]})!=tt.end()){
					add(ss[ind]);
				}
			}
			ans[ss[i]]=co;
			//continue;
		/*	if(ss[i]==0){
				for(auto jj:ee){
					cout<<jj.a<<",,"<<jj.b<<endl;
				}
			}*/
			dfs4(ss[i]);
		/*	if(ss[i]==0){
				cout<<ma2[ss[i]]<<"?"<<endl;
			}*/
			dfs5(ss[i]);
		//	cout<<endl;
			//ans[ss[i]]=co;
		}
		if(i==(int)ss.size()-i-1 and ha==0){
			ha=1;
			dfs4(ss[i]);
			dfs5(ss[i]);
 
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n-1;i++){
		int aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		adj[aa].pb(bb);
		adj[bb].pb(aa);
	}
	for(int i=0;i<n;i++){
		cin>>it[i];
	}
	dfs(0);
	int x=ma.b;
	ma={-1,-1};
	dfs(x);
	dfs2(x);
 
	vector<int> ss;
	int cur=ma.b;
	while(true){
		ss.pb(cur);
		cur=par2[cur];
		if(cur==-1){
			break;
		}
	}
	for(auto j:ss){
		vis[j]=1;
	}
	for(int i=0;i<n;i++){
		for(auto j:adj[i]){
			if(vis[i]+vis[j]<2){
				adj2[i].pb(j);
			}
		}
	}
 
	for(auto j:ss){
		ma={-1,-1};
		dfs2(j);
		val[j]=ma.a;
//		cout<<j<<":"<<ma.a<<endl;
	}
 
	solve(ss);
	reverse(ss.begin(),ss.end());
	solve(ss);
/*	for(auto j:ss){
		pp=ans[j];
		dfs3(j);
	}*/
	for(int i=0;i<n;i++){
		cout<<ans[i]<<endl;
	}
 
 
	
 
	
 
 
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'void solve(std::vector<int>)':
joi2019_ho_t5.cpp:183:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |  for(int i=0;i<ss.size();i++){
      |              ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 11 ms 14700 KB Output is correct
3 Correct 9 ms 14556 KB Output is correct
4 Correct 11 ms 14824 KB Output is correct
5 Correct 10 ms 14676 KB Output is correct
6 Correct 11 ms 14676 KB Output is correct
7 Correct 11 ms 14932 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14704 KB Output is correct
10 Correct 12 ms 14588 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 8 ms 14664 KB Output is correct
13 Correct 9 ms 14696 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 10 ms 14700 KB Output is correct
16 Correct 8 ms 14696 KB Output is correct
17 Correct 10 ms 14676 KB Output is correct
18 Correct 9 ms 14772 KB Output is correct
19 Correct 9 ms 14704 KB Output is correct
20 Correct 12 ms 14784 KB Output is correct
21 Correct 9 ms 14832 KB Output is correct
22 Correct 10 ms 14700 KB Output is correct
23 Correct 10 ms 14700 KB Output is correct
24 Correct 10 ms 14676 KB Output is correct
25 Correct 10 ms 14636 KB Output is correct
26 Correct 11 ms 14680 KB Output is correct
27 Correct 13 ms 14764 KB Output is correct
28 Correct 9 ms 14780 KB Output is correct
29 Correct 9 ms 14804 KB Output is correct
30 Correct 8 ms 14700 KB Output is correct
31 Correct 12 ms 14716 KB Output is correct
32 Correct 9 ms 14804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 28508 KB Output is correct
2 Correct 179 ms 34600 KB Output is correct
3 Correct 42 ms 19656 KB Output is correct
4 Correct 310 ms 38884 KB Output is correct
5 Correct 395 ms 49728 KB Output is correct
6 Correct 489 ms 51896 KB Output is correct
7 Correct 237 ms 37640 KB Output is correct
8 Correct 291 ms 39416 KB Output is correct
9 Correct 293 ms 39328 KB Output is correct
10 Correct 280 ms 40228 KB Output is correct
11 Correct 144 ms 37264 KB Output is correct
12 Correct 385 ms 46260 KB Output is correct
13 Correct 332 ms 44320 KB Output is correct
14 Correct 292 ms 51032 KB Output is correct
15 Correct 131 ms 36932 KB Output is correct
16 Correct 259 ms 45900 KB Output is correct
17 Correct 288 ms 52248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 370 ms 40584 KB Output is correct
2 Correct 752 ms 58932 KB Output is correct
3 Correct 56 ms 21108 KB Output is correct
4 Correct 536 ms 45708 KB Output is correct
5 Correct 738 ms 60888 KB Output is correct
6 Correct 569 ms 56892 KB Output is correct
7 Correct 491 ms 47988 KB Output is correct
8 Correct 422 ms 46064 KB Output is correct
9 Correct 480 ms 46428 KB Output is correct
10 Correct 425 ms 47936 KB Output is correct
11 Correct 403 ms 48036 KB Output is correct
12 Correct 642 ms 55732 KB Output is correct
13 Correct 465 ms 50692 KB Output is correct
14 Correct 601 ms 55976 KB Output is correct
15 Correct 207 ms 47160 KB Output is correct
16 Correct 616 ms 55956 KB Output is correct
17 Correct 551 ms 57132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 11 ms 14700 KB Output is correct
3 Correct 9 ms 14556 KB Output is correct
4 Correct 11 ms 14824 KB Output is correct
5 Correct 10 ms 14676 KB Output is correct
6 Correct 11 ms 14676 KB Output is correct
7 Correct 11 ms 14932 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14704 KB Output is correct
10 Correct 12 ms 14588 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 8 ms 14664 KB Output is correct
13 Correct 9 ms 14696 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 10 ms 14700 KB Output is correct
16 Correct 8 ms 14696 KB Output is correct
17 Correct 10 ms 14676 KB Output is correct
18 Correct 9 ms 14772 KB Output is correct
19 Correct 9 ms 14704 KB Output is correct
20 Correct 12 ms 14784 KB Output is correct
21 Correct 9 ms 14832 KB Output is correct
22 Correct 10 ms 14700 KB Output is correct
23 Correct 10 ms 14700 KB Output is correct
24 Correct 10 ms 14676 KB Output is correct
25 Correct 10 ms 14636 KB Output is correct
26 Correct 11 ms 14680 KB Output is correct
27 Correct 13 ms 14764 KB Output is correct
28 Correct 9 ms 14780 KB Output is correct
29 Correct 9 ms 14804 KB Output is correct
30 Correct 8 ms 14700 KB Output is correct
31 Correct 12 ms 14716 KB Output is correct
32 Correct 9 ms 14804 KB Output is correct
33 Correct 136 ms 28508 KB Output is correct
34 Correct 179 ms 34600 KB Output is correct
35 Correct 42 ms 19656 KB Output is correct
36 Correct 310 ms 38884 KB Output is correct
37 Correct 395 ms 49728 KB Output is correct
38 Correct 489 ms 51896 KB Output is correct
39 Correct 237 ms 37640 KB Output is correct
40 Correct 291 ms 39416 KB Output is correct
41 Correct 293 ms 39328 KB Output is correct
42 Correct 280 ms 40228 KB Output is correct
43 Correct 144 ms 37264 KB Output is correct
44 Correct 385 ms 46260 KB Output is correct
45 Correct 332 ms 44320 KB Output is correct
46 Correct 292 ms 51032 KB Output is correct
47 Correct 131 ms 36932 KB Output is correct
48 Correct 259 ms 45900 KB Output is correct
49 Correct 288 ms 52248 KB Output is correct
50 Correct 370 ms 40584 KB Output is correct
51 Correct 752 ms 58932 KB Output is correct
52 Correct 56 ms 21108 KB Output is correct
53 Correct 536 ms 45708 KB Output is correct
54 Correct 738 ms 60888 KB Output is correct
55 Correct 569 ms 56892 KB Output is correct
56 Correct 491 ms 47988 KB Output is correct
57 Correct 422 ms 46064 KB Output is correct
58 Correct 480 ms 46428 KB Output is correct
59 Correct 425 ms 47936 KB Output is correct
60 Correct 403 ms 48036 KB Output is correct
61 Correct 642 ms 55732 KB Output is correct
62 Correct 465 ms 50692 KB Output is correct
63 Correct 601 ms 55976 KB Output is correct
64 Correct 207 ms 47160 KB Output is correct
65 Correct 616 ms 55956 KB Output is correct
66 Correct 551 ms 57132 KB Output is correct
67 Correct 45 ms 18356 KB Output is correct
68 Correct 212 ms 31704 KB Output is correct
69 Correct 258 ms 38596 KB Output is correct
70 Correct 281 ms 38940 KB Output is correct
71 Correct 453 ms 49692 KB Output is correct
72 Correct 383 ms 51916 KB Output is correct
73 Correct 358 ms 37772 KB Output is correct
74 Correct 276 ms 40480 KB Output is correct
75 Correct 304 ms 39388 KB Output is correct
76 Correct 281 ms 40660 KB Output is correct
77 Correct 193 ms 37620 KB Output is correct
78 Correct 434 ms 47456 KB Output is correct
79 Correct 303 ms 45260 KB Output is correct
80 Correct 286 ms 49620 KB Output is correct
81 Correct 136 ms 36760 KB Output is correct
82 Correct 341 ms 45852 KB Output is correct
83 Correct 286 ms 52148 KB Output is correct
84 Correct 350 ms 39140 KB Output is correct
85 Correct 393 ms 50456 KB Output is correct
86 Correct 371 ms 52240 KB Output is correct
87 Correct 269 ms 38068 KB Output is correct
88 Correct 328 ms 41136 KB Output is correct
89 Correct 365 ms 40136 KB Output is correct
90 Correct 327 ms 41120 KB Output is correct
91 Correct 251 ms 38136 KB Output is correct
92 Correct 450 ms 50256 KB Output is correct
93 Correct 371 ms 42652 KB Output is correct
94 Correct 347 ms 46024 KB Output is correct
95 Correct 171 ms 37248 KB Output is correct
96 Correct 328 ms 46188 KB Output is correct
97 Correct 373 ms 52748 KB Output is correct
98 Correct 466 ms 43872 KB Output is correct
99 Correct 515 ms 50952 KB Output is correct
100 Correct 537 ms 54244 KB Output is correct
101 Correct 413 ms 39616 KB Output is correct
102 Correct 384 ms 42060 KB Output is correct
103 Correct 452 ms 42248 KB Output is correct
104 Correct 404 ms 42412 KB Output is correct
105 Correct 280 ms 38868 KB Output is correct
106 Correct 508 ms 47456 KB Output is correct
107 Correct 442 ms 46328 KB Output is correct
108 Correct 565 ms 50260 KB Output is correct
109 Correct 229 ms 41796 KB Output is correct
110 Correct 484 ms 48232 KB Output is correct
111 Correct 461 ms 55092 KB Output is correct