#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout<<"\n"
#define all(v) v.begin(),v.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define forn(i,a,b) for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b) for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod 1'000'000'007
#define mod2 998'244'353
#define inf 1'000'000'000'000'007
#define pi 3.14159265358979323846
template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;
template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p) {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m) {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}
void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail>
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//---------------------------------mars4---------------------------------
vector<ll> sz;
vector<ll> dist;
vector<ll> depth;
vector<map<ll,ll>> g;
vector<vector<pair<ll,ll>>> v;
ll k;
ll ans=inf;
void dfs_sz(ll cur,ll prev,ll len,ll level)
{
sz[cur]++;
dist[cur]=len;
depth[cur]=level;
for(auto [i,w]:v[cur])
{
if(i!=prev)
{
dfs_sz(i,cur,len+w,level+1);
sz[cur]+=sz[i];
}
}
}
void dfs(ll cur,ll prev)
{
ll bsz=-1;
ll big=-1;
for(auto [i,w]:v[cur])
{
if(i!=prev)
{
if(sz[i]>bsz)
{
bsz=sz[i];
big=i;
}
}
}
for(auto [i,w]:v[cur])
{
if(i!=prev and i!=big)
{
dfs(i,cur);
}
}
if(big!=-1)
{
dfs(big,cur);
swap(g[cur],g[big]);
}
for(auto [i,w]:v[cur])
{
if(i!=prev and i!=big)
{
for(auto [len,d]:g[i])
{
ll req=k-len+2*dist[cur];
if(g[cur].count(req))
{
ans=min(ans,g[cur][req]+d-2*depth[cur]);
}
}
for(auto [len,d]:g[i])
{
if(g[cur].count(len))
{
g[cur][len]=min(g[cur][len],d);
}
else
{
g[cur][len]=d;
}
}
}
}
ll req=dist[cur]+k;
if(g[cur].count(req))
{
ans=min(ans,g[cur][req]-depth[cur]);
}
g[cur][dist[cur]]=depth[cur];
}
int best_path(int N,int K,int H[][2],int L[])
{
k=K;
sz=vector<ll>(N);
dist=vector<ll>(N);
depth=vector<ll>(N);
g=vector<map<ll,ll>>(N);
v=vector<vector<pair<ll,ll>>>(N);
ans=inf;
forn(i,0,N-1)
{
ll l=H[i][0];
ll r=H[i][1];
ll w=L[i];
v[l].push_back({r,w});
v[r].push_back({l,w});
}
dfs_sz(0,-1,0,0);
dfs(0,-1);
if(ans==inf)
{
ans=-1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
2 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
1 ms |
588 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
132 ms |
27132 KB |
Output is correct |
20 |
Correct |
131 ms |
27076 KB |
Output is correct |
21 |
Correct |
124 ms |
27036 KB |
Output is correct |
22 |
Correct |
120 ms |
27112 KB |
Output is correct |
23 |
Correct |
153 ms |
40844 KB |
Output is correct |
24 |
Correct |
122 ms |
32764 KB |
Output is correct |
25 |
Correct |
94 ms |
27776 KB |
Output is correct |
26 |
Correct |
55 ms |
37496 KB |
Output is correct |
27 |
Correct |
185 ms |
44868 KB |
Output is correct |
28 |
Correct |
299 ms |
87072 KB |
Output is correct |
29 |
Correct |
254 ms |
84688 KB |
Output is correct |
30 |
Correct |
185 ms |
44860 KB |
Output is correct |
31 |
Correct |
214 ms |
44856 KB |
Output is correct |
32 |
Correct |
241 ms |
44944 KB |
Output is correct |
33 |
Correct |
236 ms |
43912 KB |
Output is correct |
34 |
Correct |
338 ms |
75612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
2 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
1 ms |
588 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
588 KB |
Output is correct |
39 |
Correct |
132 ms |
27132 KB |
Output is correct |
40 |
Correct |
131 ms |
27076 KB |
Output is correct |
41 |
Correct |
124 ms |
27036 KB |
Output is correct |
42 |
Correct |
120 ms |
27112 KB |
Output is correct |
43 |
Correct |
153 ms |
40844 KB |
Output is correct |
44 |
Correct |
122 ms |
32764 KB |
Output is correct |
45 |
Correct |
94 ms |
27776 KB |
Output is correct |
46 |
Correct |
55 ms |
37496 KB |
Output is correct |
47 |
Correct |
185 ms |
44868 KB |
Output is correct |
48 |
Correct |
299 ms |
87072 KB |
Output is correct |
49 |
Correct |
254 ms |
84688 KB |
Output is correct |
50 |
Correct |
185 ms |
44860 KB |
Output is correct |
51 |
Correct |
214 ms |
44856 KB |
Output is correct |
52 |
Correct |
241 ms |
44944 KB |
Output is correct |
53 |
Correct |
236 ms |
43912 KB |
Output is correct |
54 |
Correct |
338 ms |
75612 KB |
Output is correct |
55 |
Correct |
15 ms |
4044 KB |
Output is correct |
56 |
Correct |
9 ms |
2720 KB |
Output is correct |
57 |
Correct |
85 ms |
26020 KB |
Output is correct |
58 |
Correct |
53 ms |
22836 KB |
Output is correct |
59 |
Correct |
82 ms |
45096 KB |
Output is correct |
60 |
Correct |
250 ms |
88408 KB |
Output is correct |
61 |
Correct |
214 ms |
51268 KB |
Output is correct |
62 |
Correct |
193 ms |
47792 KB |
Output is correct |
63 |
Correct |
241 ms |
47864 KB |
Output is correct |
64 |
Correct |
463 ms |
98120 KB |
Output is correct |
65 |
Correct |
422 ms |
98792 KB |
Output is correct |
66 |
Correct |
266 ms |
82676 KB |
Output is correct |
67 |
Correct |
182 ms |
46748 KB |
Output is correct |
68 |
Correct |
312 ms |
65816 KB |
Output is correct |
69 |
Correct |
324 ms |
66492 KB |
Output is correct |
70 |
Correct |
294 ms |
62912 KB |
Output is correct |