# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
982389 |
2024-05-14T07:58:31 Z |
Godgift42 |
Race (IOI11_race) |
C++17 |
|
1 ms |
2396 KB |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adj;
int n, k;
vector<int> subtr;
vector<bool> vis;
unordered_map<int,int> mp1;
unordered_map<int,int>mp2;
int ans=INT_MAX;
void findsize(int u, int p){
subtr[u]=1;
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
findsize(v,u);
subtr[u]+=subtr[v];
}
}
}
int findcentroid(int u, int p, int sz){
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v] and subtr[v]>sz/2)return findcentroid(v,u,sz);
}
return u;
}
void dfs1(int u, int p, int dist, int depth, int cnt){
if(dist>k)return;
if((mp1[k-dist] and mp2[k-dist]==cnt) or k==dist) ans=min(ans,mp1[k-dist]+depth);
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
dfs1(v,u,dist+w,depth+1,cnt);
}
}
}
void dfs2(int u, int p, int dist, int depth,int cnt){
if(dist>k)return;
if(mp1[dist]==0){
mp1[dist]=depth;
mp2[dist]=cnt;
}
else{
if(mp2[dist]>cnt){
mp1[dist]=depth;
mp2[dist]=cnt;
}
else if(mp1[dist]>depth){
mp1[dist]=depth;
mp2[dist]=cnt;
}
}
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
dfs2(v,u,dist+w,depth+1,cnt);
}
}
}
void buildcentroid(int u, int p, int cnt){
findsize(u,u);
int c=findcentroid(u,u,subtr[u]);
vis[c]=true;
mp2[0]=cnt;
for(auto [v,w]:adj[c]){
if(v!=p and !vis[v]){
dfs1(v,c,w,1,cnt);
dfs2(v,c,w,1,cnt);
}
}
for(auto [v,w]:adj[c]){
if(!vis[v])buildcentroid(v,c,cnt-1);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
adj.resize(n);
subtr.resize(n);
vis.resize(n);
for(int i=0;i<n-1;i++){
adj[H[i][1]].push_back({H[i][0],L[i]});
adj[H[i][0]].push_back({H[i][1],L[i]});
}
buildcentroid(0,-1,n+1);
if(ans==INT_MAX)return -1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |