Submission #1083649

# Submission time Handle Problem Language Result Execution time Memory
1083649 2024-09-03T17:59:05 Z Rawlat_vanak Race (IOI11_race) C++17
9 / 100
3000 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define speedIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define pb push_back

vector<vector<pii>> graph;
vector<ll> sz,dist,rep,depth;
vector<bool> removed;
ll n,k,final;

void subtreesize(ll u, ll p){
	sz[u]=1;
	for(pii v:graph[u]){
		if(v.ff==p) continue;
		subtreesize(v.ff,u);
		sz[u]+=sz[v.ff];
	}
}

ll findcentroid(ll u, ll p, ll m){
	for(pii v:graph[u]){
		if(removed[v.ff]) continue;
		if(v.ff==p) continue;
		if(2*sz[v.ff]>m){
			return findcentroid(v.ff,u,m);
		}
	}
	return u;
}


void dfs(ll u, ll p, ll cen){
	for(pii x:graph[u]){
		ll v=x.ff,w=x.ss;
		if(v==p) continue;
		if(removed[v]) continue;
		if(u!=cen) rep[v]=rep[u];
		depth[v]=depth[u]+1;
		dist[v]=dist[u]+w;
		dfs(v,u,cen);
		

	}
}


void centroid_decomp(ll u, ll p){

	
	ll ctrd=findcentroid(u,p,sz[u]);
	//cout<<"hi "<<u<<' '<<p<<' '<<ctrd<<'\n';
	dist.clear();
	dist.resize(n,0);
	rep.clear();
	rep.resize(n);
	depth.clear();
	depth.resize(n,0);
	
	for(pii v:graph[ctrd]){
		if(removed[v.ff]) continue;
		rep[v.ff]=v.ff;
	}
	dfs(ctrd,-1,ctrd);

	vector<vector<ll>> values(k+1);
	for(ll i=0;i<n;i++){
		if(removed[i]) continue;
		if(dist[i]==0 or depth[i]==0 or dist[i]>k) continue;
		values[dist[i]].pb(i);
	}

	/*for(int i:dist){
		cout<<i<<' ';
	}
	cout<<'\n';
			
	for(int i:depth){
		cout<<i<<' ';
	}
	cout<<'\n';

	for(int i:rep){
		cout<<i<<' ';
	}
	cout<<'\n';*/

	ll ans=1e7;
	values[0].pb(0);
	for(ll i=0;i<=k;i++){
		if(values[i].size()==0) continue;
		if(values[k-i].size()!=0){
			for(int one:values[i]){
				for(int two:values[k-i]){
					//cout<<one<<" ori2[ri] "<<two<<'\n';
					if(rep[one]==rep[two]) continue;
					ans=min(ans,depth[one]+depth[two]);
					//cout<<ans<<' '<<ctrd<<' '<<i<<'\n';
				}
			}
			
		}
	}
	final=min(final,ans);




	removed[ctrd]=true;
	for(pii v:graph[ctrd]){
		if(removed[v.ff]) continue;
		centroid_decomp(v.ff,ctrd);
	}

	
}



int best_path(int N, int K, int h[][2], int l[]){
	n=N,k=K;
	graph.clear();
	graph.resize(n);
	sz.clear();
	sz.resize(n);
	removed.clear();
	removed.resize(n);
	dist.clear();
	dist.resize(n);
	for(ll i=0;i<n-1;i++){
		graph[h[i][0]].pb({h[i][1],l[i]});
		graph[h[i][1]].pb({h[i][0],l[i]});
	}
	final=1e7;
	subtreesize(0,-1);
	centroid_decomp(0,-1);
	if(final==1e7){
		return -1;
	}else{
		return final;
	}


}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 5 ms 604 KB Output is correct
22 Runtime error 346 ms 262144 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Execution timed out 3053 ms 14236 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 5 ms 604 KB Output is correct
22 Runtime error 346 ms 262144 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -