답안 #913878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
913878 2024-01-20T11:28:45 Z MilosMilutinovic Hard route (IZhO17_road) C++14
52 / 100
637 ms 187836 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;
}

#define int long long

int n,tot;
int v[1000005],nxt[1000005],h[1000005],mx[1000005],cnt[1000005];
ll res=0,c=1;

void addedge(int x,int y){
	v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;
	v[++tot]=x; nxt[tot]=h[y]; h[y]=tot;
}

void dfs(int x,int px,int d){
	mx[x]=d;
	cnt[x]=1;
	for(int p=h[x];p;p=nxt[p]){
		if(v[p]==px) continue;
		dfs(v[p],x,d+1);
		if(mx[v[p]]==mx[x]) cnt[x]+=cnt[v[p]];
		else if(mx[x]<mx[v[p]]) cnt[x]=cnt[v[p]];
		mx[x]=max(mx[x],mx[v[p]]);
	}
}

void reroot(int x,int px,int d){
	vector<pii> f;
	for(int p=h[x];p;p=nxt[p]){
		if(v[p]!=px) mx[v[p]]-=d;
		f.pb(mp(mx[v[p]],cnt[v[p]]));
	}
	sort(f.rbegin(),f.rend());
	if((int)f.size()>=3){
		ll cur=0,cc=0;
		cur=f[0].fi*1ll*(f[1].fi+f[2].fi);
		ll s=0;
		for(int j=0;j<f.size();j++){
			if(f[j].fi==f[2].fi) cc+=s*f[2].se;
			if(f[j].fi==f[1].fi) s+=f[j].se;
		}
		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> c;
	for(int p=h[x];p;p=nxt[p]){
		st.insert(mx[v[p]]);
		c[mx[v[p]]]+=cnt[v[p]];
	}
	st.insert(0);
	c[0]+=1;
	for(int p=h[x];p;p=nxt[p]){
		if(v[p]==px) continue;
		st.erase(st.find(mx[v[p]]));
		c[mx[v[p]]]-=cnt[v[p]];
		mx[x]=*prev(st.end())+1;
		cnt[x]=c[mx[x]-1];
		reroot(v[p],x,d+1);
		st.emplace(mx[v[p]]);
		c[mx[v[p]]]+=cnt[v[p]];
	}
	mx[x]=mx_bef;
	cnt[x]=cnt_bef;
}

signed 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;
}

Compilation message

road.cpp: In function 'void reroot(long long int, long long int, long long int)':
road.cpp:60:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   for(int j=0;j<f.size();j++){
      |               ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8664 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8572 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8664 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8572 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 4 ms 9564 KB Output is correct
26 Correct 4 ms 9820 KB Output is correct
27 Correct 5 ms 10284 KB Output is correct
28 Correct 7 ms 10232 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 4 ms 10332 KB Output is correct
31 Correct 5 ms 10332 KB Output is correct
32 Correct 4 ms 9820 KB Output is correct
33 Correct 4 ms 9996 KB Output is correct
34 Correct 4 ms 10332 KB Output is correct
35 Correct 4 ms 10076 KB Output is correct
36 Correct 4 ms 10076 KB Output is correct
37 Correct 5 ms 10332 KB Output is correct
38 Correct 6 ms 11612 KB Output is correct
39 Correct 4 ms 9560 KB Output is correct
40 Correct 4 ms 9052 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 3 ms 8612 KB Output is correct
43 Correct 4 ms 8536 KB Output is correct
44 Correct 3 ms 8536 KB Output is correct
45 Correct 3 ms 8540 KB Output is correct
46 Correct 3 ms 8536 KB Output is correct
47 Correct 3 ms 8796 KB Output is correct
48 Correct 4 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8664 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8572 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 4 ms 9564 KB Output is correct
26 Correct 4 ms 9820 KB Output is correct
27 Correct 5 ms 10284 KB Output is correct
28 Correct 7 ms 10232 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 4 ms 10332 KB Output is correct
31 Correct 5 ms 10332 KB Output is correct
32 Correct 4 ms 9820 KB Output is correct
33 Correct 4 ms 9996 KB Output is correct
34 Correct 4 ms 10332 KB Output is correct
35 Correct 4 ms 10076 KB Output is correct
36 Correct 4 ms 10076 KB Output is correct
37 Correct 5 ms 10332 KB Output is correct
38 Correct 6 ms 11612 KB Output is correct
39 Correct 4 ms 9560 KB Output is correct
40 Correct 4 ms 9052 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 3 ms 8612 KB Output is correct
43 Correct 4 ms 8536 KB Output is correct
44 Correct 3 ms 8536 KB Output is correct
45 Correct 3 ms 8540 KB Output is correct
46 Correct 3 ms 8536 KB Output is correct
47 Correct 3 ms 8796 KB Output is correct
48 Correct 4 ms 8796 KB Output is correct
49 Correct 635 ms 147200 KB Output is correct
50 Correct 588 ms 169196 KB Output is correct
51 Correct 637 ms 187836 KB Output is correct
52 Incorrect 624 ms 121368 KB Output isn't correct
53 Halted 0 ms 0 KB -