QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#407486#4511. Wonderland ChaseChrysanthBlossom8 36ms12808kbC++232.2kb2024-05-08 19:52:352024-05-08 19:52:37

Judging History

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

  • [2024-05-08 19:52:37]
  • 评测
  • 测评结果:8
  • 用时:36ms
  • 内存:12808kb
  • [2024-05-08 19:52:35]
  • 提交

answer

#include<bits/stdc++.h>
#define ri register int
#define ll long long
using namespace std;
const int maxn=2e5+7;
int T;
int n,m;
int cnt,head[maxn],to[maxn<<1],nxt[maxn<<1];
void add(int u,int v){
	++cnt;
	to[cnt]=v;
	nxt[cnt]=head[u];
	head[u]=cnt;
}
int dis[2][maxn];
int dfn[maxn],low[maxn];
int tot;
int tim;
stack<int>st;
vector<int>edcc[maxn];
void tarjan(int u,int fa){
	st.push(u);
	dfn[u]=low[u]=++tim;
	for(ri e=head[u];e;e=nxt[e]){
		int v=to[e];
		if(v==fa)continue;
		if(dfn[v]){
			low[u]=min(low[u],dfn[v]);
			continue;
		} 
		tarjan(v,u);
		low[u]=min(low[u],low[v]);
		if(low[v]>dfn[u]){
			++tot;
			while(st.top()!=v){
				edcc[tot].emplace_back(st.top());
				st.pop();
			}
			edcc[tot].emplace_back(st.top());
			st.pop();
		}
	}
}
int a,b;
bool vis[maxn];
void get_dis(int u,int p){
	queue<int>q;
	q.push(u);
	memset(dis[p],0x3f,sizeof(dis[p]));
	dis[p][u]=0;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(ri e=head[u];e;e=nxt[e]){
			int v=to[e];
			if(dis[p][u]+1<dis[p][v]){
				dis[p][v]=dis[p][u]+1;
				q.push(v);
			}
		}
	}
}
void clear(){
	memset(head,0,sizeof(head));
	memset(to,0,sizeof(to));
	memset(nxt,0,sizeof(nxt));
	cnt=0;tim=0;
	for(ri i=1;i<=tot;i++){
		edcc[i].clear();
	}
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
}
void solve(int now){
	cin>>n>>m>>a>>b;
	for(ri i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		add(u,v);add(v,u);
	}
	for(ri i=1;i<=n;i++){
		tarjan(i,0);
		++tot;
		while(!st.empty()){
			edcc[tot].emplace_back(st.top());
			st.pop();
		}
	}
	for(ri i=1;i<=tot;i++){
		if(edcc[i].size()>=3){
			for(auto x:edcc[i]){
				vis[x]=1;
			}
		}
	}
	get_dis(a,0);
	get_dis(b,1);
	if(dis[0][b]>m){
		cout<<"Case #"<<now<<": SAFE\n";
		return;
	}
	for(ri i=1;i<=n;i++){
		if(dis[0][i]<dis[1][i]&&vis[i]){
			cout<<"Case #"<<now<<": SAFE\n";
			return;
		}
	}
	int ans=0;
	for(ri i=1;i<=n;i++){
		if(dis[0][i]<dis[1][i]){
			ans=max(ans,dis[1][i]);
		}
	}
	cout<<"Case #"<<now<<": "<<ans*2<<endl;
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	for(ri i=1;i<=T;i++){
		solve(i);
		clear();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 8
Accepted
time: 36ms
memory: 12808kb

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:

Case #1: 2
Case #2: SAFE
Case #3: 8
Case #4: 6
Case #5: SAFE
Case #6: SAFE
Case #7: SAFE
Case #8: SAFE
Case #9: SAFE
Case #10: 8
Case #11: 4
Case #12: 2
Case #13: SAFE
Case #14: 2
Case #15: 10
Case #16: 10
Case #17: 6
Case #18: 2
Case #19: 28
Case #20: 28
Case #21: 18
Case #22: 2
Case #23: 58
Case #...

result:

ok 100 lines

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:

Case #1: SAFE
Case #2: SAFE
Case #3: 8
Case #4: 4

result: