# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
147713 |
2019-08-30T13:15:02 Z |
MvC |
Valley (BOI19_valley) |
C++11 |
|
411 ms |
77304 KB |
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
//#include "boxes.h"
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e5+50;
const int mod=1e9+7;
using namespace std;
int n,s,q,e,x,y,ex[nmax],ey[nmax],par[nmax][20],fin[nmax],fout[nmax],b[nmax],i,tin,v,l;
ll w,ew[nmax],h[nmax],w1,w2,up[nmax],dw[nmax],u[nmax][20],d[nmax][20],w3,vl[nmax],vd[nmax],vu[nmax];
vector<pair<int,ll> >a[nmax];
vector<pair<ll,int> >vc[nmax],vec[nmax];
void dfsdw(int x,int p)
{
if(b[x])vc[x].pb(mkp(0,x));
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i].fr;
if(y==p)continue;
dfsdw(y,x);
if(vc[y].empty())continue;
vc[x].pb(mkp(vc[y][0].fr+a[x][i].sc,y));
sort(vc[x].begin(),vc[x].end());
if(vc[x].size()==3)vc[x].pop_back();
}
vec[x]=vc[x];
}
void dfsup(int x,int p,ll z)
{
up[x]=dw[x]=llinf;
if(!vc[x].empty())dw[x]=vc[x][0].fr;
if(b[x])up[x]=0;
if(x!=p && up[x])
{
for(int i=0;i<vc[p].size();i++)
{
int y=vc[p][i].sc;
if(y!=x)
{
up[x]=vc[p][i].fr+z;
vc[x].pb(mkp(up[x],p));
sort(vc[x].begin(),vc[x].end());
if(vc[x].size()==3)vc[x].pop_back();
break;
}
}
}
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i].fr;
if(y==p)continue;
dfsup(y,x,a[x][i].sc);
}
}
void build(int x,int p,ll z)
{
vl[x]=z;
par[x][0]=p;
h[x]=h[p]+z;
d[x][0]=u[x][0]=vd[x]=vu[x]=llinf;
if(dw[x]!=llinf)vd[x]=dw[x]-h[x];
if(dw[p]!=llinf)d[x][0]=dw[p]-h[p];
if(up[x]!=llinf)
{
if(up[x])vu[x]=up[x]-vl[x]+h[x];
else vu[x]=up[x]+h[x];
}
if(up[p]!=llinf)
{
if(up[p])u[x][0]=up[p]-vl[p]+h[p];
else u[x][0]=up[p]+h[p];
}
fin[x]=++tin;
for(int i=1;i<18;i++)
{
par[x][i]=par[par[x][i-1]][i-1];
d[x][i]=u[x][i]=llinf;
d[x][i]=min(d[x][i-1],d[par[x][i-1]][i-1]);
u[x][i]=min(u[x][i-1],u[par[x][i-1]][i-1]);
}
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i].fr;
if(y==p)continue;
build(y,x,a[x][i].sc);
}
fout[x]=++tin;
}
int anc(int x,int y)
{
return fin[x]<=fin[y] && fout[y]<=fout[x];
}
ll fnd(int x,int y,int bl)
{
ll rs=llinf;
rs=min(rs,vd[x]);
for(int i=17;i>=0;i--)
{
if(!anc(par[x][i],y))
{
rs=min(rs,d[x][i]);
x=par[x][i];
rs=min(rs,vd[x]);
}
}
if(bl && x!=y)
{
rs=min(rs,d[x][0]);
}
return rs;
}
ll fnu(int x,int y)
{
ll rs=llinf;
rs=min(rs,vu[x]);
for(int i=17;i>=0;i--)
{
if(!anc(par[x][i],y))
{
rs=min(rs,u[x][i]);
x=par[x][i];
rs=min(rs,vu[x]);
}
}
return rs;
}
int lca(int x,int y)
{
if(anc(x,y))return x;
if(anc(y,x))return y;
for(int i=17;i>=0;i--)if(!anc(par[x][i],y))x=par[x][i];
return par[x][0];
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>s>>q>>e;
for(i=1;i<n;i++)
{
cin>>x>>y>>w;
a[x].pb(mkp(y,w));
a[y].pb(mkp(x,w));
ex[i]=x,ey[i]=y,ew[i]=w;
}
for(i=1;i<=s;i++)
{
cin>>x;
b[x]=1;
}
dfsdw(e,e);
dfsup(e,e,0);
build(e,e,0);
while(q--)
{
cin>>x>>y;
if(h[ex[x]]>h[ey[x]])swap(ex[x],ey[x]);
v=ex[x];
x=ey[x];
if(!anc(x,y))cout<<"escaped\n";
else
{
w=fnd(y,x,1);
if(w!=llinf)w+=h[y];
if(w==llinf)cout<<"oo\n";
else cout<<w<<'\n';
}
}
return 0;
}
Compilation message
valley.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
valley.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
valley.cpp: In function 'void dfsdw(int, int)':
valley.cpp:29:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
valley.cpp: In function 'void dfsup(int, int, ll)':
valley.cpp:48:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<vc[p].size();i++)
~^~~~~~~~~~~~~
valley.cpp:61:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
valley.cpp: In function 'void build(int, int, ll)':
valley.cpp:94:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7544 KB |
Output is correct |
2 |
Correct |
11 ms |
7672 KB |
Output is correct |
3 |
Correct |
12 ms |
7672 KB |
Output is correct |
4 |
Correct |
12 ms |
7672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7544 KB |
Output is correct |
2 |
Correct |
11 ms |
7672 KB |
Output is correct |
3 |
Correct |
12 ms |
7672 KB |
Output is correct |
4 |
Correct |
12 ms |
7672 KB |
Output is correct |
5 |
Correct |
11 ms |
8184 KB |
Output is correct |
6 |
Correct |
11 ms |
8124 KB |
Output is correct |
7 |
Correct |
11 ms |
8056 KB |
Output is correct |
8 |
Correct |
10 ms |
8056 KB |
Output is correct |
9 |
Correct |
11 ms |
8104 KB |
Output is correct |
10 |
Correct |
11 ms |
8056 KB |
Output is correct |
11 |
Correct |
11 ms |
8056 KB |
Output is correct |
12 |
Correct |
10 ms |
8056 KB |
Output is correct |
13 |
Correct |
11 ms |
8156 KB |
Output is correct |
14 |
Correct |
10 ms |
8056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
67028 KB |
Output is correct |
2 |
Correct |
320 ms |
68252 KB |
Output is correct |
3 |
Correct |
351 ms |
68728 KB |
Output is correct |
4 |
Correct |
408 ms |
71544 KB |
Output is correct |
5 |
Correct |
365 ms |
71332 KB |
Output is correct |
6 |
Correct |
411 ms |
75128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7544 KB |
Output is correct |
2 |
Correct |
11 ms |
7672 KB |
Output is correct |
3 |
Correct |
12 ms |
7672 KB |
Output is correct |
4 |
Correct |
12 ms |
7672 KB |
Output is correct |
5 |
Correct |
11 ms |
8184 KB |
Output is correct |
6 |
Correct |
11 ms |
8124 KB |
Output is correct |
7 |
Correct |
11 ms |
8056 KB |
Output is correct |
8 |
Correct |
10 ms |
8056 KB |
Output is correct |
9 |
Correct |
11 ms |
8104 KB |
Output is correct |
10 |
Correct |
11 ms |
8056 KB |
Output is correct |
11 |
Correct |
11 ms |
8056 KB |
Output is correct |
12 |
Correct |
10 ms |
8056 KB |
Output is correct |
13 |
Correct |
11 ms |
8156 KB |
Output is correct |
14 |
Correct |
10 ms |
8056 KB |
Output is correct |
15 |
Correct |
290 ms |
67028 KB |
Output is correct |
16 |
Correct |
320 ms |
68252 KB |
Output is correct |
17 |
Correct |
351 ms |
68728 KB |
Output is correct |
18 |
Correct |
408 ms |
71544 KB |
Output is correct |
19 |
Correct |
365 ms |
71332 KB |
Output is correct |
20 |
Correct |
411 ms |
75128 KB |
Output is correct |
21 |
Correct |
261 ms |
65884 KB |
Output is correct |
22 |
Correct |
290 ms |
65648 KB |
Output is correct |
23 |
Correct |
309 ms |
65732 KB |
Output is correct |
24 |
Correct |
341 ms |
68600 KB |
Output is correct |
25 |
Correct |
361 ms |
72916 KB |
Output is correct |
26 |
Correct |
343 ms |
66200 KB |
Output is correct |
27 |
Correct |
314 ms |
66044 KB |
Output is correct |
28 |
Correct |
294 ms |
66140 KB |
Output is correct |
29 |
Correct |
357 ms |
69220 KB |
Output is correct |
30 |
Correct |
386 ms |
74364 KB |
Output is correct |
31 |
Correct |
263 ms |
67052 KB |
Output is correct |
32 |
Correct |
295 ms |
67536 KB |
Output is correct |
33 |
Correct |
306 ms |
68416 KB |
Output is correct |
34 |
Correct |
365 ms |
71544 KB |
Output is correct |
35 |
Correct |
374 ms |
77304 KB |
Output is correct |
36 |
Correct |
351 ms |
72544 KB |
Output is correct |