이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//#undef LOCALKL
#define IO \
ios_base::sync_with_stdio(0);(cin).tie(0);(cout).tie(0)
#define y1 y1_
#define prev prev_
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#ifdef LOCALKL
// #define cerr cerr<<"\33[1;32m"
// #define cout cout<<"\33[0m"
#else
#ifndef OO
#define endl '\n'
#define cerr if(1){}else cerr
#endif
#endif
#define OK cerr<<"OK\n"<<endl;
#define setpre(k) fixed<<setprecision(k)
#define mmset(k,y) memset(k,y,sizeof(k))
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ull = unsigned long long;
using intt = long long;
using ll = long long;
using ld = long double;
const ll m9 = 998244353;
const ll m7 = 1000000007;
const ll m18 = 1000000000000000000;
const ll i127 = 2139062143;
const ll l127 = 9187201950435737471;
int n, k, ans = i127;
bool removed[200002];
vector<pii>g[200002];
map<ll,int>m;
void calc(int v, int parent, int dis, int depth)
{
if(m[k-dis] or dis==k) ans=min(ans, m[k-dis]+depth);
if(dis >= k) return;
for(pii u : g[v])
if(u.F!=parent and !removed[u.F])
calc(u.F, v, dis+u.S, depth+1);
}
void add_dis(int v, int parent, int dis, int depth)
{
m[dis] = depth;
if(dis >= k) return;
for(pii u : g[v])
if(u.F!=parent and !removed[u.F])
add_dis(u.F, v, dis+u.S, depth+1);
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
for(int i=0;i<n-1;i++)
{
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
for(int i=0;i<n;i++)
{
m.clear();
for(pii u : g[i])
if(!removed[u.F])
{
calc(u.F, i, u.S, 1);
add_dis(u.F, i, u.S, 1);
}
// cerr<<i<<' '<<ans<<endl;
}
return ans==i127?-1:ans;
}
# | 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... |