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;
}


Work-Stealing Queue是一种用于并发编程的数据结构,它允许多个线程在共享队列上执行工作项,其中每个线程维护自己的任务队列,并且当自己的队列为空时,可以从其他线程的队列中“偷”一些工作项来执行。该数据结构适用于任务量不确定、任务执行时间较长的情况。 下面是一个使用Work-Stealing Queue实现顺序执行的简单代码示例: ```c++ #include <iostream> #include <thread> #include <vector> #include "taskflow/taskflow.hpp" void func1() { std::cout << "Function 1" << std::endl; } void func2() { std::cout << "Function 2" << std::endl; } void func3() { std::cout << "Function 3" << std::endl; } int main() { tf::Executor executor; tf::Taskflow taskflow; // 将三个函数作为任务添加到taskflow中 auto task1 = taskflow.emplace([]() { func1(); }); auto task2 = taskflow.emplace([]() { func2(); }); auto task3 = taskflow.emplace([]() { func3(); }); // 任务之间的依赖关系,保证顺序执行 task1.precede(task2); task2.precede(task3); // 将taskflow提交给executor执行 executor.run(taskflow).wait(); return 0; } ``` 在上面的示例中,我们使用`Taskflow`库来创建一个`taskflow`对象,并将三个函数作为任务添加到其中。然后,我们使用`precede`方法来定义任务之间的依赖关系,以确保它们按照正确的顺序执行。最后,我们将`taskflow`提交给`executor`执行,并等待任务完成。 这样,我们就可以实现一个函数一个函数顺序执行的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值