ZOJ3811 Untrusted Patrol

题意:给你一个可能不联通的图,其中有k个点有计时器,计时器只能记录第一次到的时间,最后有L个计时器返回时间。

解题思路:深搜,按顺序找,每次找到不是site + 1 的电且有计时器的点就结束并把这个点加入我们的搜索池里面,每次搜完以后再看site + 1 是不是在池子里面,如果使得额话又从这个点开始搜索

解题代码:

  1 // File Name: c.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年09月07日 星期日 13时56分37秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 #define maxn 100005
 26 using namespace std;
 27
 28 int n , m , kn ,ln;
 29 vector<int> mp[maxn];
 30 int visit[maxn];
 31 int k[maxn];
 32 int l[maxn];
 33 int hs[maxn];
 34 int ok ;
 35 int site;
 36 int sum ;
 37 void dfs(int s )
 38 {
 39   int num = mp[s].size();
 40   for(int i = 0;i < num;i ++)
 41   {
 42      if(!visit[mp[s][i]])
 43      {
 44          if(!k[mp[s][i]]){
 45             visit[mp[s][i]] = 1;
 46             sum ++ ;
 47             dfs(mp[s][i]);
 48          }
 49          else{
 50            if(l[mp[s][i]] == site + 1 )
 51            {
 52              visit[mp[s][i]] = 1;
 53              site ++ ;
 54              sum ++ ;
 55              dfs(mp[s][i]);
 56            }else if(l[mp[s][i]]){
 57              hs[l[mp[s][i]]] = mp[s][i];
 58          }
 59          }
 60      }
 61   }
 62   if(site == ln)
 63   {
 64      ok = 1;
 65      return ;
 66   }else{
 67     if(hs[site + 1])
 68     {
 69       visit[hs[site+1]] = 1;
 70       site ++ ;
 71       sum ++ ;
 72       dfs(hs[site]);
 73     }
 74   }
 75 }
 76 int main(){
 77     int t;
 78     scanf("%d",&t);
 79     while(t--)
 80     {
 81        scanf("%d %d %d",&n,&m,&kn);
 82        int a, b ;
 83        for(int i = 1;i <= n;i ++)
 84            mp[i].clear();
 85        memset(hs,0,sizeof(hs));
 86        memset(k,0,sizeof(k));
 87        memset(l,0,sizeof(l));
 88        memset(visit,0,sizeof(visit));
 89        int temp ;
 90        for(int i = 1;i <= kn ;i ++ )
 91        {
 92            scanf("%d",&temp);
 93            k[temp] = 1 ;
 94        }
 95        for(int i =    1 ;i <= m;i ++)
 96        {
 97            scanf("%d %d",&a,&b);
 98            mp[a].push_back(b);
 99            mp[b].push_back(a);
100        }
101        int be;
102        scanf("%d",&ln);
103        for(int i = 1;i <= ln ;i ++)
104        {
105           scanf("%d",&temp);
106           if(i == 1 )
107               be = temp;
108           l[temp] = i;
109        }
110        if(ln < kn)
111        {
112           printf("No\n");
113           continue;
114        }
115        ok  = 0 ;
116        site = 1;
117        sum =  1 ;
118        visit[be] = 1;
119        dfs(be);
120
121        if(ok && sum == n)
122            printf("Yes\n");
123        else printf("No\n");
124     }
125 return 0;
126 }

时间: 2024-08-03 06:49:30

ZOJ3811 Untrusted Patrol的相关文章

zoj3811 Untrusted Patrol (dfs)

2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large fact

ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题

n个点,m条双向边,k个传感器.其中有l个传感器记录到了第一次到达的时间顺序,求是否有可能检查了所有的顶点. 首先判断l,l<k一定是不行的.然后按照传感器的时间顺序dfs,先从第一个传感器位置搜索dfs搜到所有的到达传感器的位置结束,如果这个传感器被标记为可访问,则从这个传感器继续搜索下一层传感器,否则是不能满足时间顺序的.最后还要判断整个图的连通性,所有的顶点都要可以到达的. #include <iostream> #include <cstdio> #include &

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure t

ZOJ 3811 Untrusted Patrol dfs

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

zoj 3811 Untrusted Patrol(bfs或dfs)

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

ZOJ Problem Set - 3811 Untrusted Patrol

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

ZOJ 3811 Untrusted Patrol 并查集+邻接表,注意所有点都要走过

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

zoj 3818 Untrusted Patrol(dsf+并査集+邻接表)

ZOJ Problem Set - 3811 Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safet

ZOJ 3811 Untrusted Patrol The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

Description Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hired a security man to patrol the warehouse. The warehous