bzoj 1574: [Usaco2009 Jan] Earthquake Damage [dfs]

It's different from March's, it's just a very simple dfs with greed.
First, one point is cut off, and all the points adjacent to it will also be cut off. Mark it for deletion, and you can start from 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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325398059&siteId=291194637