이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* Author: Anil Byar
**/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template<class T>
// using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define ft first
#define sd second
#define pb push_back
// Source: https://codeforces.com/blog/entry/68809
// { start
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// } end
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vl> vvl;
#define dbg if(0)
const ll MOD = 1e9+7;
const ll MOD9 = 998244353;
const ll INFLL = 1e18+5;
const int INF = 1e9;
const int maxN = 2e5+5;
const int maxK = 1e6+5;
void printbit(int x, int len) {string s="\n";while(len--){s=((x%2)?'1':'0')+s;x/=2;} cout<<s;}
template<class T>
istream& operator>>(istream&in, vector<T>&v){
for (T& x:v) in>>x;
return in;
}
template<class T>
ostream& operator<<(ostream&out, vector<T>&v){
for (T& x:v) out<<x<<' ';
cout<<'\n';
return out;
}
int mnhighways[maxK];
int ans, k, n;
struct Edge{
int v, w;
bool operator==(Edge e){
return v==e.v;
}
};
struct Centroid{
std::vector<std::vector<Edge>> adj;
std::vector<int> parent, stree;
const long long INF = 1e9;
// nearest red
std::vector<int> nearest;
Centroid(std::vector<std::vector<Edge>>& _adj):adj(_adj){
parent.resize((int)_adj.size());
stree.resize((int)_adj.size());
nearest.resize((int)_adj.size(), INF);
}
void build(int node = 1, int par = -1){
int n = dfssize(node, par);
int centroid = dfscentroid(node, par, n);
parent[centroid] = par;
auto tmp = adj[centroid];
adj[centroid].clear();
for (Edge y:tmp) {
int x = y.v, w = y.w;
Edge tmp = {centroid, -1};
auto it = find(all(adj[x]), tmp);
// debug(*it);
adj[x].erase(find(all(adj[x]), tmp));
work(x, centroid, w, 1);
build(x, centroid);
}
}
int dfssize(int node, int par){
stree[node] = 1;
for (Edge y:adj[node]){
int x = y.v;
if (x==par) continue;
dfssize(x, node);
stree[node] += stree[x];
}
return stree[node];
}
int dfscentroid(int node, int par, int n){
for (Edge y:adj[node]){
int x = y.v;
if (x==par) continue;
if (stree[x]>n/2) return dfscentroid(x, node, n);
}
return node;
}
void work(int node, int par, int h, int dep){
if (h>k) return;
ans = min(ans, dep + mnhighways[k-h]);
for (Edge x:adj[node]){
if (x.v==par) continue;
work(x.v, node, h+x.w, dep+1);
}
mnhighways[h] = min(mnhighways[h], dep);
}
};
int best_path(int N,int K, int H[][2],int L[]){
vector<vector<Edge>> tadj(N);
for (int i = 0;i<N-1;i++){
int u = H[i][0], v = H[i][1], w = L[i];
tadj[u].pb({v, w});
tadj[v].pb({u, w});
}
k = K;
ans = INF;
memset(mnhighways, 127, sizeof(mnhighways));
mnhighways[0] = 0;
Centroid ctr(tadj);
ctr.build(0, -1);
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In member function 'void Centroid::build(int, int)':
race.cpp:121:18: warning: variable 'it' set but not used [-Wunused-but-set-variable]
121 | auto it = find(all(adj[x]), tmp);
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |