[BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)

传送门

告诉你一些点不能到达1,由于是双向边,也就是1不能到达那些点

那么从1开始dfs,如果当前点能到达不能到达的点,那么当前点就是损坏的。

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100001

int n, m, p, cnt, tot;
int head[N], to[N << 1], next[N << 1];
bool vis[N], flag[N];

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

inline void add(int x, int y)
{
	to[cnt] = y;
	next[cnt] = head[x];
	head[x] = cnt++;
}

inline void dfs(int u)
{
	int i, v;
	vis[u] = 1;
	for(i = head[u]; i ^ -1; i = next[i])
	{
		v = to[i];
		if(flag[v]) return;
	}
	for(i = head[u]; i ^ -1; i = next[i])
	{
		v = to[i];
		if(!vis[v]) dfs(v);
	}
	tot++;
}

int main()
{
	int i, x, y;
	n = read();
	m = read();
	p = read();
	memset(head, -1, sizeof(head));
	for(i = 1; i <= m; i++)
	{
		x = read();
		y = read();
		add(x, y);
		add(y, x);
	}
	for(i = 1; i <= p; i++)
	{
		x = read();
		flag[x] = 1;
	}
	dfs(1);
	printf("%d
", n - tot);
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhenghaotian/p/7508244.html