1. Title Description: Click to open the link
2. Problem-Solving ideas: The use of bfs+ and search set to solve. The test instructions asks if the access order of the input is feasible, and all nodes are accessible, so if the whole graph is disconnected at first, it is natural to output no, and connectivity can be judged by a set of checks. If the diagram is connected, then look at the relationship between L and K, if l<k, indicating that some sensors have not received the first access to the information, that the node has not been visited, but also direct output no, otherwise, we began to look for the first to access the warehouse.
If the order of access is feasible, then the first warehouse is not important, it may be possible to collect the first sensor signal is located at the node as the first access to the warehouse, next we can use BFS to all adjacent with the sensor node to mark, that is, the first sensor node labeled 1, Put them in the BFS and place them in zero. Thus, if the next input sensor node is still 1, it is impossible to describe the access order. Otherwise, the new node will continue to be BFS.
Why is it possible to do so? Because the problem tells us that the sensor can only receive the first access information, then we start from U (U is a node with a sensor), access to the next sensor node V, when the u,v sensor is closed, and then through U to access another and his adjacent sensor node W, so down, Any sensor that is adjacent to u can be accessed in any order. Conversely, if a sensor V is still 1, then it must and u are not adjacent to the sensor, it is impossible to directly access the V from U. This proves the correctness of BFS practice.
3. Code:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <algorithm># include<cassert> #include <string> #include <sstream> #include <set> #include <vector># include<stack> #include <map> #include <queue> #include <deque> #include <cstdlib># include<cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cctype># include<functional>using namespace std; #define ME (s) memset (s,0,sizeof (s)) typedef long Long Ll;typedef unsigned int uint;typedef unsigned long long ull;typedef pair <int, int> p;const int n=100000+5;int vis[n],t[n];int p[n];vect Or<int>g[n];int find (int x) {return p[x]==x?x:p[x]=find (p[x]);} void BFs (int u) {vis[u]=1; t[u]=0; queue<int>q; Q.push (U); while (!q.empty ()) {int U=q.front (); Q.pop (); int len=g[u].size (); for (int i=0;i<len;i++) {int v=g[u][i]; if (t[v]| | VIS[V]) {t[v]=0;vis[v]=1;continue; } vis[v]=1; Q.push (v); }}}int Main () {int T; scanf ("%d", &t); while (t--) {int n,m,k,l; Me (t); me (Vis); me (P); for (int i=0;i<n;i++) g[i].clear (); scanf ("%d%d%d", &n,&m,&k); int x; for (int i=0;i<k;i++) {scanf ("%d", &x); T[x]=1; Mark the node containing the sensor as 1} int u,v; int cnt=n; for (int i=1;i<=n;i++) p[i]=i; for (int i=0;i<m;i++) {scanf ("%d%d", &u,&v); G[u].push_back (v); G[v].push_back (U); int X=find (U), Y=find (v); if (x!=y) {p[x]=y; cnt--; }} int flag=1; if (cnt>1) flag=0;//if the number of connected components is greater than 1, the entire graph is not connected scanf ("%d", &l); if (l<k) flag=0; If the number of sensors received is less than K, it indicates that some nodes do not have access to the for (int i=0;i<l;i++) { scanf ("%d", &x); if (flag==0| | (T[x]&&i)) If not the head node and t[x] is still 1, indicating that the access order is not possible {flag=0;continue; } BFS (x); Otherwise, the node is BFS, marking the adjacent sensor nodes as 0} printf ("%s\n", Flag? "). Yes ":" No "); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ3811 Untrusted Patrol (2014 Mudanjiang Division network game c)