Given a graph with n points and m edges, K points have detectors.
Given the first traversal sequence of a detector, ask if there is a traversal sequence that satisfies the given sequence and traverses all vertices.
Idea: DFS starts from the first retrieved detector. After each access to the detector, it stops and searches for non-detector nodes. Determine whether the next detector in the given sequence has been accessed after the completion. If no, it means that it cannot be reached by another detector without solution. If you have accessed this node, continue to the DFS node.
In this way, the DFS detector ensures that it can be accessed from the current node without passing through any detector node, and finally determines whether each node has been accessed, that is, whether the graph is connected.
//#include <bits/stdc++.h>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <vector>#include <iostream>#include <algorithm>using namespace std;//LOOP#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)#define PB push_back//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define sqr(x) (x) * (x)typedef long long LL;typedef unsigned long long ULL;typedef vector <int> VI;const int INF = 0x3f3f3f3f;const LL lINF = 0x3f3f3f3f3f3f3f3fLL;const double eps = 1e-9;const int MOD = 1e9 + 7;const double PI = acos(-1.0);const int maxn = 100010;int n, m, k;bool is[maxn], vis[maxn];VI G[maxn], K;void init(){ CLR(is, 0); CLR(vis, 0); K.clear(); FE(i, 0, n + 1) G[i].clear();}void dfs(int u){ vis[u] = 1; REP(i, G[u].size()) { int v = G[u][i]; if (vis[v]) continue; if (is[v]) { vis[v] = 1; continue; } else dfs(v); }}int main(){ int T, x, y; RI(T); while (T--) { RIII(n, m, k); init(); REP(i, k) { RI(x); is[x] = 1; } REP(i, m) { RII(x, y); G[x].PB(y), G[y].PB(x); } int L, flag = 1; RI(L); REP(i, L) { RI(x); K.PB(x); } if (L < k) { flag = 0; goto end; } dfs(K[0]); FF(i, 1, K.size()) { if (vis[K[i]]) dfs(K[i]); else { flag = 0; goto end; } } FE(i, 1, n) if (!vis[i]) { flag = 0; break; } end:; if (flag) puts("Yes"); else puts("No"); } return 0;}/**/
Untrusted patrol 14 Mudanjiang Network Competition C