ZOJ 3811 zoj 3811 Untrusted Patrol牡丹江网络赛C题

去年的比赛题目,今年才搞懂AC了===||

  1 #include <cstdio>

  2 #include <cstdlib>

  3 #include <cstring>

  4 #include <cctype>

  5 #include <cmath>

  6 #include <algorithm>

  7 #include <vector>

  8 #include <queue>

  9 #include <stack>

 10 using namespace std;

 11 

 12 const int inf = 0x3f;

 13 const int INF = 0x3f3f3f3f;

 14 const int maxn = 100005;

 15 int N, M, K, L;

 16 bool sen[maxn], visit[maxn];

 17 int pass[maxn];

 18 int f[maxn];

 19 vector <int>G[maxn];

 20 queue <int> q;

 21 

 22 int find(int x)

 23 {

 24     return x == f[x] ? x : f[x] = find(f[x]);

 25 }

 26 void Union(int x, int y)

 27 {

 28     int fx = find(x), fy = find(y);

 29 

 30     if (fx == fy)

 31     {

 32         return ;

 33     }

 34 

 35     if (fx > fy)

 36     {

 37         swap(fx, fy);

 38     }

 39 

 40     f[fx] = fy;

 41 }

 42 

 43 bool BFS(int start)

 44 {

 45     while (!q.empty())

 46     {

 47         q.pop();

 48     }

 49 

 50     q.push(start);

 51 

 52     visit[start] = true;

 53 

 54     while (!q.empty())

 55     {

 56         int now, next;

 57         now = q.front();

 58         q.pop();

 59 

 60         for (int i = 0; i < G[now].size(); i++)

 61         {

 62             next = G[now][i];

 63 

 64             //printf("start = %d, next = %d\n", start, next);

 65             if (visit[next])

 66             {

 67                 continue;

 68             }

 69 

 70             visit[next] = true;

 71             //如果是传感器不会入队列,但是要标记为 访问过

 72             if (sen[next])

 73             {

 74                 continue;

 75             }

 76             q.push(next);

 77         }

 78     }

 79 

 80     return false;

 81 }

 82 

 83 bool slove()

 84 {

 85     if(K == 0) return true;

 86     if(K != L) return false;

 87 

 88     for (int i = 0; i < L - 1; i++)

 89     {

 90         BFS(pass[i]);

 91         if(!visit[pass[i+1]]) return false;

 92     }

 93     BFS(pass[L-1]);

 94 

 95     for(int i = 1; i <= N; i++) //检查所有点是否都被访问了

 96         if(!visit[i])

 97             return false;

 98     return true;

 99 

100 }

101 int main()

102 {

103     int t;

104     scanf("%d", &t);

105 

106     while (t--)

107     {

108         memset(sen, false, sizeof(sen));

109         memset(visit, false, sizeof(visit));

110 

111         int x, y, f = 0;

112         scanf("%d %d %d", &N, &M, &K);

113 

114         for(int i = 1; i <= N; i++)

115             G[i].clear(); //千万不要忘记清空啊

116 

117         for (int i = 0; i < K; i++)

118         {

119             scanf("%d", &x);

120             sen[x] = true;

121         }

122 

123         for (int i = 0; i < M; i++)

124         {

125             scanf("%d %d", &x, &y);

126             G[x].push_back(y);

127             G[y].push_back(x);

128         }

129 

130         scanf("%d", &L);

131 

132         for (int i = 0; i < L; i++)

133         {

134             scanf("%d", &pass[i]);

135         }

136 

137 

138         if (!slove())

139         {

140             puts("No");

141         }

142         else

143         {

144             puts("Yes");

145         }

146     }

147 }
View Code

 

你可能感兴趣的:(ZOJ)