QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84574#4511. Wonderland Chasexxzx0 0ms0kbC++141.5kb2023-03-06 15:58:122023-03-06 15:58:16

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 15:58:16]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-03-06 15:58:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef pair<int,int> PII;
int n,m,A,B,ind[N],usafe[N],dis[2][N],vis[N];	
vector<int> g[N];
void dij(int op,int st) {
	memset(vis,0,sizeof(vis));
	priority_queue<PII,vector<PII>,greater<PII>> q;
	dis[op][st]=0; q.emplace(0,st);
	while(q.size()) {
		int u=q.top().second; q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(auto to:g[u]) if(dis[op][to]>dis[op][u]+1){
			dis[op][to]=dis[op][u]+1; q.emplace(dis[op][to],to);
		}
	}
}
bool dfs(int x) {
	vis[x]=1;
	for(auto to:g[x]) if(!vis[to]) dfs(to);
}
int test;
void solve() {
	scanf("%d%d%d%d",&n,&m,&A,&B);
	for(int i=1;i<=n;i++) g[i].clear();
	memset(ind,0,sizeof(ind)), memset(usafe,0,sizeof(usafe)), memset(dis,0x3f,sizeof(dis));
	for(int i=1,u,v;i<=m;i++) {
		scanf("%d%d",&u,&v);
		g[u].emplace_back(v), g[v].emplace_back(u);
		ind[u]++, ind[v]++;
	}
	queue<int> q;
	for(int i=1;i<=n;i++) if(ind[i]<=1) q.emplace(i);
	while(q.size()) {
		int u=q.front(); q.pop();
		usafe[u]=1;
		for(auto to:g[u]) if(!usafe[to]) {
			ind[to]--;
			if(ind[to]<=1) q.emplace(to);
		}
	}
	memset(vis,0,sizeof(vis)); dfs(A);
	if(!vis[B]) return printf("Case #%d: SAFE\n",++test), void();
	dij(0,A), dij(1,B);
	int ans=0;
	for(int i=1;i<=n;i++) {
		if(dis[0][i]<dis[1][i]) {
			if(!usafe[i]) ans=INT_MAX;
			else ans=max(ans,dis[1][i]);
		}
	}
	if(ans==INT_MAX) printf("Case #%d: SAFE\n",++test);
	else printf("Case #%d: %d\n",++test,ans*2);
}
int main() {
	int T; scanf("%d",&T);
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

100
2 1 1 2
1 2
3 3 1 2
1 3
1 2
2 3
6 6 5 1
1 4
5 6
3 4
3 6
2 3
1 2
6 6 2 4
4 5
1 4
2 3
2 5
1 6
5 6
6 6 2 3
1 3
3 4
2 6
2 5
4 5
1 5
6 5 5 3
2 5
3 4
1 2
3 6
4 6
6 5 1 6
1 4
1 2
5 6
3 5
2 4
30 29 11 5
9 21
25 28
14 20
13 30
21 28
5 18
5 23
8 22
10 30
4 8
7 24
16 26
13 26
12 18
22 23
11 16
3 11
2 17
1 ...

output:


result:


Test #2:

score: 0
Runtime Error

input:

100
100000 99999 32832 52005
67463 96972
10280 86580
12146 44520
41541 86634
46936 64223
22701 46291
9093 80967
52512 77386
51062 58931
2092 55026
2096 2384
85102 92986
39914 66949
33370 68952
41576 58836
27668 33997
5843 30705
44415 57721
15188 28706
23340 55082
20335 90872
16029 80328
4656 74633
8...

output:


result: