人类正在经历一场生化危机,许多城市已经被病毒侵袭,这些城市中的人们为了避免感染病毒,计划开车逃往其他没有被病毒入侵的城市(安全城市)。有些城市之间有公路直达,有些没有。虽然他们知道哪些城市是安全的,但是不知道有没有一条安全路径能够到达安全城市(只有该路径上经过的所有城市都是安全的,该路径才是安全路径)。请你编写一个程序帮助他们判断。
输入格式:
输入第一行为三个正整数,分别表示所有城市个数m(m<=100)、安全城市个数n(m<=50)、公路个数k(k<=100)。随后一行给出n个安全城市的编号。随后k行,每一行给出两个整数,表示连接一条公路的两个城市编号。最后一行输入两个整数,分别表示当前所在城市s、目标城市d。每行整数之间都用空格分隔。
输出格式:
若目标城市已被病毒入侵(非安全城市),输出"The City i is not safe!";若目标城市为安全城市且从当前所在城市能够经过一条安全路径到达目标城市,输出"The city can arrive safely!";若目标城市为安全城市但是从当前所在城市没有一条安全路径到达目标城市,输出"The city can not arrive safely!",i为目标城市编号。
输入样例1:
5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 4
输出样例1:
The city 4 can arrive safely!
输入样例2:
5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 3
输出样例2:
The city 3 can not arrive safely!
输入样例3:
5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 1
输出样例3:
The city 1 is not safe!
- dfs就行了,本题没有坑点
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN (128)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO {
char print_f[105];
void read() { }
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K, safe[MAXN], ok = false, vis[MAXN];
vector<int> G[MAXN];
void dfs(int u, int V) {
if(u == V) {
ok = true;
return ;
}
vis[u] = true;
for(auto v : G[u])
if(!vis[v] && safe[v]) dfs(v, V);
}
int main() {
#ifdef debug
freopen("test", "r", stdin);
// freopen("out_main", "w", stdout);
clock_t stime = clock();
#endif
read(n, m, K);
int u, v;
for(int i=1; i<=m; i++) {
read(u);
safe[u] = true;
}
for(int i=1; i<=K; i++) {
read(u, v);
G[u].push_back(v), G[v].push_back(u);
}
read(u, v);
if(!safe[v])
printf("The city %d is not safe!\n", v);
else {
dfs(u, v);
if(ok)
printf("The city %d can arrive safely!\n", v);
else
printf("The city %d can not arrive safely!\n", v);
}
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}