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

n个点,m条双向边,k个传感器。其中有l个传感器记录到了第一次到达的时间顺序,求是否有可能检查了所有的顶点。

首先判断l,l<k一定是不行的。然后按照传感器的时间顺序dfs,先从第一个传感器位置搜索dfs搜到所有的到达传感器的位置结束,如果这个传感器被标记为可访问,则从这个传感器继续搜索下一层传感器,否则是不能满足时间顺序的。最后还要判断整个图的连通性,所有的顶点都要可以到达的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn=110000;
int head[maxn];
int visit[maxn];
int pile[maxn];
int n,m,k,l;
int num;
int s;
struct Edge
{
	int u,v;
	int next;
}edge[maxn*4];
void addedge(int u,int v)
{
	edge[num].u=u;
	edge[num].v=v;
	edge[num].next=head[u];
	head[u]=num++;
	edge[num].u=v;
	edge[num].v=u;
	edge[num].next=head[v];
	head[v]=num++;
}
void dfs(int u)
{
	visit[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(visit[v])
		{
			continue;
		}
		if(pile[v])
		{
			visit[v]=1;
			continue;
		}
		dfs(v);
	}
}
void dfs1(int u)
{
	visit[u]=1;
	s++;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!visit[v])
		{
			dfs1(v);
		}
	}
}
int main()
{
	int t;
	int flag;
	int u,v;
	scanf("%d",&t);
	while(t--)
	{
		flag=1;
		s=0;
		num=0;
		memset(head,-1,sizeof(head));
		memset(visit,0,sizeof(visit));
		memset(pile,0,sizeof(pile));
		scanf("%d%d%d",&n,&m,&k);
		for(int i=0;i<k;i++)
		{
			scanf("%d",&u);
			pile[u]=1;
		}
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&u,&v);
			addedge(u,v);
		}
		scanf("%d",&l);
		if(l<k)
		{
			flag=0;
		}
		scanf("%d",&u);
		dfs(u);
		for(int i=1;i<l;i++)
		{
			scanf("%d",&u);
			if(!visit[u])
			{
				flag=0;
			}
			if(flag)
			{
				dfs(u);
			}
		}
		memset(visit,0,sizeof(visit));
		dfs1(1);
		if(s<n)
		{
			flag=0;
		}
		if(flag)
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}
时间: 2024-12-20 09:25:19

ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题的相关文章

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 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

zoj 3811 Untrusted Patrol DFS SET

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343 当时是一个学弟过的,真心没想出来,回想起来其实可能有点后悔做ACM了,确实智商不够...... 11去牡丹江比赛,如果悲剧,ACM生涯就彻底悲剧了,尽量出结果......啥不说,专心刷题 此题还是参考了答案,,, 题目要求:按照次序访问某些点,如果能满足而且能遍历全图,输出yes否则no 学到: 1.是不是能按照规定次序,那么就这么看,按照规定次序,DFS第一个点,过程

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

zoj 3827 Information Entropy(2014牡丹江区域赛I题)

Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy. Entropy is t

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 3811 Untrusted Patrol【并查集】

题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发,此时JYB用BFS秒了,索性最后还是调出来了,今天自己写了下,感觉唯一的坑点就是需要遍历完所有的点 //zoj3811 #include <stdio.h> #include <string.h> #include <algorithm> #include <queu

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 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&g