# PTA 7-51 biochemical crisis (25 points) dfs

Posted by mattsutton on Fri, 19 Jun 2020 13:21:17 +0200

Human beings are experiencing a biochemical crisis. Many cities have been attacked by the virus. In order to avoid infection, people in these cities plan to drive to other cities (safe cities) that are not invaded by the virus. Some cities have direct roads, some don't. Although they know which cities are safe, they don't know if there is a safe path to reach the safe city (only if all the cities on the path are safe, the path is the safe path). Please write a program to help them judge.
Input format:

The first line is three positive integers, which respectively represent the number of all cities m (m < = 100), the number of safe cities n (m < = 50), and the number of highways K (k < = 100). The next line gives the number of N safe cities. Then line k, each of which gives two integers, representing the two city numbers connecting a highway. In the last line, enter two integers to represent the current city s and the target city d. Each line of integers is separated by spaces.
Output format:

If the target city has been invaded by virus (unsafe city), output "The City i is not safe!"; if the target city is a safe city and can reach the target city through a safe path from the current city, output "the city can drive safely!"! "; if the target city is a safe city but there is no safe path from the current city to the target city, output" The city can not arrive safely! ", i is the target city number.
Input example 1:

5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 4

Output sample 1:

The city 4 can arrive safely!

Input example 2:

5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 3

Output sample 2:

The city 3 can not arrive safely!

Input example 3:

5 2 5
3 4
0 1
0 2
0 4
1 2
2 4
0 1

Output example 3:

The city 1 is not safe!

• dfs is enough. There is no hole in this question
```#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;
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;
}
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;

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
int u, v;
for(int i=1; i<=m; i++) {
safe[u] = true;
}
for(int i=1; i<=K; i++) {
G[u].push_back(v), G[v].push_back(u);
}
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 second\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}

```