Submission #854480

# Submission time Handle Problem Language Result Execution time Memory
854480 2023-09-27T17:56:59 Z Onur_Ilgaz Race (IOI11_race) C++17
100 / 100
345 ms 99264 KB
#include "race.h"
#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define int long long
#define inf ((int)1e18)
using namespace std;
int ans, k;
vector <vector<pair<int, int> > > adj;
vector <map<int, int> > mp;
vector <int> depth, len;
 
void dfs(int node, int ata) {
    if(adj[node].size() == 1 and adj[node][0].first == ata) {
        mp[node].insert({0, 0});
        return;
    }
    // cerr<<"nodeata: "<<node<<" "<<ata<<"\n";
    int mx = 0, mxnode = -1, ww = -1;
    for(auto &[to, w]:adj[node]) {
        if(to == ata) continue;
        dfs(to, node);
        if((int)mp[to].size() > mx) {
            mxnode = to;
            mx = (int)mp[to].size();
            ww = w;
        }
    }
 
    mp[node].swap(mp[mxnode]);
    // do depth and len
    len[node] = len[mxnode] + ww;
    depth[node] = depth[mxnode] + 1;
 
    mp[node][0 - len[node]] = 0 - depth[node];
 
    if(mp[node].count(k - len[node])) {
        ans = min(ans, mp[node][k - len[node]] + depth[node]);
    }

    //kendi subtreesinden eklenen şeye bakabiliyor yanlışlıkla
    for(auto &[to, w]:adj[node]) {
        if(to == mxnode or to == ata) continue;
        for(auto it:mp[to]) {
            int realval = len[to] + it.first + w;
            int realdepth = depth[to] + it.second + 1;
            //check if k
            int need = (k - realval) - len[node];
            if(mp[node].count(need)) { 
                ans = min(ans, (mp[node][need] + depth[node] + realdepth));
            }
        }
        for(auto it:mp[to]) {
            int realval = len[to] + it.first + w;
            int realdepth = depth[to] + it.second + 1;
            // add it to map
            if(!(mp[node].count(realval - len[node])) ) {
                mp[node].insert({realval - len[node], realdepth - depth[node]});
            }
            else {
                mp[node][realval - len[node]] = min(mp[node][realval - len[node]], realdepth - depth[node]);
            }
        }
    }
}
 
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[])
{
    ans = inf, k = K;
    if(N == 1) {
        return -1;
    }
    adj.clear(); adj.resize(N + 5);
    map <int, int> tmp;
    mp.clear(); mp.assign(N + 5, tmp);
    depth.clear(); depth.resize(N + 5);
    len.clear(); len.resize(N + 5);
    for(int i = 0; i < N - 1; i++) {
        int a = H[i][0], b = H[i][1], w = L[i];
        adj[a].push_back({b, w});
        adj[b].push_back({a, w});
    }
    dfs(0, -1);
    // for(auto &[tv, td]:mp[0]) {
    //     int realval = tv + len[0];
    //     int realdepth = td + depth[0];
    //     cout<<realval<<" "<<realdepth<<"\n";
    // }
    return (ans == inf) ? -1 : ans;
}

// #define MAX_N 500000
 
// static int32_t N, K;
// static int32_t H[MAX_N][2];
// static int32_t L[MAX_N];
// static int32_t solution;
 
// inline 
// void my_assert(int e) {if (!e) abort();}
 
// void read_input()
// {
//   int i;
//   my_assert(2==scanf("%lld %lld",&N,&K));
//   for(i=0; i<N-1; i++)
//     my_assert(3==scanf("%lld %lld %lld",&H[i][0],&H[i][1],&L[i]));
// }
 
// int32_t main()
// {
//   int32_t ans;
//   read_input();
//   ans = best_path(N,K,H,L);
//   cout<<ans<<"\n";
//   return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2492 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2492 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2648 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2752 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2492 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 97 ms 30292 KB Output is correct
20 Correct 85 ms 30804 KB Output is correct
21 Correct 89 ms 30692 KB Output is correct
22 Correct 84 ms 30776 KB Output is correct
23 Correct 137 ms 44292 KB Output is correct
24 Correct 94 ms 36436 KB Output is correct
25 Correct 62 ms 30748 KB Output is correct
26 Correct 49 ms 39516 KB Output is correct
27 Correct 144 ms 48312 KB Output is correct
28 Correct 194 ms 87380 KB Output is correct
29 Correct 181 ms 85396 KB Output is correct
30 Correct 153 ms 48204 KB Output is correct
31 Correct 157 ms 48204 KB Output is correct
32 Correct 184 ms 48448 KB Output is correct
33 Correct 164 ms 46928 KB Output is correct
34 Correct 269 ms 79876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2492 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2648 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2752 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2648 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 97 ms 30292 KB Output is correct
40 Correct 85 ms 30804 KB Output is correct
41 Correct 89 ms 30692 KB Output is correct
42 Correct 84 ms 30776 KB Output is correct
43 Correct 137 ms 44292 KB Output is correct
44 Correct 94 ms 36436 KB Output is correct
45 Correct 62 ms 30748 KB Output is correct
46 Correct 49 ms 39516 KB Output is correct
47 Correct 144 ms 48312 KB Output is correct
48 Correct 194 ms 87380 KB Output is correct
49 Correct 181 ms 85396 KB Output is correct
50 Correct 153 ms 48204 KB Output is correct
51 Correct 157 ms 48204 KB Output is correct
52 Correct 184 ms 48448 KB Output is correct
53 Correct 164 ms 46928 KB Output is correct
54 Correct 269 ms 79876 KB Output is correct
55 Correct 11 ms 5976 KB Output is correct
56 Correct 6 ms 4700 KB Output is correct
57 Correct 51 ms 29260 KB Output is correct
58 Correct 43 ms 25224 KB Output is correct
59 Correct 62 ms 45904 KB Output is correct
60 Correct 178 ms 85840 KB Output is correct
61 Correct 176 ms 51716 KB Output is correct
62 Correct 147 ms 48332 KB Output is correct
63 Correct 186 ms 48444 KB Output is correct
64 Correct 345 ms 98640 KB Output is correct
65 Correct 331 ms 99264 KB Output is correct
66 Correct 194 ms 80828 KB Output is correct
67 Correct 140 ms 47160 KB Output is correct
68 Correct 278 ms 66348 KB Output is correct
69 Correct 252 ms 66900 KB Output is correct
70 Correct 250 ms 63576 KB Output is correct