Stealing Spider-Man (暴力递归)

Stealing Spider-Man

Description
Headline Story: Perpetrator Tom Holland (Spider-Man) Proven To Have Stolen Pure Topaz! How Scary! Please Think: How Should Programmers Theorize Helpful Solutions, Persevering Through Hardly Simple, Problematic Times?
You are the head of the Highly Smart Programming Team division in the NYPD and have been tasked with determining if the police have enough time to stop Spider-Man before he escapes. The police have available to them one Highly Suspicious Police Trap, which can block one road so Spider-Man cannot escape using that road.
The Problem:
Being a part of the sophisticated police department, you have available to you a complete representation of the city in the form of intersections and roads connecting those intersections. Luckily, all locations in the city are connected with some pathway so Spider-Man won’t be able to hide! Your task is to determine if there is a road such that if you place your Highly Suspicious Police Trap on that road, thereby blocking the road, Spider-Man will not be able to escape from the museum (where he stole the topaz) to his getaway helicopter.
Input
The input will start with a single, positive integer, t, representing the number of heists for you to solve. Each heist will start with a line containing two integers, n and m (2≤n≤1,000; 1≤m≤1,000), representing the number of intersections and the number of roads, respectively.
For the purposes of this problem, intersection 1 will be the museum, and intersection n will be Spider-Man’s getaway point. Then, m lines will follow, each containing two integers, u and v (1≤u≤n; 1≤v≤n), denoting that there is a unique two-way road between intersections u and v (no roads appear twice in the input within a single heist).
Output
For each heist, output “Halt, Spider-Man! Plans Thwarted!” if it is possible to place your Highly Suspicious Police Trap such that Spider-Man cannot escape that heist, or output “How Sad, Perpetrator Triumphed.” otherwise.
Samples
Input
5 5
1 3
4 5
3 2
1 2
4 3
3 3
1 3
2 3
2 1
Output
Halt, Spider-Man! Plans Thwarted!
How Sad, Perpetrator Triumphed.

Source
UCF HSPT 2021


这题在比赛的时候没敢写,这个其实和一个人只有两只手牵别人,然后问你形成最长的链长度是多少有点像。

题意:给你n个点,m条边,问你如果去掉任意一条边使1无法到到n那么就输出“Halt, Spider-Man! Plans Thwarted!”,否则输出“How Sad, Perpetrator Triumphed.”
思路:我们观察数据范围(2≤n≤1,000; 1≤m≤1,000),所以暴力去处每一条边然后去迭代跑图复杂度最多1e6.可行。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,T,u,v;
bool a[1010][1010];
bool vis[1010];
struct node {
	int u,v;
} d[1010];
bool dfs(int x) {
	//cout<<x<<endl;
	if(x==n) return 1;
	vis[x] = 1;
	for(int i=1;i<=n;i++) {
		if(a[i][x] && a[x][i] && !vis[i]) {
			if(dfs(i)) return 1;
		}
	}
	return 0;
}
int main() {
	cin>>T;
	while(T--) {
		scanf("%d%d",&n,&m);
		memset(a,0,sizeof(a));
		for(int i=1; i<=m; i++) {
			scanf("%d%d",&u,&v);
			a[u][v] = 1;
			a[v][u] = 1;
			d[i].u = u;
			d[i].v = v;
		}
		bool ok = 1;
		for(int i=1; i<=m; i++) {
			memset(vis,0,sizeof(vis));
			int x = d[i].u,y = d[i].v;
			a[x][y] = a[y][x] = 0;
			if(!dfs(1)) {
				ok = 0;
				break;
			}
			a[x][y] = a[y][x] = 1;
		}
		if(ok) puts("How Sad, Perpetrator Triumphed.");
		else puts("Halt, Spider-Man! Plans Thwarted!");
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值