22642023-01-06 20:31:47mraronTom és Jerry2 (60)cpp17Accepted 60/6081ms17996 KiB
#include<bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
int n,m,t,qs,e;
struct edg {
	int to,w,ind;
};
vector<edg> adj[30001];
int st1[30001], dfs_low[30001], dfs_num[30001], ind=0, bc[100001], bcind=1, dst_t[30001], dst_e[30001], st2[30001], art[30001];
stack<int> st;
vector<pair<int,int>> edgs;
void dfs1(int x, int par_e=-1) {
	st1[x]=1;
	dfs_low[x]=dfs_num[x]=++ind;
	int ch=0;
	for(edg i:adj[x]) {
		if(par_e==i.ind) {
			continue ;
		}
		if(st1[i.to]) {
			dfs_low[x]=min(dfs_low[x], dfs_num[i.to]);
			if(dfs_num[x]>dfs_num[i.to]) st.push(i.ind);
		}else {
			ch++;
			st.push(i.ind);
			dfs1(i.to, i.ind);
			if((ch>1 && par_e==-1) || (dfs_low[i.to]>=dfs_num[x])) {
				art[x]=1;
				while(st.top()!=i.ind) {
					//~ cerr<<edgs[st.top()].xx<<" "<<edgs[st.top()].yy<<"\n";
					bc[st.top()]=bcind;
					st.pop();
				}
				//~ cerr<<edgs[st.top()].xx<<" "<<edgs[st.top()].yy<<"\n";
				//~ cerr<<"===\n";
				bc[st.top()]=bcind++;
				st.pop();
			}
			
			dfs_low[x]=min(dfs_low[x], dfs_low[i.to]);
		}
	}
}

int ans[30001];
int ac;
void dfs2(int x, int par_e, int idx, int tav) {
	//~ cerr<<x<<" "<<idx<<" "<<tav<<"\n";
	if(tav<=0) {
		//~ cerr<<x<<" -> "<<idx<<"\n";
		ans[x]=idx;
	}
	st2[x]=1;
	for(auto i:adj[x]) {
		if(dst_e[i.to]!=dst_e[x]+1) continue ;
		if(x==e) {
			if(bc[i.ind]!=ac) {
				continue ;
			}
			dfs2(i.to, i.ind, idx, tav);
		}else {
			if(!st2[i.to]) {
				if(bc[i.ind]==bc[par_e]) {
					dfs2(i.to, i.ind, idx, tav-1);
				}else {
					if(dst_t[x]<=tav)
						dfs2(i.to, i.ind, x, dst_t[x]-1);
					else 
						dfs2(i.to, i.ind, idx, tav-1);
				}
			}
		}
		
		
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin>>n>>m>>t>>qs>>e;
	for(int i=0;i<m;++i) {
		int a,b,c;
		cin>>a>>b>>c;
		adj[a].pb(edg{b,c,i});
		adj[b].pb(edg{a,c,i});
		edgs.pb(mp(a,b));
	}
	
	dfs1(e);
	while(!st.empty()) {
		//~ cerr<<edgs[st.top()].xx<<" "<<edgs[st.top()].yy<<"\n";
		bc[st.top()]=bcind;
		st.pop();
	}
	//~ cerr<<"===\n";
	bcind++;
	
	queue<int> q;
	for(int i=1;i<=n;++i) dst_t[i]=1e9;
	q.push(t);
	st2[t]=1;
	dst_t[t]=0;
	while(!q.empty()) {
		auto fr=q.front();q.pop();
		for(auto i:adj[fr]) {
			if(i.w==2 && !st2[i.to] && i.to!=e) {
				dst_t[i.to]=dst_t[fr]+1;
				st2[i.to]=1;
				q.push(i.to);
			}
		}
	}

	q.push(e);
	dst_e[e]=0;
	memset(st2, 0, sizeof st2);
	st2[e]=1;
	while(!q.empty()) {
		auto fr=q.front();q.pop();
		for(auto i:adj[fr]) {
			if(!st2[i.to]) {
				dst_e[i.to]=dst_e[fr]+1;
				st2[i.to]=1;
				q.push(i.to);
			}
		}
	}
	
	memset(st2, 0, sizeof st2);
	vector<int> volt(n+1);
	for(auto i:adj[e]) {
		if(!volt[bc[i.ind]]) {
			//cerr<<i.to<<"\n";
			//cerr<<bc[i.ind]<<"what\n";
			ac=bc[i.ind];
			dfs2(e, -1, 1e9, 1e9);
			volt[bc[i.ind]]=1;
		}
	}
	
	for(int i=0;i<qs;++i) {
		int x;
		cin>>x;
		cout<<ans[x]<<"\n";
	}
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms3584 KiB
2Accepted0/021ms5812 KiB
3Accepted2/23ms4044 KiB
4Accepted2/23ms3920 KiB
5Accepted2/23ms3936 KiB
6Accepted3/33ms4208 KiB
7Accepted2/23ms4772 KiB
8Accepted2/24ms4752 KiB
9Accepted2/23ms4848 KiB
10Accepted3/34ms4900 KiB
11Accepted3/34ms5192 KiB
12Accepted3/36ms5648 KiB
13Accepted3/38ms6500 KiB
14Accepted3/312ms7092 KiB
15Accepted3/314ms7684 KiB
16Accepted3/317ms8100 KiB
17Accepted3/329ms9496 KiB
18Accepted3/365ms13448 KiB
19Accepted4/441ms13916 KiB
20Accepted4/481ms17996 KiB
21Accepted5/554ms15452 KiB
22Accepted5/554ms17656 KiB