#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// template<class T> using Tree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("no-stack-protector")
// #define ll __int128
#define ll long long
// #define ll int
#define f(i,a,b) for(ll i=a;i<b;i++)
#define mod 1000000007
// #define mod 998244353
#define mp make_pair
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define ff first
#define ss second
#define rf(i,a,b) for(ll i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf printf
#define sz(a) (int)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
const double pi = acos(-1);
ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}
ll binpow(ll a, ll b, ll md){ll res=1;a%=md;if(a==0)return 0;while(b!=0){if(b&1)res*=a,res%=md;a*=a,a%=md;b>>=1;}return res%md;}
using namespace std;
const int N=21;
vector<vector<array<ll,2> > > v;
vector<ll> dis,val;
vector<map<ll,ll> > m;
int ans,k;
void dfs(ll cur, ll par)
{
f(i,0,sz(v[cur]))
{
ll node=v[cur][i][0],w=v[cur][i][1];
if(node!=par)
{
dis[node]=1+dis[cur],val[node]=val[cur]+w;
dfs(node,cur);
}
}
}
void update(ll num)
{
if(ans==-1)
ans=num;
else
ans=min(1LL*ans,1LL*num);
}
void merge(map<ll,ll> &a, map<ll,ll> &b, ll &s, ll &d)
{
if(sz(a)<sz(b))
a.swap(b);
for(auto it=b.begin();it!=b.end();it++)
{
ll sum=(it->ff),cdis=(it->ss);
ll rem=s-sum;
if(a.find(rem)!=a.end())
{
ll cdis2=a[rem],tot=cdis+cdis2-2*d;
update(tot);
}
}
for(auto it=b.begin();it!=b.end();it++)
{
ll sum=(it->ff),cdis=(it->ss);
if(a.find(sum)==a.end())
a[sum]=cdis;
else
a[sum]=min(a[sum],cdis);
}
b.clear();
}
void dfs2(int cur, int par)
{
int req=k+val[cur];
ll req2=k+2*val[cur];
f(i,0,sz(v[cur]))
{
ll node=v[cur][i][0];
if(node!=par)
{
dfs2(node,cur);
merge(m[cur],m[node],req2,dis[cur]);
}
}
if(m[cur].find(val[cur])!=m[cur].end())
m[cur][val[cur]]=min(m[cur][val[cur]],dis[cur]);
else
m[cur][val[cur]]=dis[cur];
if(m[cur].find(req)!=m[cur].end())
update(m[cur][req]-dis[cur]);
}
int best_path(int n, int K, int h[][2], int l[])
{
v.clear(),dis.clear(),m.clear(),val.clear();
v.rz(n+1),dis.rz(n+1),m.rz(n+1),val.rz(n+1);
k=K;
f(i,0,n-1)
{
ll x=h[i][0],y=h[i][1],w=l[i];
x++,y++;
v[x].pb({y,w}),v[y].pb({x,w});
}
dfs(1,1),ans=-1;
dfs2(1,1);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
113 ms |
15184 KB |
Output is correct |
20 |
Correct |
114 ms |
15312 KB |
Output is correct |
21 |
Correct |
102 ms |
15236 KB |
Output is correct |
22 |
Correct |
116 ms |
15284 KB |
Output is correct |
23 |
Correct |
131 ms |
15824 KB |
Output is correct |
24 |
Correct |
110 ms |
14884 KB |
Output is correct |
25 |
Correct |
118 ms |
22932 KB |
Output is correct |
26 |
Correct |
53 ms |
30124 KB |
Output is correct |
27 |
Correct |
203 ms |
33684 KB |
Output is correct |
28 |
Correct |
350 ms |
72972 KB |
Output is correct |
29 |
Correct |
292 ms |
71724 KB |
Output is correct |
30 |
Correct |
214 ms |
33708 KB |
Output is correct |
31 |
Correct |
187 ms |
33644 KB |
Output is correct |
32 |
Correct |
275 ms |
33944 KB |
Output is correct |
33 |
Correct |
215 ms |
32936 KB |
Output is correct |
34 |
Correct |
331 ms |
45884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
113 ms |
15184 KB |
Output is correct |
40 |
Correct |
114 ms |
15312 KB |
Output is correct |
41 |
Correct |
102 ms |
15236 KB |
Output is correct |
42 |
Correct |
116 ms |
15284 KB |
Output is correct |
43 |
Correct |
131 ms |
15824 KB |
Output is correct |
44 |
Correct |
110 ms |
14884 KB |
Output is correct |
45 |
Correct |
118 ms |
22932 KB |
Output is correct |
46 |
Correct |
53 ms |
30124 KB |
Output is correct |
47 |
Correct |
203 ms |
33684 KB |
Output is correct |
48 |
Correct |
350 ms |
72972 KB |
Output is correct |
49 |
Correct |
292 ms |
71724 KB |
Output is correct |
50 |
Correct |
214 ms |
33708 KB |
Output is correct |
51 |
Correct |
187 ms |
33644 KB |
Output is correct |
52 |
Correct |
275 ms |
33944 KB |
Output is correct |
53 |
Correct |
215 ms |
32936 KB |
Output is correct |
54 |
Correct |
331 ms |
45884 KB |
Output is correct |
55 |
Correct |
17 ms |
2388 KB |
Output is correct |
56 |
Correct |
9 ms |
1944 KB |
Output is correct |
57 |
Correct |
57 ms |
16824 KB |
Output is correct |
58 |
Correct |
50 ms |
15760 KB |
Output is correct |
59 |
Correct |
88 ms |
36588 KB |
Output is correct |
60 |
Correct |
329 ms |
71936 KB |
Output is correct |
61 |
Correct |
223 ms |
35520 KB |
Output is correct |
62 |
Correct |
173 ms |
33656 KB |
Output is correct |
63 |
Correct |
257 ms |
33804 KB |
Output is correct |
64 |
Correct |
455 ms |
42080 KB |
Output is correct |
65 |
Correct |
481 ms |
46112 KB |
Output is correct |
66 |
Correct |
290 ms |
69524 KB |
Output is correct |
67 |
Correct |
151 ms |
32676 KB |
Output is correct |
68 |
Correct |
395 ms |
43928 KB |
Output is correct |
69 |
Correct |
368 ms |
47952 KB |
Output is correct |
70 |
Correct |
315 ms |
41964 KB |
Output is correct |