이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#include "race.h"
using namespace std;
const int MXN = 1e5+5;
vector<pii> adj[MXN];
int n,k,sz[MXN];
bool vis[MXN],bad[MXN];
int calcsize(int v,int p) {
sz[v] = 1;
for (auto i : adj[v]) {
int to = i.fi;
if (!bad[to] && to != p) {
calcsize(to,v);
sz[v] += sz[to];
}
}
return sz[v];
}
pii MN;
int SZ;
void getcenter(int v,int p) {
int mxsz = 0,sum = 0;
for (auto i : adj[v]) {
int to = i.fi;
if (to == p || bad[to]) continue;
getcenter(to,v);
mxsz = max(mxsz,sz[to]);
sum += sz[to];
}
int adit = SZ - sum - 1;
mxsz = max(mxsz,adit);
MN = min(MN,make_pair(mxsz,v));
}
const int MAXK = 1e6+6;
int dis[MAXK + 5];
int ans = INT_MAX;
void get(int v,int p,int depth,int val) {
if (val > k) return;
if (dis[k-val] && dis[k - val] + depth - 1 < ans)
ans = dis[k-val] + depth - 1;
for (auto i : adj[v]) {
int to = i.fi;
if (p != to)
get(to,v,depth+1,val + i.se);
}
}
void upd(int v,int p,int depth,int val,bool flag) {
if (dis[val] == 0 || dis[val] > depth)
dis[val] = depth;
if (flag == 0) dis[val] = 0;
for (auto i : adj[v]) {
int to = i.fi;
if (p!= to)
upd(to,v,depth+1,val + i.se,flag);
}
}
void dfs(int v,int p,int depth,bool keep) {
int big = -1,mxsz =- 1,money = -1;
for (auto i : adj[v]) {
int to = i.fi;
if (to != p && mxsz < sz[to]) {
// mxsz = sz[to];
/// big = to;
// money = i.se;
}
}
/* if (big + 1) {
get(big,v,depth+1,money);
upd(big,v,depth+1,money,1);
}*/
for (auto i : adj[v]) {
int to = i.fi;
//if (i == p || i == big)
/// continue;
get(to,v,2,i.se);
upd(to,v,2,i.se,1);
}
for (auto i : adj[v]) {
int to = i.fi;
if (to != p)
upd(to,v,depth+1,i.se,0);
}
}
void split(int v){
bool ok = 0;
for (auto i : adj[v])
if (!bad[i.fi]) ok = 1;
if (ok == 0) return;
SZ = calcsize(v,-1);
MN = {INT_MAX,0};
getcenter(v,-1);
int center = MN.se;
calcsize(center,-1);
dis[0] = 1;
dfs(center,-1,1,0);
bad[center] = 1;
for (auto & i : adj[center])
if (bad[i.fi] == 0)
split(i.fi);
}
int best_path(int n1, int k1, int H[][2], int L[])
{
n = n1;
k = k1;
for (int i = 0; i< n ;i++) {
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
if (ans == INT_MAX) return -1;
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void dfs(int, int, int, bool)':
race.cpp:68:6: warning: unused variable 'big' [-Wunused-variable]
68 | int big = -1,mxsz =- 1,money = -1;
| ^~~
race.cpp:68:25: warning: unused variable 'money' [-Wunused-variable]
68 | int big = -1,mxsz =- 1,money = -1;
| ^~~~~
# | 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... |