您的位置:首页 > 其它

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

2017-09-12 08:00 441 查看

传送门

 

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

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

 

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

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

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", n - tot);
return 0;
}

  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: