P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 爆搜

这题怎么这么水~~~本来以为挺难的一道题,结果随便一写就过了。。。本来还不知道损坏的牛棚算不算,结果不明不白就过了。。。

题干:

农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有牛棚间的路经都还能使用. FJ的农场有P(1 <= P <= 30,000)个牛棚,编号1..P. C(1 <= C <= 100,000)条双向路经联接这些牛棚,编号为1..C. 路经i连接牛棚a_i和b_i (1 <= a_i<= P;1 <= b_i <= P).路经可能连接a_i到它自己,两个牛棚之间可能有多条路经.农庄在编号为1的牛棚. N (1 <= N <= P)头在不同牛棚的牛通过手机短信report_j(2 <= report_j <= P)告诉FJ它们的牛棚(report_j)没有损坏,但是它们无法通过路经和没有损坏的牛棚回到到农场. 当FJ接到所有短信之后,找出最小的不可能回到农庄的牛棚数目.这个数目包括损坏的牛棚. 注意:前50次提交将提供在一些测试数据上的运行结果.
输入输出格式
输入格式:

* Line 1: Three space-separated integers: P, C, and N

* Lines 2..C+1: Line i+1 describes cowpath i with two integers: a_i and b_i

* Lines C+2..C+N+1: Line C+1+j contains a single integer: report_j

输出格式:

* Line 1: A single integer that is the minimum count of pastures from which a cow can not return to the barn (including the damaged pastures themselves)

输入输出样例
输入样例#1: 复制

4 3 1 
1 2 
2 3 
3 4 
3 

输出样例#1: 复制

3 

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int p,c,n,lst[60005],len = 0;
struct node
{
    int l,r,nxt;
}a[200005];
void add(int x,int y)
{
    a[++len].l = x;
    a[len].r = y;
    a[len].nxt = lst[x];
    lst[x] = len;
//    cout<<len<<" "<<a[len].l<<" "<<a[len].r<<endl;
}
int vis[60005];
void dfs(int x)
{
    if(vis[x] == -1 || vis[x] == 1)
    return;
    vis[x] = 1;
    for(int k = lst[x];k;k = a[k].nxt)
    {
        int y = a[k].r;
        if(vis[y] == 0)
        dfs(y);
    }
}
int main()
{
    clean(vis); 
    read(p);read(c);read(n);
    duke(i,1,c)
    {
        int g,h;
        read(g);
        read(h);
        add(g,h);
        add(h,g);
    }
    duke(i,1,n)
    {
        int k;
        read(k);
        vis[k] = -1;
        for(int j = lst[k];j;j = a[j].nxt)
        {
            int y = a[j].r;
            vis[y] = -1;
        }
    }
    dfs(1);
    int tot = 0;
    duke(i,1,p)
    {
        if(vis[i] == 0 || vis[i] == -1)
            tot++;
    }
    write(tot);
    return 0;
}
原文地址:https://www.cnblogs.com/DukeLv/p/9535046.html