結果
問題 | No.1212 Second Path |
ユーザー | blackyuki |
提出日時 | 2020-08-30 15:59:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 660 ms / 3,000 ms |
コード長 | 4,804 bytes |
コンパイル時間 | 3,262 ms |
コンパイル使用メモリ | 220,488 KB |
実行使用メモリ | 42,372 KB |
最終ジャッジ日時 | 2024-11-15 10:33:13 |
合計ジャッジ時間 | 27,987 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 405 ms
42,240 KB |
testcase_01 | AC | 521 ms
40,192 KB |
testcase_02 | AC | 533 ms
37,632 KB |
testcase_03 | AC | 524 ms
38,144 KB |
testcase_04 | AC | 527 ms
33,824 KB |
testcase_05 | AC | 518 ms
33,920 KB |
testcase_06 | AC | 432 ms
23,676 KB |
testcase_07 | AC | 649 ms
24,704 KB |
testcase_08 | AC | 640 ms
24,840 KB |
testcase_09 | AC | 620 ms
24,704 KB |
testcase_10 | AC | 645 ms
24,704 KB |
testcase_11 | AC | 656 ms
24,960 KB |
testcase_12 | AC | 649 ms
24,704 KB |
testcase_13 | AC | 638 ms
24,704 KB |
testcase_14 | AC | 660 ms
24,832 KB |
testcase_15 | AC | 659 ms
24,704 KB |
testcase_16 | AC | 660 ms
24,832 KB |
testcase_17 | AC | 407 ms
42,240 KB |
testcase_18 | AC | 186 ms
5,248 KB |
testcase_19 | AC | 191 ms
5,248 KB |
testcase_20 | AC | 190 ms
5,248 KB |
testcase_21 | AC | 175 ms
5,248 KB |
testcase_22 | AC | 190 ms
5,248 KB |
testcase_23 | AC | 159 ms
5,248 KB |
testcase_24 | AC | 162 ms
5,248 KB |
testcase_25 | AC | 161 ms
5,248 KB |
testcase_26 | AC | 171 ms
5,248 KB |
testcase_27 | AC | 159 ms
5,248 KB |
testcase_28 | AC | 159 ms
5,248 KB |
testcase_29 | AC | 480 ms
42,372 KB |
testcase_30 | AC | 474 ms
42,240 KB |
testcase_31 | AC | 482 ms
42,240 KB |
testcase_32 | AC | 565 ms
19,712 KB |
testcase_33 | AC | 502 ms
15,360 KB |
testcase_34 | AC | 632 ms
23,424 KB |
testcase_35 | AC | 329 ms
5,248 KB |
testcase_36 | AC | 585 ms
20,352 KB |
testcase_37 | AC | 558 ms
18,816 KB |
testcase_38 | AC | 585 ms
19,584 KB |
testcase_39 | AC | 446 ms
12,288 KB |
testcase_40 | AC | 245 ms
5,248 KB |
testcase_41 | AC | 565 ms
19,712 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 2 ms
5,248 KB |
testcase_45 | AC | 432 ms
23,540 KB |
testcase_46 | AC | 428 ms
23,672 KB |
testcase_47 | AC | 430 ms
23,672 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb emplace_back #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; typedef multiset<ll> S; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} vvp g; vi sz,par,id,head,dep,mi,m,M; vvp aa; ll num; ll dfs(int i,int p){ if(g[i][0].fi==p)swap(g[i][0],g[i].back()); for(auto&to:g[i]){ if(to.fi==p)continue; sz[i]+=dfs(to.fi,i); if(sz[to.fi]>sz[g[i][0].fi])swap(to,g[i][0]); } return sz[i]; } void dfs1(int i,int p){ par[i]=p;id[i]=num++; vi v; for(auto to:g[i]){ if(to.fi==p)continue; v.pb(to.se); aa[i].pb(to.se,to.fi); chmin(mi[i],to.se); if(to==g[i][0])head[to.fi]=head[i]; dep[to.fi]=dep[i]+to.se; M[to.fi]=to.se; dfs1(to.fi,i); } sort(all(v)); sort(all(aa[i])); if(v.size()>1){ for(auto to:g[i])if(to.fi!=p){ if(v[0]==to.se)m[id[to.fi]]=v[1]; else m[id[to.fi]]=v[0]; } } } ll lca(ll a,ll b){ while(head[a]!=head[b]){ if(dep[head[a]]>dep[head[b]])a=par[head[a]]; else b=par[head[b]]; } if(dep[a]<dep[b])return a; return b; } vi seg; ll N; void initseg(vi v){ N=1; while(N<v.size())N*=2; seg=vi(N*2-1,inf); rep(i,v.size())seg[i+N-1]=v[i]; for(int i=N-2;i>=0;i--)seg[i]=min(seg[i*2+1],seg[i*2+2]); } ll getmi(ll a,ll b,ll k,ll l,ll r){ if(r<=a||b<=l)return inf; if(a<=l&&r<=b)return seg[k]; ll c1=getmi(a,b,k*2+1,l,(l+r)/2); ll c2=getmi(a,b,k*2+2,(l+r)/2,r); return min(c1,c2); } ll do_edge(int a,int b){ ll res=inf; while(head[a]!=head[b]){ if(par[head[b]]!=a)chmin(res,getmi(id[head[b]],id[b]+1,0,0,N)); else chmin(res,getmi(id[head[b]]+1,id[b]+1,0,0,N)); b=par[head[b]]; } chmin(res,getmi(id[a]+2,id[b]+1,0,0,N)); return res; } void init(ll n){ sz=vi(n,1);par=vi(n);id=vi(n);head=vi(n);dep=vi(n);mi=vi(n,inf);m=vi(n,inf);M=vi(n,inf); aa=vvp(n); rep(i,n)head[i]=i; num=0;dfs(0,-1);dfs1(0,-1); initseg(m); } int main(){ ll n;cin>>n; g=vvp(n); rep(i,n-1){ ll a,b,c;cin>>a>>b>>c;a--;b--; g[a].pb(b,c); g[b].pb(a,c); } init(n); ll q;cin>>q; rep(qq,q){ ll a,b;cin>>a>>b;a--;b--; ll l=lca(a,b); ll ans=min(do_edge(l,a),do_edge(l,b)); if(a!=l)chmin(ans,mi[a]); if(b!=l)chmin(ans,mi[b]); chmin(ans,M[l]); for(auto p:aa[l]){ if(lca(p.se,a)==l&&lca(p.se,b)==l){ chmin(ans,p.fi);break; } } if(ans!=inf)out(dep[a]+dep[b]-dep[l]*2+ans*2); else out(-1); } }