QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#84773#4511. Wonderland ChaseKaiserWilheim8 0ms7664kbC++201.9kb2023-03-06 18:27:502023-03-06 18:28:52

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 18:28:52]
  • 评测
  • 测评结果:8
  • 用时:0ms
  • 内存:7664kb
  • [2023-03-06 18:27:50]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 100010, M = 400010;
int n, m, s, t;
int fa[N];
int find(int x)
{
	if(fa[x] != x)fa[x] = find(fa[x]);
	return fa[x];
}
void soyuz(int u, int v)
{
	int fu = find(u), fv = find(v);
	if(fu != fv)fa[fu] = fv;
}
int h[N], e[M], ne[M], idx;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int deg[N];
bool safe[N];
int dis[2][N];
void bfs(int s, int ty)
{
	queue<int>q;
	dis[ty][s] = 0;
	q.push(s);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		for(int i = h[u]; ~i; i = ne[i])
		{
			int v = e[i];
			if(dis[ty][v] != 1e9)continue;
			dis[ty][v] = dis[ty][u] + 1;
			q.push(v);
		}
	}
}
void solve(int id)
{
	scanf("%d%d", &n, &m);
	scanf("%d%d", &s, &t);
	for(int i = 1; i <= n; i++)
	{
		h[i] = -1;
		deg[i] = 0;
		safe[i] = true;
		dis[0][i] = dis[1][i] = 1e9;
		fa[i] = i;
	}
	for(int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		add(u, v), add(v, u);
		soyuz(u, v);
		deg[v]++, deg[u]++;
	}
	if(find(s) != find(t))
	{
		printf("Case #%d: SAFE\n", id);
		return;
	}
	queue<int>q;
	for(int i = 1; i <= n; i++)
		if(deg[i] == 1)q.push(i);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		safe[u] = false;
		for(int i = h[u]; ~i; i = ne[i])
		{
			deg[e[i]]--;
			if(deg[e[i]] == 1)q.push(e[i]);
		}
	}
	bfs(s, 0);
	bfs(t, 1);
	bool flag = false;
	for(int i = 1; i <= n; i++)
	{
		if(!safe[i])continue;
		if(dis[0][i] < dis[1][i])flag = true;
	}
	if(flag)
	{
		printf("Case #%d: SAFE\n", id);
		return;
	}
	int maxn = 0;
	for(int i = 1; i <= n; i++)
	{
		if(dis[0][i] >= dis[1][i])continue;
		maxn = max(maxn, dis[1][i]);
	}
	printf("Case #%d: %d\n", id, maxn * 2);
}
int main()
{
	int T;
	scanf("%d", &T);
	for(int i = 1; i <= T; i++)
	{
		solve(i);
	}
	return 0;
}

详细

Test #1:

score: 8
Accepted
time: 0ms
memory: 7664kb

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
Memory Limit Exceeded

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: