Submission #913974

# Submission time Handle Problem Language Result Execution time Memory
913974 2024-01-20T16:34:55 Z MilosMilutinovic Hard route (IZhO17_road) C++14
52 / 100
850 ms 262144 KB
#include<bits/stdc++.h>
 
#define pb push_back
#define fi first
#define se second
#define mp make_pair
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
 
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
 
ll readint(){
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
 
int n,tot;
int mx[500005],cnt[500005];
vector<int> g[500005];
ll res=0,c=1;
 
void addedge(int x,int y){ g[x].pb(y); g[y].pb(x); }
 
void dfs(int x,int px,int d){
	mx[x]=d;
	cnt[x]=1;
	for(auto&v:g[x]){
		if(v==px) continue;
		dfs(v,x,d+1);
		if(mx[v]==mx[x]) cnt[x]+=cnt[v];
		else if(mx[x]<mx[v]) cnt[x]=cnt[v];
		mx[x]=max(mx[x],mx[v]);
	}
}
 
void reroot(int x,int px,int d){
	for(auto&v:g[x]){
		if(v!=px) mx[v]-=d;
	}
	sort(g[x].begin(),g[x].end(),[&](int i,int j){return mx[i]>mx[j];});
	if((int)g[x].size()>=3){
		ll cur=0,cc=0;
		cur=mx[g[x][0]]*1ll*(mx[g[x][1]]+mx[g[x][2]]);
		ll s=0;
		for(int j=0;j<(int)g[x].size();j++){
			if(mx[g[x][j]]==mx[g[x][2]]) cc+=s*cnt[g[x][j]];
			if(mx[g[x][j]]==mx[g[x][1]]) s+=cnt[g[x][j]];
		}
		if(res<cur) res=cur,c=cc;
		else if(res==cur) c+=cc;
	}
	int mx_bef=mx[x],cnt_bef=cnt[x];
	multiset<int> st;
	map<int,int> m;
	for(auto&v:g[x]){
		st.insert(mx[v]);
		m[mx[v]]+=cnt[v];
	}
	st.insert(0);
	m[0]+=1;
	for(auto&v:g[x]){
		if(v==px) continue;
		st.erase(st.find(mx[v]));
		m[mx[v]]-=cnt[v];
		mx[x]=*prev(st.end())+1;
		cnt[x]=m[mx[x]-1];
		reroot(v,x,d+1);
		st.emplace(mx[v]);
		m[mx[v]]+=cnt[v];
	}
	mx[x]=mx_bef;
	cnt[x]=cnt_bef;
}
 
int main(){
	n=readint();
	for(int i=1;i<n;i++) addedge(readint(),readint());
	dfs(1,1,0);
	reroot(1,1,0);
	printf("%lld %lld\n",res,c);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 4 ms 14936 KB Output is correct
7 Correct 4 ms 14936 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 5 ms 15092 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 5 ms 14944 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 5 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 5 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 4 ms 14944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 4 ms 14936 KB Output is correct
7 Correct 4 ms 14936 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 5 ms 15092 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 5 ms 14944 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 5 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 5 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 4 ms 14944 KB Output is correct
25 Correct 6 ms 15708 KB Output is correct
26 Correct 7 ms 15960 KB Output is correct
27 Correct 7 ms 16360 KB Output is correct
28 Correct 9 ms 16220 KB Output is correct
29 Correct 6 ms 16220 KB Output is correct
30 Correct 7 ms 16220 KB Output is correct
31 Correct 7 ms 16220 KB Output is correct
32 Correct 6 ms 15960 KB Output is correct
33 Correct 9 ms 15964 KB Output is correct
34 Correct 9 ms 16220 KB Output is correct
35 Correct 8 ms 16220 KB Output is correct
36 Correct 8 ms 16248 KB Output is correct
37 Correct 9 ms 16476 KB Output is correct
38 Correct 9 ms 17500 KB Output is correct
39 Correct 6 ms 15944 KB Output is correct
40 Correct 6 ms 15452 KB Output is correct
41 Correct 6 ms 15336 KB Output is correct
42 Correct 9 ms 14940 KB Output is correct
43 Correct 6 ms 14940 KB Output is correct
44 Correct 6 ms 14968 KB Output is correct
45 Correct 7 ms 15116 KB Output is correct
46 Correct 6 ms 14964 KB Output is correct
47 Correct 6 ms 15196 KB Output is correct
48 Correct 8 ms 15384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 4 ms 14936 KB Output is correct
7 Correct 4 ms 14936 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 5 ms 15092 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 5 ms 14944 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 5 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 5 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 4 ms 14944 KB Output is correct
25 Correct 6 ms 15708 KB Output is correct
26 Correct 7 ms 15960 KB Output is correct
27 Correct 7 ms 16360 KB Output is correct
28 Correct 9 ms 16220 KB Output is correct
29 Correct 6 ms 16220 KB Output is correct
30 Correct 7 ms 16220 KB Output is correct
31 Correct 7 ms 16220 KB Output is correct
32 Correct 6 ms 15960 KB Output is correct
33 Correct 9 ms 15964 KB Output is correct
34 Correct 9 ms 16220 KB Output is correct
35 Correct 8 ms 16220 KB Output is correct
36 Correct 8 ms 16248 KB Output is correct
37 Correct 9 ms 16476 KB Output is correct
38 Correct 9 ms 17500 KB Output is correct
39 Correct 6 ms 15944 KB Output is correct
40 Correct 6 ms 15452 KB Output is correct
41 Correct 6 ms 15336 KB Output is correct
42 Correct 9 ms 14940 KB Output is correct
43 Correct 6 ms 14940 KB Output is correct
44 Correct 6 ms 14968 KB Output is correct
45 Correct 7 ms 15116 KB Output is correct
46 Correct 6 ms 14964 KB Output is correct
47 Correct 6 ms 15196 KB Output is correct
48 Correct 8 ms 15384 KB Output is correct
49 Correct 817 ms 124824 KB Output is correct
50 Correct 780 ms 142412 KB Output is correct
51 Correct 653 ms 157012 KB Output is correct
52 Correct 693 ms 104180 KB Output is correct
53 Correct 616 ms 152600 KB Output is correct
54 Correct 583 ms 171988 KB Output is correct
55 Correct 614 ms 123980 KB Output is correct
56 Correct 634 ms 144824 KB Output is correct
57 Correct 767 ms 168788 KB Output is correct
58 Correct 660 ms 148828 KB Output is correct
59 Correct 620 ms 148484 KB Output is correct
60 Correct 648 ms 139144 KB Output is correct
61 Runtime error 850 ms 262144 KB Execution killed with signal 9
62 Halted 0 ms 0 KB -