結果

問題 No.1600 Many Shortest Path Problems
ユーザー 沙耶花沙耶花
提出日時 2021-07-10 00:08:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 783 ms / 4,000 ms
コード長 4,594 bytes
コンパイル時間 5,449 ms
コンパイル使用メモリ 284,244 KB
実行使用メモリ 48,368 KB
最終ジャッジ日時 2023-09-14 11:31:36
合計ジャッジ時間 24,917 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 783 ms
47,528 KB
testcase_05 AC 752 ms
47,468 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 299 ms
5,080 KB
testcase_11 AC 505 ms
6,880 KB
testcase_12 AC 674 ms
17,180 KB
testcase_13 AC 735 ms
37,052 KB
testcase_14 AC 713 ms
47,452 KB
testcase_15 AC 2 ms
4,356 KB
testcase_16 AC 2 ms
4,356 KB
testcase_17 AC 740 ms
26,388 KB
testcase_18 AC 705 ms
47,424 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 693 ms
26,056 KB
testcase_22 AC 2 ms
4,356 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 746 ms
47,364 KB
testcase_25 AC 1 ms
4,356 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,356 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 335 ms
46,300 KB
testcase_30 AC 377 ms
39,568 KB
testcase_31 AC 635 ms
26,224 KB
testcase_32 AC 701 ms
25,912 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 481 ms
47,240 KB
testcase_36 AC 313 ms
48,368 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 329 ms
39,680 KB
testcase_39 AC 1 ms
4,352 KB
testcase_40 AC 365 ms
39,396 KB
testcase_41 AC 271 ms
46,200 KB
testcase_42 AC 305 ms
39,656 KB
testcase_43 AC 297 ms
39,460 KB
testcase_44 AC 361 ms
35,412 KB
testcase_45 AC 309 ms
46,312 KB
testcase_46 AC 310 ms
39,504 KB
testcase_47 AC 373 ms
39,536 KB
testcase_48 AC 380 ms
38,648 KB
testcase_49 AC 1 ms
4,352 KB
testcase_50 AC 2 ms
4,356 KB
testcase_51 AC 2 ms
4,360 KB
testcase_52 AC 2 ms
4,352 KB
testcase_53 AC 1 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};

int op(int a,int b){
	return min(a,b);
}

int e(){
	return Inf;
}

int mapping(int a,int b){
	if(a==-1)return b;
	return a;
}

int composition(int a,int b){
	if(a==-1)return b;
	if(b==-1)return a;
	return a;
}

int id(){
	return -1;
}

int main(){
	
	int N,M;
	cin>>N>>M;
	
	dsu D(N);
	
	vector<int> A(M),B(M);
	rep(i,M){
		scanf("%d %d",&A[i],&B[i]);
		A[i]--;B[i]--;
	}
	
	vector<int> ind;
	vector<vector<int>> E(N);
	
	rep(i,M){
		if(D.same(A[i],B[i]))continue;
		D.merge(A[i],B[i]);
		int t = E.size();
		E[A[i]].push_back(t);
		E[B[i]].push_back(t);
		E.push_back({A[i],B[i]});
		ind.push_back(i);
	}
	
	HLD H(E);
	
	vector<mint> cost(E.size(),0);
	vector<bool> f(E.size(),false);
	f[0] = true;
	{
		queue<int> Q;
		Q.push(0);
		
		while(Q.size()>0){
			int u = Q.front();
			Q.pop();
			
			rep(i,E[u].size()){
				int v = E[u][i];
				if(f[v])continue;
				f[v] = true;
				mint temp = cost[u];
				if(v >= N){
					temp += mint(2).pow(ind[v-N]);
				}
				cost[v] = temp;
				Q.push(v);
			}
			
		}
	}
	
	lazy_segtree<int,op,e,int,mapping,composition,id> seg(E.size());
	
	for(int i=M-1;i>=0;i--){
		if(binary_search(ind.begin(),ind.end(),i))continue;
		auto ret = H.query(A[i],B[i]);
		
		rep(j,ret.size()){
			int l = ret[j].first,r = ret[j].second;
			if(l>r)swap(l,r);
			r++;
			seg.apply(l,r,i);
		}
	}
	/*
	rep(i,E.size()){
		cout<<seg.get(H.pos[i])<<endl;
	}
	*/
	
	int Q;
	cin>>Q;
	
	rep(_,Q){
		int x,y,z;
		scanf("%d %d %d",&x,&y,&z);
		x--;y--;z--;
		mint ans = 0;
		bool ng = false;
		if(!binary_search(ind.begin(),ind.end(),z)){
			ans += cost[x] + cost[y];
			ans -= cost[H.lca(x,y)] * 2;
		}
		else{
			z = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),z));
			int pos = H.pos[N+z];
			

			auto ret = H.query(x,y);
			bool ff = true;
			rep(j,ret.size()){
				int l = ret[j].first,r = ret[j].second;
				if(l>r)swap(l,r);
				r++;
				if(l<=pos&&pos<r){
					ff = false;
					break;
				}
			}
			
			if(ff){
				ans += cost[x] + cost[y];
				ans -= cost[H.lca(x,y)] * 2;
			}
			else{
				int ttt = seg.get(pos);
				if(ttt==Inf){
					ng = true;
				}
				else{
					ans += mint(2).pow(ttt);
					ret = H.query(x,A[ttt]);
					ff = true;
					rep(j,ret.size()){
						int l = ret[j].first,r = ret[j].second;
						if(l>r)swap(l,r);
						r++;
						if(l<=pos&&pos<r){
							ff = false;
							break;
						}
					}
					
					if(H.get_distance(x,A[ttt])+H.get_distance(y,B[ttt])>H.get_distance(y,A[ttt])+H.get_distance(x,B[ttt])){
						swap(x,y);
					}
					
					ans += cost[x] + cost[A[ttt]] - cost[H.lca(x,A[ttt])]*2;
					ans += cost[y] + cost[B[ttt]] - cost[H.lca(y,B[ttt])]*2;
					
				}
				
				
			}
			
		}
		ans *= 2;
		if(ng)printf("-1\n");
		else printf("%d\n",ans.val());
		
	}
	
	
	return 0;
}
0