結果

問題 No.1600 Many Shortest Path Problems
ユーザー chocoruskchocorusk
提出日時 2021-07-15 01:33:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,332 ms / 4,000 ms
コード長 5,962 bytes
コンパイル時間 5,856 ms
コンパイル使用メモリ 206,636 KB
実行使用メモリ 90,200 KB
最終ジャッジ日時 2023-09-17 04:13:33
合計ジャッジ時間 37,861 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
24,356 KB
testcase_01 AC 8 ms
24,264 KB
testcase_02 AC 8 ms
24,032 KB
testcase_03 AC 8 ms
24,136 KB
testcase_04 AC 1,310 ms
84,604 KB
testcase_05 AC 1,329 ms
84,628 KB
testcase_06 AC 8 ms
24,196 KB
testcase_07 AC 7 ms
24,036 KB
testcase_08 AC 8 ms
24,104 KB
testcase_09 AC 8 ms
24,152 KB
testcase_10 AC 534 ms
29,480 KB
testcase_11 AC 637 ms
32,300 KB
testcase_12 AC 862 ms
46,852 KB
testcase_13 AC 1,201 ms
71,428 KB
testcase_14 AC 1,325 ms
84,688 KB
testcase_15 AC 8 ms
24,040 KB
testcase_16 AC 7 ms
24,104 KB
testcase_17 AC 1,026 ms
58,808 KB
testcase_18 AC 1,324 ms
84,744 KB
testcase_19 AC 8 ms
24,220 KB
testcase_20 AC 7 ms
24,092 KB
testcase_21 AC 1,038 ms
58,792 KB
testcase_22 AC 7 ms
24,272 KB
testcase_23 AC 8 ms
24,092 KB
testcase_24 AC 1,332 ms
84,704 KB
testcase_25 AC 7 ms
24,280 KB
testcase_26 AC 8 ms
24,148 KB
testcase_27 AC 8 ms
24,212 KB
testcase_28 AC 8 ms
24,048 KB
testcase_29 AC 920 ms
61,988 KB
testcase_30 AC 932 ms
61,136 KB
testcase_31 AC 1,000 ms
58,656 KB
testcase_32 AC 930 ms
58,652 KB
testcase_33 AC 8 ms
24,092 KB
testcase_34 AC 7 ms
24,408 KB
testcase_35 AC 908 ms
85,028 KB
testcase_36 AC 823 ms
90,200 KB
testcase_37 AC 7 ms
24,272 KB
testcase_38 AC 905 ms
61,116 KB
testcase_39 AC 8 ms
24,172 KB
testcase_40 AC 951 ms
61,664 KB
testcase_41 AC 711 ms
61,880 KB
testcase_42 AC 728 ms
61,216 KB
testcase_43 AC 723 ms
61,116 KB
testcase_44 AC 743 ms
60,532 KB
testcase_45 AC 883 ms
61,760 KB
testcase_46 AC 917 ms
60,940 KB
testcase_47 AC 925 ms
61,076 KB
testcase_48 AC 925 ms
61,076 KB
testcase_49 AC 8 ms
24,088 KB
testcase_50 AC 8 ms
24,076 KB
testcase_51 AC 8 ms
24,040 KB
testcase_52 AC 7 ms
24,092 KB
testcase_53 AC 8 ms
24,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct LCA{
    vector<vector<int>> g;
    vector<int> d;
    vector<vector<int>> p;
    int log;
    int n;
    LCA(const vector<vector<int>> &g):n(g.size()), g(g), d(g.size()){
        log=0;
        while(1<<log<=n) log++;
        p.resize(log, vector<int>(n));
    }
    void dfs(int x, int prev){
        for(auto y:g[x]){
            if(y==prev) continue;
            d[y]=d[x]+1;
            p[0][y]=x;
            dfs(y, x);
        }
    }
    void build(){
        dfs(0, -1);
        for(int i=1; i<log; i++){
            for(int j=0; j<n; j++){
                p[i][j]=p[i-1][p[i-1][j]];
            }
        }
    }
    int lca(int a, int b){
        if(d[a]>d[b]) swap(a, b);
        int dd=d[b]-d[a], i=0;
        int a1=a, b1=b;
        while(dd){
            if(dd&1) b1=p[i][b1];
            dd>>=1;
            i++;
        }
        if(a1==b1) return a1;
        for(int j=log-1; j>=0; j--){
            if(p[j][a1]!=p[j][b1]){
                a1=p[j][a1], b1=p[j][b1];
            }
        }
        return p[0][a1];
    }
    int dist(int a, int b){
        return d[a]+d[b]-2*d[lca(a, b)];
    }
};
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
template<typename Monoid>
struct SegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	int sz;
	vector<Monoid> seg;
	const F f;
	const Monoid e;

	SegmentTree(int n, const F f, const Monoid &e): f(f), e(e){
		sz=1;
		while(sz<n) sz<<=1;
		seg.resize(2*sz, e);
	}

	SegmentTree(int n, const F f, const Monoid &e, vector<Monoid> v): f(f), e(e){
		sz=1;
		while(sz<n) sz<<=1;
		seg.resize(2*sz, e);
		for(int i=0; i<n; i++) seg[i+sz]=v[i];
		for(int i=sz-1; i>=1; i--){
			seg[i]=f(seg[2*i], seg[2*i+1]);
		}
	}

	void update(int k, const Monoid &x){
		k+=sz;
		seg[k]=x;
		while(k>1){
			k>>=1;
			seg[k]=f(seg[2*k], seg[2*k+1]);
		}
	}

	Monoid query(int a, int b){
		a+=sz, b+=sz;
		Monoid ret=e;
		for(;a<b; a>>=1, b>>=1){
			if(b&1) ret=f(ret, seg[--b]);
			if(a&1) ret=f(ret, seg[a++]);
		}
		return ret;
	}

	/*Monoid query(int a, int b){ //演算が非可換のとき
		a+=sz, b+=sz;
		Monoid retl=e, retr=e;
		for(;a<b; a>>=1, b>>=1){
			if(b&1) retr=f(seg[--b], retr);
			if(a&1) retl=f(retl, seg[a++]);
		}
		return f(retl, retr);
	}*/

	Monoid operator[](const int &k) const{
		return seg[k+sz];
	}
};
int n, m;
int a[200020], b[200020];
int q;
int x[200020], y[200020], z[200020];
vector<vector<int>> g;
vector<vector<int>> ge;
vector<int> e;
using mint=modint1000000007;
mint d[200020];
int d0[200020];
mint p2[200020];
//mint ans[200020];
int l[200020], r[200020], rev[200020];
vector<int> v1[200020], v2[200020], vr[200020];
int mnl[200020], mnr[200020];
bool t[200020];
int main()
{
	cin>>n>>m;
	g.resize(n);
	ge.resize(n);
	for(int i=0; i<m; i++){
		cin>>a[i]>>b[i];
		a[i]--; b[i]--;
	}
	unionfind uf(n);
	for(int i=0; i<m; i++){
		if(uf.same(a[i], b[i])){
			e.push_back(i);
		}else{
			g[a[i]].push_back(b[i]);
			g[b[i]].push_back(a[i]);
			ge[a[i]].push_back(i);
			ge[b[i]].push_back(i);
			uf.unite(a[i], b[i]);
			t[i]=1;
		}
	}
	p2[0]=1;
	for(int i=0; i<m; i++) p2[i+1]=p2[i]*2;
	LCA lca(g);
	lca.build();
	int ord=0;
	auto dfs=[&](auto dfs, int x, int p)->void{
		l[x]=ord++;
		rev[l[x]]=x;
		for(int i=0; i<g[x].size(); i++){
			int y=g[x][i];
			if(y==p) continue;
			int j=ge[x][i];
			if(y==a[j]) swap(a[j], b[j]);
			d[y]=d[x]+p2[j+1];
			//d0[y]=d0[x]+1;
			dfs(dfs, y, x);
		}
		r[x]=ord;
		vr[r[x]].push_back(x);
	};
	dfs(dfs, 0, -1);
	const int INF=1e9;
	SegmentTree<int> seg(n, [](int x, int y){ return min(x, y);}, INF);
	for(auto i:e){
		if(l[a[i]]>l[b[i]]) swap(a[i], b[i]);
		v1[l[a[i]]].push_back(i);
		v2[l[b[i]]].push_back(i);
	}
	int mn1[200020];
	fill(mn1, mn1+n, INF);
	for(int i=0; i<n; i++){
		int r1=r[rev[i]];
		mnl[rev[i]]=seg.query(i, r1);
		for(auto j:v1[i]){
			int l1=l[b[j]];
			mn1[l1]=min(mn1[l1], j);
			seg.update(l1, mn1[l1]);
		}
	}
	SegmentTree<int> seg2(n, [](int x, int y){ return min(x, y);}, INF);
	fill(mn1, mn1+n, INF);
	for(int i=n; i>=1; i--){
		for(auto j:v2[i]){
			int l1=l[a[j]];
			mn1[l1]=min(mn1[l1], j);
			seg2.update(l1, mn1[l1]);
		}
		for(auto j:vr[i]){
			mnr[j]=seg2.query(l[j], r[j]);
		}
	}
	auto calc=[&](int x, int y){
		return d[x]+d[y]-2*d[lca.lca(x, y)];
	};
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>x[i]>>y[i]>>z[i];
		x[i]--; y[i]--; z[i]--;
		if(!t[z[i]] || lca.dist(x[i], y[i])!=lca.dist(x[i], a[z[i]])+lca.dist(y[i], a[z[i]]) || lca.dist(x[i], y[i])!=lca.dist(x[i], b[z[i]])+lca.dist(y[i], b[z[i]])){
			mint ans=calc(x[i], y[i]);
			cout<<ans.val()<<endl;
		}else{
			int bz=b[z[i]];
			int k=min(mnl[bz], mnr[bz]);
			if(k==INF){
				cout<<-1<<endl;
				continue;
			}
			int a1=a[k], b1=b[k];
			if(l[bz]<=l[y[i]] && l[y[i]]<r[bz]) swap(x[i], y[i]);
			if(l[bz]<=l[b1] && l[b1]<r[bz]) swap(a1, b1);
			mint ans=calc(x[i], a1)+calc(y[i], b1)+p2[k+1];
			cout<<ans.val()<<endl;
		}
	}

    return 0;
}
0