Submission #866342

# Submission time Handle Problem Language Result Execution time Memory
866342 2023-10-26T00:44:06 Z yeediot Race (IOI11_race) C++17
100 / 100
327 ms 71588 KB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define pb push_back
#define sz(x) (long long)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<long long>
#define vp vector<pii>
#define vvi vector<vi>
//Don't open the standings during contests.
void setIO(string s) {
    freopen((s).c_str(), "r", stdin);
    //freopen((s + ".ou).c_str(), "w", stdout);
}
const long long mxn=1e6+5;
vector<pii>adj[mxn];
long long cnt[mxn];
bool vis[mxn];
int n,k;
void cnt_sz(long long v,long long pa){
    //cout<<v<<' ';
    cnt[v]=1;
    for(auto [u,d]:adj[v]){
        if(vis[u] or u==pa)continue;
        cnt_sz(u,v);
        //cout<<u<<' ';
        cnt[v]+=cnt[u];
    }
}
long long find(long long v,long long pa,long long tar){
    for(auto [u,d]:adj[v]){
        if(vis[u] or u==pa)continue;
        if(cnt[u]*2>tar)return find(u,v,tar);
    }
    return v;
}
long long ans=1e9;
vector<pii>dis;
void dfs(long long v,long long pa,long long l,long long len){
    if(len>k)return;
    dis.pb({l,len});
    for(auto [u,d]:adj[v]){
        if(vis[u] or u==pa)continue;
        dfs(u,v,l+1,len+d);
    }
}
long long f[mxn];
void cd(long long v){
    cnt_sz(v,v);
    v=find(v,v,cnt[v]);
    vis[v]=1;
    f[0]=0;
    vector<long long>undo;
    for(auto [u,d]:adj[v]){
        if(vis[u])continue;
        dis.clear();
        dis.pb({0,0});
        dfs(u,v,1,d);
        for(long long j=0;j<sz(dis);j++){
            //cout<<dis[j].F<<' '<<dis[j].S<<'\n';
            chmin(ans,f[k-dis[j].S]+dis[j].F);
           // cout<<u<<' '<<dis[j].S<<' '<<dis[j].F<<' '<<f[k-dis[j].S]<<' '<<ans<<'\n';
        }
        for(long long j=0;j<sz(dis);j++){
            undo.pb(dis[j].S);
            chmin(f[dis[j].S],dis[j].F);
        }
    }
    for(auto x:undo)f[x]=1e9;
    for(auto [u,d]:adj[v]){
        if(vis[u])continue;
        cd(u);
    }
}
int best_path(int N,int K,int H[][2],int L[]){
    n=N;
    k=K;
    for(long long i=0;i<N-1;i++){
        adj[H[i][0]].pb({H[i][1],L[i]});
        adj[H[i][1]].pb({H[i][0],L[i]});
    }
    for(long long i=1;i<=K;i++){
        f[i]=1e9;
    }
    cd(0);
    return (ans>=1e9?-1:ans);
}
//signed main(){
//    setIO("/Users/iantsai/Downloads/ioi2011tests/race-test/subtask1/grader.in.1");
//    ios::sync_with_stdio(0);
//    cin.tie(0);cout.tie(0);
//    int n,k;
//    cin>>n>>k;
//    int h[n-1][2];
//    int l[n-1];
//    for(long long i=0;i<n-1;i++){
//        cin>>h[i][0]>>h[i][1];
//        cin>>l[i];
//    }
//    for(long long i=0;i<n-1;i++){
//    }
//    cout<<best_path(n,k,h,l);
//}
 /*
 input:
  11 12
  0 1
  0 2
  2 3
  3 4
  4 5
  0 6
  6 7
  6 8
  8 9
  8 10
  3
  4
  5
  4
  6
  3
  2
  5
  6
  7
*/

Compilation message

race.cpp: In function 'void setIO(std::string)':
race.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s).c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33884 KB Output is correct
2 Correct 6 ms 33756 KB Output is correct
3 Correct 6 ms 33884 KB Output is correct
4 Correct 6 ms 33884 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33932 KB Output is correct
8 Correct 6 ms 33884 KB Output is correct
9 Correct 6 ms 33880 KB Output is correct
10 Correct 7 ms 33884 KB Output is correct
11 Correct 6 ms 33916 KB Output is correct
12 Correct 6 ms 33884 KB Output is correct
13 Correct 6 ms 33880 KB Output is correct
14 Correct 6 ms 33884 KB Output is correct
15 Correct 7 ms 33880 KB Output is correct
16 Correct 7 ms 33884 KB Output is correct
17 Correct 6 ms 33884 KB Output is correct
18 Correct 7 ms 33884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33884 KB Output is correct
2 Correct 6 ms 33756 KB Output is correct
3 Correct 6 ms 33884 KB Output is correct
4 Correct 6 ms 33884 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33932 KB Output is correct
8 Correct 6 ms 33884 KB Output is correct
9 Correct 6 ms 33880 KB Output is correct
10 Correct 7 ms 33884 KB Output is correct
11 Correct 6 ms 33916 KB Output is correct
12 Correct 6 ms 33884 KB Output is correct
13 Correct 6 ms 33880 KB Output is correct
14 Correct 6 ms 33884 KB Output is correct
15 Correct 7 ms 33880 KB Output is correct
16 Correct 7 ms 33884 KB Output is correct
17 Correct 6 ms 33884 KB Output is correct
18 Correct 7 ms 33884 KB Output is correct
19 Correct 6 ms 33884 KB Output is correct
20 Correct 8 ms 33884 KB Output is correct
21 Correct 8 ms 33928 KB Output is correct
22 Correct 8 ms 40028 KB Output is correct
23 Correct 8 ms 40024 KB Output is correct
24 Correct 9 ms 40028 KB Output is correct
25 Correct 9 ms 40028 KB Output is correct
26 Correct 7 ms 38040 KB Output is correct
27 Correct 9 ms 40172 KB Output is correct
28 Correct 7 ms 35932 KB Output is correct
29 Correct 7 ms 38044 KB Output is correct
30 Correct 8 ms 37980 KB Output is correct
31 Correct 8 ms 40028 KB Output is correct
32 Correct 8 ms 40028 KB Output is correct
33 Correct 8 ms 40028 KB Output is correct
34 Correct 9 ms 40212 KB Output is correct
35 Correct 8 ms 40116 KB Output is correct
36 Correct 9 ms 40028 KB Output is correct
37 Correct 8 ms 40028 KB Output is correct
38 Correct 8 ms 37980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33884 KB Output is correct
2 Correct 6 ms 33756 KB Output is correct
3 Correct 6 ms 33884 KB Output is correct
4 Correct 6 ms 33884 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33932 KB Output is correct
8 Correct 6 ms 33884 KB Output is correct
9 Correct 6 ms 33880 KB Output is correct
10 Correct 7 ms 33884 KB Output is correct
11 Correct 6 ms 33916 KB Output is correct
12 Correct 6 ms 33884 KB Output is correct
13 Correct 6 ms 33880 KB Output is correct
14 Correct 6 ms 33884 KB Output is correct
15 Correct 7 ms 33880 KB Output is correct
16 Correct 7 ms 33884 KB Output is correct
17 Correct 6 ms 33884 KB Output is correct
18 Correct 7 ms 33884 KB Output is correct
19 Correct 79 ms 43092 KB Output is correct
20 Correct 83 ms 42972 KB Output is correct
21 Correct 85 ms 43660 KB Output is correct
22 Correct 75 ms 44704 KB Output is correct
23 Correct 63 ms 43076 KB Output is correct
24 Correct 47 ms 42108 KB Output is correct
25 Correct 96 ms 45652 KB Output is correct
26 Correct 85 ms 50764 KB Output is correct
27 Correct 143 ms 49796 KB Output is correct
28 Correct 172 ms 61080 KB Output is correct
29 Correct 169 ms 59984 KB Output is correct
30 Correct 121 ms 49776 KB Output is correct
31 Correct 121 ms 49940 KB Output is correct
32 Correct 126 ms 49996 KB Output is correct
33 Correct 159 ms 48540 KB Output is correct
34 Correct 114 ms 49472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33884 KB Output is correct
2 Correct 6 ms 33756 KB Output is correct
3 Correct 6 ms 33884 KB Output is correct
4 Correct 6 ms 33884 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33932 KB Output is correct
8 Correct 6 ms 33884 KB Output is correct
9 Correct 6 ms 33880 KB Output is correct
10 Correct 7 ms 33884 KB Output is correct
11 Correct 6 ms 33916 KB Output is correct
12 Correct 6 ms 33884 KB Output is correct
13 Correct 6 ms 33880 KB Output is correct
14 Correct 6 ms 33884 KB Output is correct
15 Correct 7 ms 33880 KB Output is correct
16 Correct 7 ms 33884 KB Output is correct
17 Correct 6 ms 33884 KB Output is correct
18 Correct 7 ms 33884 KB Output is correct
19 Correct 6 ms 33884 KB Output is correct
20 Correct 8 ms 33884 KB Output is correct
21 Correct 8 ms 33928 KB Output is correct
22 Correct 8 ms 40028 KB Output is correct
23 Correct 8 ms 40024 KB Output is correct
24 Correct 9 ms 40028 KB Output is correct
25 Correct 9 ms 40028 KB Output is correct
26 Correct 7 ms 38040 KB Output is correct
27 Correct 9 ms 40172 KB Output is correct
28 Correct 7 ms 35932 KB Output is correct
29 Correct 7 ms 38044 KB Output is correct
30 Correct 8 ms 37980 KB Output is correct
31 Correct 8 ms 40028 KB Output is correct
32 Correct 8 ms 40028 KB Output is correct
33 Correct 8 ms 40028 KB Output is correct
34 Correct 9 ms 40212 KB Output is correct
35 Correct 8 ms 40116 KB Output is correct
36 Correct 9 ms 40028 KB Output is correct
37 Correct 8 ms 40028 KB Output is correct
38 Correct 8 ms 37980 KB Output is correct
39 Correct 79 ms 43092 KB Output is correct
40 Correct 83 ms 42972 KB Output is correct
41 Correct 85 ms 43660 KB Output is correct
42 Correct 75 ms 44704 KB Output is correct
43 Correct 63 ms 43076 KB Output is correct
44 Correct 47 ms 42108 KB Output is correct
45 Correct 96 ms 45652 KB Output is correct
46 Correct 85 ms 50764 KB Output is correct
47 Correct 143 ms 49796 KB Output is correct
48 Correct 172 ms 61080 KB Output is correct
49 Correct 169 ms 59984 KB Output is correct
50 Correct 121 ms 49776 KB Output is correct
51 Correct 121 ms 49940 KB Output is correct
52 Correct 126 ms 49996 KB Output is correct
53 Correct 159 ms 48540 KB Output is correct
54 Correct 114 ms 49472 KB Output is correct
55 Correct 12 ms 34648 KB Output is correct
56 Correct 13 ms 34908 KB Output is correct
57 Correct 66 ms 44968 KB Output is correct
58 Correct 38 ms 44988 KB Output is correct
59 Correct 79 ms 52808 KB Output is correct
60 Correct 268 ms 71588 KB Output is correct
61 Correct 126 ms 50396 KB Output is correct
62 Correct 157 ms 58264 KB Output is correct
63 Correct 160 ms 58072 KB Output is correct
64 Correct 327 ms 58616 KB Output is correct
65 Correct 125 ms 50256 KB Output is correct
66 Correct 247 ms 64700 KB Output is correct
67 Correct 99 ms 63380 KB Output is correct
68 Correct 172 ms 58304 KB Output is correct
69 Correct 155 ms 58308 KB Output is correct
70 Correct 196 ms 57744 KB Output is correct