제출 #480855

#제출 시각아이디문제언어결과실행 시간메모리
480855Sam_a17경주 (Race) (IOI11_race)C++14
100 / 100
1177 ms40788 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include "race.h"
#include <cstdio>
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(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'

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5
void fastIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str) {
    fastIO();

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

const int N = 2e5 + 10;

int n, k, sz[N], compSize, answ = -1;
vector< pair<int, int> > adj[N], mas;
map<int, int> mp;
bool used[N];

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

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

    if(mxSz * 2 <= compSize) {
        return node;
    } else {
        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(!used[i.first] && i.first ^ parent) {
            solve(i.first, node, l + i.second, d + 1);

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

                // upd map

                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_decomposition () {
    queue <int> q;
    q.push(1);

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

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

        used[center] = 1;
        for(auto i: adj[center]) {
            if(!used[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_decomposition();

    return answ;
}

//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_decomposition();
//
//    cout << answ << endl;
//    return 0;
//}

컴파일 시 표준 에러 (stderr) 메시지

race.cpp: In function 'void setIO(std::string)':
race.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:46:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |     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...