bzoj 1574: [Usaco2009 Jan]地震损坏Damage【dfs】

和March的那道不一样,只是非常单纯的带着贪心的dfs
首先一个点被隔断,与它相邻的所有点也会被隔断,打上删除标记,从1dfs即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=30005,M=200005;
int n,m,q,h[N],cnt,ans;
bool d[N],v[N];
struct qwe
{
    int ne,to;
}e[M];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
void add(int u,int v)
{
    cnt++;
    e[cnt].ne=h[u];
    e[cnt].to=v;
    h[u]=cnt;
}
void dfs(int u)
{
    v[u]=1;ans++;
    for(int i=h[u];i;i=e[i].ne)
        if(!d[e[i].to]&&!v[e[i].to])
            dfs(e[i].to);
}
int main()
{
    n=read(),m=read(),q=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        if(x!=y)
            add(x,y),add(y,x);
    }
    while(q--)
    {
        int u=read();
        for(int i=h[u];i;i=e[i].ne)
            d[e[i].to]=1;
    }
    dfs(1);
    printf("%d\n",n-ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lokiii/p/8997306.html
今日推荐