Submission #480190

#TimeUsernameProblemLanguageResultExecution timeMemory
480190Sam_a17Race (IOI11_race)C++17
43 / 100
3088 ms38784 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <cstdio>
#include "race.h"
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(T v[],T n);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

#include <ext/rope>
using namespace __gnu_cxx;

void fastIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
}
void setIO(string str) {
    fastIO();

    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
}
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int M = 2e5 + 10;
vector< pair<int, int> > adj[M], mas;
unordered_map<int, int> mp;
int ans = -1, vis[M], sz[M], compSize, k, n;

int dfs(int node, int parent = 0) {
    int csz = 1; compSize++;
    for(auto i: adj[node]) {
        if (i.first ^ parent && !vis[i.first]) {
            csz += dfs(i.first, node);
        }
    }
    sz[node] = csz;
    return csz;
}

int find_center(int node, int parent = 0) {
    int mxSz = 0, nd;
    for(auto i: adj[node]) {
        if (i.first ^ parent && !vis[i.first]) {
            if(sz[i.first] > mxSz) {
                mxSz = sz[i.first];
                nd = i.first;
            }
        }
    }

    if(mxSz * 2 <= compSize) {
        return node;
    }
    return find_center(nd, node);
}

int centroid(int node) {
    compSize = 0;
    dfs(node);
    int center = find_center(node);
    dfs(center);
    return center;
}


void solve(int node, int parent = 0, int l = 0, int d = 0) {

    if(parent) {
        mas.push_back({l, d});
    }

    for(auto i: adj[node]) {
        if(i.first ^ parent && !vis[i.first]) {
             solve(i.first, node, l + i.second, d + 1);

             if(!parent) {
                for(auto j: mas) {
                    int L = j.first, D = j.second;
                    if(L > k) {
                        continue;
                    } else if(L == k) {
                        if(ans == -1 || ans > D) {
                            ans = D;
                        }
                    } else {
                        if(mp.find(k - L) != mp.end()) {
                            int answ = D + mp[k - L];
                            if(ans == -1 || ans > answ) {
                                ans = answ;
                            }
                        }
                    }

                }

                for(auto j: mas) {
                    int L = j.first, D = j.second;
                    if(L > k) {
                        continue;
                    }
                    if(mp.find(L) != mp.end()) {
                        mp[L] = min(mp[L], D);
                    } else {
                        mp[L] = D;
                    }
                }

                mas.clear();
             }
        }
    }

}

void centroid_decomp() {
    queue<int> q;
    q.push(1);

    while(!q.empty()) {
        auto u = q.front();
        q.pop();

        int center = centroid(u);
//        dbg(center)
        mp.clear();
        solve(center);

        vis[center] = 1;
        for(auto i: adj[center]) {
            if(!vis[i.first] && sz[i.first] != 1) {
                q.push(i.first);
            }
        }
    }

}

int best_path(int N, int K, int H[][2], int L[]) {
    k = K, n = N;

    for(int i = 0; i < N - 1; i++) {
        H[i][0]++, H[i][1]++;
        adj[ H[i][0] ].push_back({H[i][1], L[i]});
        adj[ H[i][1] ].push_back({H[i][0], L[i]});
    }

    centroid_decomp();

    return ans;
}

//int main() {
//    cin >> n >> k;
//    for(int i = 0; i < n - 1; i++) {
//        int a, b, c; cin >> a >> b >> c, a++, b++;
//        adj[ a ].push_back({b, c});
//        adj[ b ].push_back({a, c});
//    }
//
//    centroid_decomp();
//
//    cout << ans << endl;
//    return 0;
//}

Compilation message (stderr)

race.cpp: In function 'void setIO(std::string)':
race.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int find_center(int, int)':
race.cpp:69:19: warning: 'nd' may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |     int mxSz = 0, nd;
      |                   ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...