zoukankan      html  css  js  c++  java
  • bzoj1574[Usaco2009 Jan]地震损坏Damage*

    bzoj1574[Usaco2009 Jan]地震损坏Damage

    题意:

    n点m边无向图,知道p条信息ai,表示ai没被损坏但它和点1不联通(损坏的点不能通行),问有多少点和1联通(不包括损坏的点)。n≤30000,m≤100000。

    题解:

    有一个结论,最优删点方案应该是对每个信息ai,将所有ai的相邻点均设为损坏。在这之后DFS求联通性即可。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define maxn 30010
     7 using namespace std;
     8 
     9 inline int read(){
    10     char ch=getchar(); int f=1,x=0;
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    13     return f*x;
    14 }
    15 struct e{int t,n;}es[maxn*8]; int g[maxn],ess;
    16 void pe(int f,int t){es[++ess]=(e){t,g[f]}; g[f]=ess;}
    17 bool vis[maxn]; int n,m,p,ans;
    18 void dfs(int x){
    19     ans++; for(int i=g[x];i;i=es[i].n)if(!vis[es[i].t])vis[es[i].t]=1,dfs(es[i].t);
    20 }
    21 int main(){
    22     n=read(); m=read(); p=read(); inc(i,1,m){int x=read(),y=read(); pe(x,y); pe(y,x);}
    23     inc(i,1,p){int x=read(); for(int j=g[x];j;j=es[j].n)if(es[j].t!=x)vis[es[j].t]=1;}
    24     vis[1]=1; dfs(1); printf("%d",n-ans); return 0;
    25 }

    20160926

  • 相关阅读:
    HDU 3537
    POJ 1175
    POJ 1021 人品题
    POJ 2068
    POJ 2608
    POJ 2960
    poj 1635
    ustc 1117
    ural 1468
    数字游戏
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5910809.html
Copyright © 2011-2022 走看看