답안 #715569

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
715569 2023-03-27T08:32:07 Z Urvuk3 경주 (Race) (IOI11_race) C++17
100 / 100
1532 ms 57664 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int INF=1e9;
const ll LINF=1e18;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define pb push_back

void PRINT(int x) {cerr << x;}
void PRINT(ll x) {cerr << x;}
void PRINT(double x) {cerr << x;}
void PRINT(char x) {cerr << '\'' << x << '\'';}
void PRINT(string x) {cerr << '\"' << x << '\"';}
void PRINT(bool x) {cerr << (x ? "true" : "false");}

template<typename T,typename V>
void PRINT(pair<T,V>& x){
    cerr<<"{";
    PRINT(x.fi);
    cerr<<",";
    PRINT(x.se);
    cerr<<"}";
}
template<typename T>
void PRINT(T &x){
    int id=0;
    cerr<<"{";
    for(auto _i:x){
        cerr<<(id++ ? "," : "");
        PRINT(_i);
    }
    cerr<<"}";
}
void _PRINT(){
    cerr<<"]\n";
}
template<typename Head,typename... Tail>
void _PRINT(Head h,Tail... t){
    PRINT(h);
    if(sizeof...(t)) cerr<<", ";
    _PRINT(t...);
}

#ifndef ONLINE_JUDGE
#define Debug(x...) cerr<<"["<<#x<<"]=["; _PRINT(x)
#endif

vector<vector<pll>> adj;
vector<bool> vis;
vector<ll> siz;
vector<ll> dub,dubw;
map<ll,ll> global,local;
ll res,W;

void DfsDecompose(int node,int par){
    siz[node]=1;
    for(pll p:adj[node]){
        int v=p.fi,w=p.se;
        if(!vis[v] && v!=par){
            DfsDecompose(v,node);
            siz[node]+=siz[v];
        }
    }
}

void Dfs(int node,int par,int wp){
    if(par==-1){
        dub[node]=1;
        dubw[node]=wp;
    }
    else{
        dub[node]=dub[par]+1;
        dubw[node]=dubw[par]+wp;
    }
    local[dubw[node]]=min((local.count(dubw[node]) ? local[dubw[node]] : INF),dub[node]);
    for(pll p:adj[node]){
        int v=p.fi,w=p.se;
        if(!vis[v] && v!=par){
            Dfs(v,node,w);
        }
    }
}

int Find(int node,int par,int n){
    for(pll p:adj[node]){
        int v=p.fi,w=p.se;
        if(!vis[v] && v!=par && siz[v]>n/2)
            return Find(v,node,n);
    }
    return node;
}

void Decompose(int node){
    DfsDecompose(node,-1);
    node=Find(node,-1,siz[node]);
    vis[node]=true;
    //Debug(node);
    global.clear();
    for(pll p:adj[node]){
        int v=p.fi,w=p.se;
        if(!vis[v]){
            local.clear();
            Dfs(v,-1,w);
            for(auto p:local){
                res=min(res,(global.count(W-p.fi) ? global[W-p.fi] : INF)+p.se);
            }
            for(auto p:local){
                global[p.fi]=min((global.count(p.fi) ? global[p.fi] : INF),p.se);
            }
            //Debug(v);
            //Debug(local);
        }
    }
    res=min(res,(global.count(W) ? global[W] : INF));
    for(pll p:adj[node]){
        int v=p.fi,w=p.se;
        if(!vis[v]) Decompose(v);
    }
}

int best_path(int N, int K, int H[][2], int L[]){
    adj.resize(N+1); siz.resize(N+1); vis.resize(N+1); dub.resize(N+1); dubw.resize(N+1);
    W=K;
    for(int i=0;i<N-1;i++){
        int u=H[i][0],v=H[i][1],w=L[i];
        adj[u].pb({v,w});
        adj[v].pb({u,w});
    }
    res=LINF;
    Decompose(0);
    return (res>=INF ? -1 : res);
}

/*
4 3
0 1 1
1 2 2
1 3 4
2

3 3
0 1 1
1 2 1
-1

11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/

Compilation message

race.cpp: In function 'void DfsDecompose(int, int)':
race.cpp:68:20: warning: unused variable 'w' [-Wunused-variable]
   68 |         int v=p.fi,w=p.se;
      |                    ^
race.cpp: In function 'int Find(int, int, int)':
race.cpp:96:20: warning: unused variable 'w' [-Wunused-variable]
   96 |         int v=p.fi,w=p.se;
      |                    ^
race.cpp: In function 'void Decompose(int)':
race.cpp:126:20: warning: unused variable 'w' [-Wunused-variable]
  126 |         int v=p.fi,w=p.se;
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 224 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 224 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 3 ms 468 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 224 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 290 ms 11408 KB Output is correct
20 Correct 367 ms 11340 KB Output is correct
21 Correct 274 ms 11332 KB Output is correct
22 Correct 264 ms 11288 KB Output is correct
23 Correct 378 ms 11724 KB Output is correct
24 Correct 171 ms 10960 KB Output is correct
25 Correct 261 ms 15732 KB Output is correct
26 Correct 98 ms 18636 KB Output is correct
27 Correct 371 ms 22968 KB Output is correct
28 Correct 1528 ms 55712 KB Output is correct
29 Correct 1472 ms 57664 KB Output is correct
30 Correct 403 ms 25980 KB Output is correct
31 Correct 373 ms 25920 KB Output is correct
32 Correct 537 ms 26012 KB Output is correct
33 Correct 773 ms 24836 KB Output is correct
34 Correct 1395 ms 40908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 224 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 3 ms 468 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 290 ms 11408 KB Output is correct
40 Correct 367 ms 11340 KB Output is correct
41 Correct 274 ms 11332 KB Output is correct
42 Correct 264 ms 11288 KB Output is correct
43 Correct 378 ms 11724 KB Output is correct
44 Correct 171 ms 10960 KB Output is correct
45 Correct 261 ms 15732 KB Output is correct
46 Correct 98 ms 18636 KB Output is correct
47 Correct 371 ms 22968 KB Output is correct
48 Correct 1528 ms 55712 KB Output is correct
49 Correct 1472 ms 57664 KB Output is correct
50 Correct 403 ms 25980 KB Output is correct
51 Correct 373 ms 25920 KB Output is correct
52 Correct 537 ms 26012 KB Output is correct
53 Correct 773 ms 24836 KB Output is correct
54 Correct 1395 ms 40908 KB Output is correct
55 Correct 27 ms 2004 KB Output is correct
56 Correct 15 ms 1540 KB Output is correct
57 Correct 145 ms 13016 KB Output is correct
58 Correct 47 ms 11952 KB Output is correct
59 Correct 489 ms 27828 KB Output is correct
60 Correct 1493 ms 57040 KB Output is correct
61 Correct 473 ms 27764 KB Output is correct
62 Correct 365 ms 25976 KB Output is correct
63 Correct 501 ms 26016 KB Output is correct
64 Correct 1483 ms 34644 KB Output is correct
65 Correct 1170 ms 39204 KB Output is correct
66 Correct 1532 ms 55300 KB Output is correct
67 Correct 143 ms 24888 KB Output is correct
68 Correct 652 ms 36408 KB Output is correct
69 Correct 710 ms 36760 KB Output is correct
70 Correct 661 ms 34636 KB Output is correct