博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
阅读量:6690 次
发布时间:2019-06-25

本文共 7473 字,大约阅读时间需要 24 分钟。

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 hired a security man to patrol the warehouse. The warehouse has N piles of drinks and M passageways connected them (warehouse is not big enough). When the evening comes, the security man will start to patrol the warehouse following a path to check all piles of drinks.

Unfortunately, Edward is a suspicious man, so he sets sensors on K piles of the drinks. When the security man comes to check the drinks, the sensor will record a message. Because of the memory limit, the sensors can only record for the first time of the security man's visit.

After a peaceful evening, Edward gathered all messages ordered by recording time. He wants to know whether is possible that the security man has checked all piles of drinks. Can you help him?

The security man may start to patrol at any piles of drinks. It is guaranteed that the sensors work properly. However, Edward thinks the security man may not works as expected. For example, he may digs through walls, climb over piles, use some black magic to teleport to anywhere and so on. 

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

The first line contains three integers N (1 <= N <= 100000), M (1 <= M <= 200000) and K (1 <= K <= N).

The next line contains K distinct integers indicating the indexes of piles (1-based) that have sensors installed. The following M lines, each line contains two integers Ai and Bi (1 <= Ai, Bi <= N) which indicates a bidirectional passageway connects piles Ai and Bi.

Then, there is an integer L (1 <= L <= K) indicating the number of messages gathered from all sensors. The next line contains L distinct integers. These are the indexes of piles where the messages came from (each is among the K integers above), ordered by recording time.

Output

For each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not.

Sample Input

25 5 31 2 41 22 33 11 44 534 2 15 5 31 2 41 22 33 11 44 534 1 2

Sample Output

NoYes 转自:

题目:

给定一个无向图,n个点,  m条边,k个特殊点(有传感器),只有当第一次到达特殊点的时候才会发出信号,给出发出信号的序列,问是否存在这样的路径使得每个点至少遍历一次,而且特殊点第一次到达的顺序和和题目输入一样。  (1 <= N <= 100000), M (1 <= M <= 200000)  先特判 如果询问时输入的L<k, 那么直接No, 因为l<k肯定有传感器的点没有到达,不满足每个点都遍历一次。 先把第一个特殊点入队,遍历所有的可以到达的点(中途不经过其他特殊点),标记为可以到达。  之后判断第二个点是否可以从第一个点到达(是否被标记),如果不可以则No,否则遍历从第二个特殊点出发的可以到达的点(同样中途不经过剩余特殊点). ....... 依次处理完所有点即可. 如果前面的一个特殊点可以到达某个点,那么 他后面的特殊点一定也可以到达这些点,因为他可以回到前面的特殊点再走过去.

 

本题可用多种思路做,但是拍代码前一定要先分析好再开拍,不然,,,

可用方法:bfs、dfs、并查集。

吐血ac。。。

 

代码:

bfs:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 10 #define N 100005 11 #define M 15 12 #define mod 6 13 #define mod2 100000000 14 #define ll long long 15 #define maxi(a,b) (a)>(b)? (a) : (b) 16 #define mini(a,b) (a)<(b)? (a) : (b) 17 18 using namespace std; 19 20 int T; 21 int n,m,k,l; 22 int vis[N]; 23 int vis2[N]; 24 vector
bian[N]; 25 int re[N]; 26 int a,b; 27 vector
::iterator it; 28 29 void ini() 30 { 31 int i; 32 memset(vis,0,sizeof(vis)); 33 memset(vis2,0,sizeof(vis2)); 34 for(i=1;i<=100003;i++){ 35 bian[i].clear(); 36 } 37 scanf("%d%d%d",&n,&m,&k); 38 for(i=1;i<=k;i++){ 39 scanf("%d",&re[i]); 40 } 41 while(m--){ 42 scanf("%d%d",&a,&b); 43 bian[a].push_back(b); 44 bian[b].push_back(a); 45 } 46 scanf("%d",&l); 47 for(i=1;i<=l;i++){ 48 scanf("%d",&re[i]); 49 vis2[ re[i] ]=1; 50 } 51 } 52 53 int solve() 54 { 55 if(l!=k){ 56 return 0; 57 } 58 queue
q; 59 int r; 60 vis[ re[1] ]=1; 61 for(int i=1;i<=l;i++){ 62 // printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]); 63 if(vis[ re[i] ]==0) return 0; 64 // vis2[ re[i] ]=0; 65 q.push(re[i]); 66 while( q.size()>0 ) 67 { 68 r=q.front(); 69 //printf(" r=%d\n",r); 70 q.pop(); 71 // vis[r]=1; 72 73 for(it=bian[r].begin();it!=bian[r].end();it++){ 74 // printf(" it=%d\n",*it); 75 if(vis[*it]==1) continue; 76 vis[ *it ]=1; 77 if( vis2[ *it ]==0 ){ 78 q.push( (*it) ); 79 } 80 } 81 /* 82 for(int j=0;j<(int)bian[r].size();j++){ 83 int y=bian[r][j]; 84 if(vis[y]==1) continue; 85 vis[y]=1; 86 if(vis2[y]==0){ 87 q.push(y); } 88 }*/ 89 } 90 } 91 92 for(int i=1;i<=n;i++){ 93 if(vis[i]==0) return 0; 94 } 95 return 1; 96 } 97 98 99 int main()100 {101 //freopen("data.in","r",stdin);102 scanf("%d",&T);103 for(int cnt=1;cnt<=T;cnt++)104 // while(T--)105 //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)106 {107 ini();108 if(solve()==0){109 printf("No\n");110 }111 else{112 printf("Yes\n");113 }114 }115 116 return 0;117 }

 

 

dfs版本:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 10 #define N 100005 11 #define M 15 12 #define mod 6 13 #define mod2 100000000 14 #define ll long long 15 #define maxi(a,b) (a)>(b)? (a) : (b) 16 #define mini(a,b) (a)<(b)? (a) : (b) 17 18 using namespace std; 19 20 int T; 21 int n,m,k,l; 22 int vis[N]; 23 int vis2[N]; 24 vector
bian[N]; 25 int re[N]; 26 int a,b; 27 28 void ini() 29 { 30 int i; 31 memset(vis,0,sizeof(vis)); 32 memset(vis2,0,sizeof(vis2)); 33 for(i=1;i<=100003;i++){ 34 bian[i].clear(); 35 } 36 scanf("%d%d%d",&n,&m,&k); 37 for(i=1;i<=k;i++){ 38 scanf("%d",&re[i]); 39 } 40 while(m--){ 41 scanf("%d%d",&a,&b); 42 bian[a].push_back(b); 43 bian[b].push_back(a); 44 } 45 scanf("%d",&l); 46 for(i=1;i<=l;i++){ 47 scanf("%d",&re[i]); 48 vis2[ re[i] ]=1; 49 } 50 } 51 52 void dfs(int s) 53 { 54 vector
::iterator it; 55 for(it=bian[s].begin();it!=bian[s].end();it++){ 56 if(vis[*it]==1) continue; 57 vis[*it]=1; 58 if(vis2[*it]==0){ 59 dfs(*it); 60 } 61 } 62 } 63 64 int solve() 65 { 66 if(l!=k){ 67 return 0; 68 } 69 vis[ re[1] ]=1; 70 for(int i=1;i<=l;i++){ 71 // printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]); 72 if(vis[ re[i] ]==0) return 0; 73 dfs(re[i]); 74 } 75 76 for(int i=1;i<=n;i++){ 77 if(vis[i]==0) return 0; 78 } 79 return 1; 80 } 81 82 83 int main() 84 { 85 //freopen("data.in","r",stdin); 86 scanf("%d",&T); 87 for(int cnt=1;cnt<=T;cnt++) 88 // while(T--) 89 //while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF) 90 { 91 ini(); 92 if(solve()==0){ 93 printf("No\n"); 94 } 95 else{ 96 printf("Yes\n"); 97 } 98 } 99 100 return 0;101 }

 

 

并查集版:

转自:

思路:

先把无触发器的点放到图里。

然后根据触发器的信号顺序把点依次加入图中,加入时只添加(与无触发器点相连的边)

然后判断这个点能否与之前的图连通。bfs+并查集判断。

==其实并查集就够了,写的时候有脑洞,bfs就冒出来了

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 10 #define N 100005 11 #define M 15 12 #define mod 6 13 #define mod2 100000000 14 #define ll long long 15 #define maxi(a,b) (a)>(b)? (a) : (b) 16 #define mini(a,b) (a)<(b)? (a) : (b) 17 18 using namespace std; 19 20 int T; 21 int n,m,k,l; 22 int f[N]; 23 int rank[N]; 24 int vis2[N]; 25 vector
bian[N]; 26 int re[N]; 27 vector
::iterator it; 28 29 int find(int v) 30 { 31 //printf(" v=%d f=%d\n",v,f[v]); 32 return f[v]=f[v]==v ? v : find(f[v]); 33 } 34 35 void merge(int x,int y) 36 { 37 int a=find(x),b=find(y); 38 if(a==b) return; 39 if(rank[a]

 

转载于:https://www.cnblogs.com/njczy2010/p/3961102.html

你可能感兴趣的文章
网络组相关实验
查看>>
《Linux菜鸟入门2》Vsftpd
查看>>
大数据就业前景怎么样?
查看>>
Python应用领域详解
查看>>
批量替换文件内容
查看>>
LVS介绍
查看>>
格式化磁盘
查看>>
Linux 下内网流量控制工具
查看>>
KVM基本功能管理与使用
查看>>
胖AP与瘦AP的区别以及胖瘦AP组网的优劣对比
查看>>
复习sed的相关内容
查看>>
NetScaler OTP双因子身份认证登录演示
查看>>
centos系统目录结构
查看>>
python Class:面向对象高级编程 __getattr__
查看>>
思科dhcp配置思路
查看>>
“中国制造2025”+云计算,制造业转型的新可能
查看>>
JavaScript基础(一)
查看>>
python爬取QQ说说并生成词云图,回忆满满
查看>>
psutil
查看>>
如何过滤出已知当前目录下oldboy中的所有一级目录
查看>>