QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#84895#4511. Wonderland Chasejohnsonloy_x30 ✓2748ms53096kbC++142.5kb2023-03-06 20:47:452023-03-06 20:47:49

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 20:47:49]
  • 评测
  • 测评结果:30
  • 用时:2748ms
  • 内存:53096kb
  • [2023-03-06 20:47:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define ll long long
const int mod = 1e9+7;
const int maxn = 1e6+10;

namespace IO{
	void openfile(){
		#ifndef ONLINE_JUDGE
		freopen("in.in","r",stdin);
		freopen("out.out","w",stdout);
		#endif
	}

	int add(int x,int y){
		x+=y;
		if(x>=mod)
			x-=mod;
		return x;
	}

	int sub(int x,int y){
		x-=y;
		if(x<0)
			x+=mod;
		return x;
	}

	inline int read(){
		int x = 0,f = 0;
		char c = getchar();
		while(!isdigit(c))
			f|=c=='-',c = getchar();
		while(isdigit(c))
			x = x*10+c-'0',c = getchar();
		if(f)x = -x;
		return x;
	}
}
using namespace IO;

int T;
int n,m,A,B,vis[maxn],in[maxn],f[maxn];
int dis1[maxn],dis2[maxn],dis[maxn];
vector<int>e[maxn];

int find(int x){
	if(x==f[x])return x;
	return f[x] = find(f[x]);
}

void merge(int x,int y){
	x = find(x),y = find(y);
	f[x] = y;
}

void dij(int s){
	memset(dis,0x3f,sizeof(dis));
	for(int i=1;i<=n;i++)
		vis[i] = 0;
	priority_queue<pii,vector<pii>,greater<pii> >q;
	q.push(mp(0,s)),dis[s] = 0;
	while(!q.empty()){
		int x = q.top().second;
		q.pop();
		if(vis[x])
			continue;
		vis[x] = 1;
		for(int i=0;i<e[x].size();i++){
			int v = e[x][i];
			if(dis[v]>dis[x]+1)
				dis[v] = dis[x]+1,q.push(mp(dis[v],v));
		}
	}
}

void solve(int num){
	n = read(),m = read(),A = read(),B = read();
	for(int i=1;i<=n;i++)
		f[i] = i,in[i] = 0,e[i].clear();
	for(int i=1;i<=m;i++){
		int x = read(),y = read();
		e[x].push_back(y),e[y].push_back(x);
		merge(x,y);
		in[x]++,in[y]++;
	}
	printf("Case #%d: ",num);
	if(find(A)!=find(B)){
		puts("SAFE");
		return;
	}
	queue<int>q;
	for(int i=1;i<=n;i++)
		if(in[i]==1)
			q.push(i);
	dij(A);
	for(int i=1;i<=n;i++)
		dis1[i] = dis[i];
	dij(B);
	for(int i=1;i<=n;i++)
		dis2[i] = dis[i],vis[i] = 0;
//	for(int i=1;i<=n;i++)
//		cout<<dis1[i]<<' '<<dis2[i]<<endl;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		vis[x] = 1;
		for(int i=0;i<e[x].size();i++){
			int v = e[x][i];
			in[v]--;
			if(in[v]==1)
				q.push(v);
		}
	}
	for(int i=1;i<=n;i++){
		if(dis1[i]<dis2[i]&&!vis[i]){
			puts("SAFE");
			return;
		}
	}
//	for(int i=1;i<=n;i++)
//		if(!vis[i])
//			cout<<i<<endl;
	int ans = 0;
	for(int i=1;i<=n;i++){
		if(dis1[i]<dis2[i])
			ans = max(ans,dis2[i]);
	}
	printf("%d\n",ans<<1);
}

signed main(){
//	openfile();
	T = read();
	for(int i=1;i<=T;i++)
		solve(i);
	return 0;
}

/*

1
2 1 1 2
1 2

*/

详细

Test #1:

score: 8
Accepted
time: 29ms
memory: 40604kb

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: 22
Accepted
time: 2748ms
memory: 53096kb

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
Case #5: 2
Case #6: SAFE
Case #7: 2
Case #8: 39998
Case #9: 39998
Case #10: 19192
Case #11: 2
Case #12: 99998
Case #13: 99998
Case #14: 16776
Case #15: 2
Case #16: 199998
Case #17: 199998
Case #18: 141806
Case #19: SAFE
Case #20: SAFE
Case #21: SAFE
...

result:

ok 100 lines