ZOJ 3811 Untrusted Patrol

题意:

一幅图某些点有监视器  监视器只记录第一次路过的时间  问  给出路线序列  是否存在满足序列的情况下遍历整幅图的点

思路:

不要想割点  割点无法处理在一个双连通分量内的多个监视器  这题就是贪心+搜索

贪心就是尽量多的使用不违背序列的点  那么我们先把序列里的第一个点和不存在监视器的点加入图  并将他们连边  对于其他的序列中的点  如果这个点存在与第一个点所形成的连通块连接的边  那么就加入这个点  否则没有答案

这里的加入点其实就是一个染色的过程  所以用搜索来搞一下

代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long LL;
#define N 100010
#define M 400010
#define inf 2147483647

int n, m, Q, q, tot, ans;
int order[N], have[N], vis[N], head[N];
struct edge {
	int u, v, next;
	bool flag;
} ed[M];

void init() {
	tot = 0;
	ans = 1;
	memset(have, 0, sizeof(have));
	memset(vis, 0, sizeof(vis));
	memset(head, -1, sizeof(head));
}

void add(int u, int v) {
	ed[tot].u = u;
	ed[tot].v = v;
	ed[tot].flag = false;
	ed[tot].next = head[u];
	head[u] = tot++;
}

void dfs(int u) {
	vis[u] = 1;
	for (int i = head[u]; ~i; i = ed[i].next) {
		int v = ed[i].v;
		if (!vis[v] && !have[v])
			dfs(v);
	}
}

int main() {
	int cas, i, u, v;
	scanf("%d", &cas);
	while (cas--) {
		init();
		scanf("%d%d%d", &n, &m, &Q);
		for (i = 1; i <= Q; i++) {
			scanf("%d", &u);
			have[u] = 1;
		}
		for (i = 1; i <= m; i++) {
			scanf("%d%d", &u, &v);
			add(u, v);
			add(v, u);
		}
		scanf("%d", &q);
		for (i = 1; i <= q; i++)
			scanf("%d", &order[i]);
		if (Q != q)
			ans = 0;
		dfs(order[1]);
		for (i = 2; i <= q; i++) {
			v = 0;
			for (u = head[order[i]]; ~u; u = ed[u].next) {
				if (vis[ed[u].v]) {
					v = 1;
					break;
				}
			}
			if (!v) {
				ans = 0;
				break;
			}
			dfs(order[i]);
		}
		for (i = 1; i <= n; i++) {
			if (!vis[i]) {
				ans = 0;
				break;
			}
		}
		if (ans)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}
时间: 2024-10-17 21:16:00

ZOJ 3811 Untrusted Patrol的相关文章

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 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 并查集+邻接表,注意所有点都要走过

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

ZOJ 3811 Untrusted Patrol【并查集】

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

ZOJ 3811 Untrusted Patrol bfs+并查集

题目链接:点击打开链接 题意: 给定n个点m条边的无向图,k个触发器. 下面k个数表示触发器安装在哪几个点. 下面m行给出边 最后有l个信号, 给出信号发出的触发器的顺序. 每个触发器只会发出一次信号,且一个点只有一个触发器. 有一个人在遍历图. 每经过一个点,那个点的触发器就会发出信号,问是否存在一种走法使得这个人遍历了所有点且触发器发出的信号顺序和给出的一样. 思路: 先把无触发器的点放到图里. 然后根据触发器的信号顺序把点依次加入图中,加入时只添加(与无触发器点相连的边) 然后判断这个点能

zoj 3811 Untrusted Patrol DFS SET

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

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