結果

問題 No.1600 Many Shortest Path Problems
ユーザー 沙耶花沙耶花
提出日時 2021-07-09 22:44:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,430 bytes
コンパイル時間 5,444 ms
コンパイル使用メモリ 283,496 KB
実行使用メモリ 48,264 KB
最終ジャッジ日時 2023-09-14 10:23:31
合計ジャッジ時間 25,766 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 749 ms
47,320 KB
testcase_05 AC 759 ms
47,428 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 820 ms
37,364 KB
testcase_14 AC 784 ms
47,516 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 804 ms
26,104 KB
testcase_18 AC 763 ms
47,504 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 753 ms
26,324 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 760 ms
47,452 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 309 ms
46,376 KB
testcase_30 AC 355 ms
39,720 KB
testcase_31 AC 680 ms
26,360 KB
testcase_32 AC 761 ms
26,272 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 518 ms
47,396 KB
testcase_36 AC 329 ms
48,264 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 327 ms
39,448 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 364 ms
39,700 KB
testcase_41 AC 264 ms
46,516 KB
testcase_42 AC 300 ms
39,660 KB
testcase_43 AC 295 ms
39,756 KB
testcase_44 AC 367 ms
35,596 KB
testcase_45 AC 303 ms
46,556 KB
testcase_46 AC 315 ms
39,448 KB
testcase_47 AC 376 ms
39,768 KB
testcase_48 AC 380 ms
38,760 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1 ms
4,376 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{
			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(!ff){
						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