結果

問題 No.1212 Second Path
ユーザー blackyukiblackyuki
提出日時 2020-08-30 15:59:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 670 ms / 3,000 ms
コード長 4,804 bytes
コンパイル時間 3,412 ms
コンパイル使用メモリ 217,980 KB
実行使用メモリ 42,196 KB
最終ジャッジ日時 2023-08-09 14:40:46
合計ジャッジ時間 30,398 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 436 ms
42,116 KB
testcase_01 AC 572 ms
40,080 KB
testcase_02 AC 547 ms
37,644 KB
testcase_03 AC 522 ms
38,136 KB
testcase_04 AC 525 ms
33,668 KB
testcase_05 AC 506 ms
33,668 KB
testcase_06 AC 437 ms
23,312 KB
testcase_07 AC 631 ms
24,504 KB
testcase_08 AC 657 ms
24,476 KB
testcase_09 AC 667 ms
24,392 KB
testcase_10 AC 670 ms
24,892 KB
testcase_11 AC 644 ms
24,436 KB
testcase_12 AC 655 ms
24,432 KB
testcase_13 AC 647 ms
24,424 KB
testcase_14 AC 636 ms
24,488 KB
testcase_15 AC 631 ms
24,448 KB
testcase_16 AC 652 ms
24,472 KB
testcase_17 AC 434 ms
42,196 KB
testcase_18 AC 199 ms
4,380 KB
testcase_19 AC 194 ms
4,388 KB
testcase_20 AC 191 ms
4,380 KB
testcase_21 AC 185 ms
4,376 KB
testcase_22 AC 193 ms
4,380 KB
testcase_23 AC 156 ms
4,380 KB
testcase_24 AC 167 ms
4,380 KB
testcase_25 AC 164 ms
4,380 KB
testcase_26 AC 169 ms
4,380 KB
testcase_27 AC 166 ms
4,384 KB
testcase_28 AC 165 ms
4,384 KB
testcase_29 AC 494 ms
42,080 KB
testcase_30 AC 478 ms
42,112 KB
testcase_31 AC 480 ms
42,068 KB
testcase_32 AC 563 ms
19,412 KB
testcase_33 AC 507 ms
15,140 KB
testcase_34 AC 621 ms
23,176 KB
testcase_35 AC 340 ms
5,456 KB
testcase_36 AC 562 ms
20,212 KB
testcase_37 AC 554 ms
18,676 KB
testcase_38 AC 591 ms
19,372 KB
testcase_39 AC 456 ms
12,016 KB
testcase_40 AC 246 ms
4,384 KB
testcase_41 AC 564 ms
19,668 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 1 ms
4,384 KB
testcase_45 AC 440 ms
23,484 KB
testcase_46 AC 427 ms
23,352 KB
testcase_47 AC 428 ms
23,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
    }
}
0