답안 #967811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967811 2024-04-22T22:20:16 Z amirhoseinfar1385 Hard route (IZhO17_road) C++17
52 / 100
650 ms 151404 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
int n;
vector<int>adj[maxn];
vector<pair<int,int>>alldp[maxn],ps[maxn],suf[maxn];
long long c2(int a){
	long long ret=a;
	ret=ret*(ret-1)/2;
	return ret;
}

pair<int,int>comp(pair<int,int>a,pair<int,int>b){
	pair<int,int>ret=a;
	if(a.first==b.first){
		a.second+=b.first;
	}else if(b.first>a.first){
		ret=b;
	}
	if(ret.first==0){
		ret.second=max(1,ret.second);
	}
	return ret;
}

void vorod(){
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
}

pair<int,int> dfsdown(int u=1,int par=-1){
	if(par!=-1){
		sort(adj[u].begin(),adj[u].end());
		adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
	}
	pair<int,int>ret=make_pair(0,1);
	for(auto x:adj[u]){
		if(x==par){
			continue;
		}
		pair<int,int>hey=dfsdown(x,u);
		hey.first++;
		alldp[u].push_back(hey);
		if(hey.first==ret.first){
			ret.second+=hey.second;
		}else{
			ret=max(ret,hey);
		}
	}
	return ret;
}

void preall(int ind){
	int sz=alldp[ind].size();
	ps[ind].resize(sz+1);
	suf[ind].resize(sz+1);
	for(int i=1;i<=sz;i++){
		if(ps[ind][i-1].first==alldp[ind][i].first){
			ps[ind][i].first=alldp[ind][i].first;
			ps[ind][i].second=alldp[ind][i].second+ps[ind][i-1].second;
		}else{
			ps[ind][i]=max(ps[ind][i-1],alldp[ind][i-1]);
		}
	}
	for(int i=sz-1;i>=0;i--){
		if(suf[ind][i+1].first==alldp[ind][i].first){
			suf[ind][i].first=alldp[ind][i].first;
			suf[ind][i].second=alldp[ind][i].second+suf[ind][i+1].second;
		}else{
			suf[ind][i]=max(suf[ind][i+1],alldp[ind][i]);
		}
	}
}
pair<int,int>ft[maxn];

void dfsup(int u=1,int par=-1){
	for(int i=0;i<(int)adj[u].size();i++){
		int x=adj[u][i];
	//	cout<<"pas: "<<u<<" "<<i<<" "<<x<<" "<<ps[u][i].first<<" "<<suf[u][i].first<<endl;
		ft[x]=comp(ft[u],comp(ps[u][i],suf[u][i+1]));
		ft[x].first++;
		dfsup(x,u);
	}
	if(par!=-1){
		alldp[u].push_back(ft[u]);
	}
}

void pre(){
	dfsdown();
	for(int i=1;i<=n;i++){
		preall(i);
	}
	dfsup();	
}

long long cal(int u){
	if((int)alldp[u].size()<3){
		return 0;
	}
	sort(alldp[u].rbegin(),alldp[u].rend());
	long long ret=alldp[u][0].first*(alldp[u][1].first+alldp[u][2].first);
	return ret;
}

long long calted(int u){
	sort(alldp[u].rbegin(),alldp[u].rend());	
//	cout<<"hey: "<<u<<endl;
//	for(auto x:alldp[u]){
//		cout<<x.first<<" "<<x.second<<"\n";
//	}
	long long suma=0,ret=0;
	for(auto x:alldp[u]){
		if(x.first==alldp[u][2].first){
			suma+=x.second;
		}
	}
	if(alldp[u][2].first==alldp[u][1].first){
	//	cout<<"wtf: "<<suma<<" "<<c2(suma)<<endl;
		ret+=c2(suma);
		for(auto x:alldp[u]){
			if(x.first==alldp[u][2].first)
				ret-=c2(x.second);
		}
		return ret;
	}
	ret=suma*alldp[u][1].second;
	return ret;
}

void solve(){
	long long mainres=0;
	for(int i=1;i<=n;i++){
		mainres=max(mainres,cal(i));
	}
	cout<<mainres<<" ";
	long long ted=0;
	for(int i=1;i<=n;i++){
		if(cal(i)==mainres&&mainres!=0){
			ted+=calted(i);
		}
	}
	if(mainres==0){
		ted=1;
	}
	cout<<ted<<endl;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	freopen("inp.txt","r",stdin);
	vorod();
	pre();
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 48984 KB Output is correct
2 Correct 11 ms 48988 KB Output is correct
3 Correct 11 ms 48988 KB Output is correct
4 Correct 12 ms 49080 KB Output is correct
5 Correct 11 ms 48988 KB Output is correct
6 Correct 11 ms 48988 KB Output is correct
7 Correct 11 ms 48988 KB Output is correct
8 Correct 11 ms 49088 KB Output is correct
9 Correct 11 ms 48984 KB Output is correct
10 Correct 11 ms 48988 KB Output is correct
11 Correct 12 ms 49112 KB Output is correct
12 Correct 11 ms 48988 KB Output is correct
13 Correct 13 ms 49044 KB Output is correct
14 Correct 12 ms 49120 KB Output is correct
15 Correct 14 ms 48988 KB Output is correct
16 Correct 11 ms 49072 KB Output is correct
17 Correct 12 ms 49112 KB Output is correct
18 Correct 11 ms 48988 KB Output is correct
19 Correct 11 ms 48988 KB Output is correct
20 Correct 11 ms 48984 KB Output is correct
21 Correct 11 ms 48988 KB Output is correct
22 Correct 12 ms 48984 KB Output is correct
23 Correct 12 ms 49244 KB Output is correct
24 Correct 13 ms 48988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 48984 KB Output is correct
2 Correct 11 ms 48988 KB Output is correct
3 Correct 11 ms 48988 KB Output is correct
4 Correct 12 ms 49080 KB Output is correct
5 Correct 11 ms 48988 KB Output is correct
6 Correct 11 ms 48988 KB Output is correct
7 Correct 11 ms 48988 KB Output is correct
8 Correct 11 ms 49088 KB Output is correct
9 Correct 11 ms 48984 KB Output is correct
10 Correct 11 ms 48988 KB Output is correct
11 Correct 12 ms 49112 KB Output is correct
12 Correct 11 ms 48988 KB Output is correct
13 Correct 13 ms 49044 KB Output is correct
14 Correct 12 ms 49120 KB Output is correct
15 Correct 14 ms 48988 KB Output is correct
16 Correct 11 ms 49072 KB Output is correct
17 Correct 12 ms 49112 KB Output is correct
18 Correct 11 ms 48988 KB Output is correct
19 Correct 11 ms 48988 KB Output is correct
20 Correct 11 ms 48984 KB Output is correct
21 Correct 11 ms 48988 KB Output is correct
22 Correct 12 ms 48984 KB Output is correct
23 Correct 12 ms 49244 KB Output is correct
24 Correct 13 ms 48988 KB Output is correct
25 Correct 14 ms 50008 KB Output is correct
26 Correct 14 ms 50012 KB Output is correct
27 Correct 14 ms 50012 KB Output is correct
28 Correct 15 ms 50012 KB Output is correct
29 Correct 14 ms 50012 KB Output is correct
30 Correct 14 ms 50012 KB Output is correct
31 Correct 17 ms 49984 KB Output is correct
32 Correct 14 ms 50008 KB Output is correct
33 Correct 13 ms 50008 KB Output is correct
34 Correct 14 ms 50012 KB Output is correct
35 Correct 14 ms 50008 KB Output is correct
36 Correct 15 ms 50008 KB Output is correct
37 Correct 14 ms 50012 KB Output is correct
38 Correct 14 ms 50268 KB Output is correct
39 Correct 16 ms 49756 KB Output is correct
40 Correct 14 ms 49752 KB Output is correct
41 Correct 15 ms 49636 KB Output is correct
42 Correct 14 ms 49752 KB Output is correct
43 Correct 13 ms 49756 KB Output is correct
44 Correct 15 ms 49756 KB Output is correct
45 Correct 14 ms 49756 KB Output is correct
46 Correct 13 ms 49496 KB Output is correct
47 Correct 14 ms 49756 KB Output is correct
48 Correct 14 ms 49752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 48984 KB Output is correct
2 Correct 11 ms 48988 KB Output is correct
3 Correct 11 ms 48988 KB Output is correct
4 Correct 12 ms 49080 KB Output is correct
5 Correct 11 ms 48988 KB Output is correct
6 Correct 11 ms 48988 KB Output is correct
7 Correct 11 ms 48988 KB Output is correct
8 Correct 11 ms 49088 KB Output is correct
9 Correct 11 ms 48984 KB Output is correct
10 Correct 11 ms 48988 KB Output is correct
11 Correct 12 ms 49112 KB Output is correct
12 Correct 11 ms 48988 KB Output is correct
13 Correct 13 ms 49044 KB Output is correct
14 Correct 12 ms 49120 KB Output is correct
15 Correct 14 ms 48988 KB Output is correct
16 Correct 11 ms 49072 KB Output is correct
17 Correct 12 ms 49112 KB Output is correct
18 Correct 11 ms 48988 KB Output is correct
19 Correct 11 ms 48988 KB Output is correct
20 Correct 11 ms 48984 KB Output is correct
21 Correct 11 ms 48988 KB Output is correct
22 Correct 12 ms 48984 KB Output is correct
23 Correct 12 ms 49244 KB Output is correct
24 Correct 13 ms 48988 KB Output is correct
25 Correct 14 ms 50008 KB Output is correct
26 Correct 14 ms 50012 KB Output is correct
27 Correct 14 ms 50012 KB Output is correct
28 Correct 15 ms 50012 KB Output is correct
29 Correct 14 ms 50012 KB Output is correct
30 Correct 14 ms 50012 KB Output is correct
31 Correct 17 ms 49984 KB Output is correct
32 Correct 14 ms 50008 KB Output is correct
33 Correct 13 ms 50008 KB Output is correct
34 Correct 14 ms 50012 KB Output is correct
35 Correct 14 ms 50008 KB Output is correct
36 Correct 15 ms 50008 KB Output is correct
37 Correct 14 ms 50012 KB Output is correct
38 Correct 14 ms 50268 KB Output is correct
39 Correct 16 ms 49756 KB Output is correct
40 Correct 14 ms 49752 KB Output is correct
41 Correct 15 ms 49636 KB Output is correct
42 Correct 14 ms 49752 KB Output is correct
43 Correct 13 ms 49756 KB Output is correct
44 Correct 15 ms 49756 KB Output is correct
45 Correct 14 ms 49756 KB Output is correct
46 Correct 13 ms 49496 KB Output is correct
47 Correct 14 ms 49756 KB Output is correct
48 Correct 14 ms 49752 KB Output is correct
49 Correct 650 ms 144368 KB Output is correct
50 Correct 592 ms 148296 KB Output is correct
51 Correct 559 ms 151404 KB Output is correct
52 Correct 562 ms 139856 KB Output is correct
53 Incorrect 580 ms 147268 KB Output isn't correct
54 Halted 0 ms 0 KB -