이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include <cmath>
#include <complex>
#include<string>
#include <ext/pb_ds/assoc_container.hpp>
//#include "testlib.h"
//freopen("orxor.in", "r", stdin);
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define pb push_back
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define dcm(a) setprecision(a)<<fixed
ll const md=1e9+7;
double const EPS=1e-9;
int const LOG=22;
using uint = unsigned int;
using ull = unsigned long long;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define clr(memo, a) memset(memo,a,sizeof(memo))
#define PI acos(-1)
#define endl '\n'
int const N=2e5+5;
vector<pair<int,ll> > adj[N];
int dep[N];
map<int,ll> mp[N];
ll sum[N];
ll ans=1e18;
ll k;
void dfs(int v,int p,int d,ll s)
{
sum[v]=s;
dep[v]=d;
mp[v][s]=d;
ll targ=k+2*s;
for(auto[u,w]:adj[v]){
if(u==p)
continue;
dfs(u,v,d+1,s+w);
if(mp[u].size()>mp[v].size())
swap(mp[u],mp[v]);
for(auto[ss,dist]:mp[u]){
if(mp[v].count(targ-ss)){
ans=min(ans,mp[v][targ-ss]+dist-2*d);
}
}
for(auto[ss,dist]:mp[u]){
if(mp[v].count(ss)){
mp[v][ss]=min(mp[v][ss],dist);
}
else{
mp[v][ss]=dist;
}
}
}
}
ll best_path(int n, int K, int edges[][2], int weights[]) {
if (K == 1) { return 0ll; }
k=K;
ans = 1e18;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].push_back({v,weights[i]});
adj[v].push_back({u,weights[i]});
}
dfs(0,-1,0,0);
return ans == 1e18 ? -1ll : ans;
}
void solve(int testCase)
{
int n;
cin>>n;
cin>>k;
for(int i=0;i<n-1;i++){
int x,y,w;
cin>>x>>y>>w;
adj[x].push_back({y,w});
adj[y].push_back({x,w});
}
dfs(0,-1,0,0);
cout<<ans<<endl;
}
# | 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... |