NOIP 2003 Infectious Disease Control

Ideas: 1.dfs again, size is determined for each point, fa, deep;

2. Each point deep into the vector;

3. dffs solved in accordance with the answer deep;

When you disconnect a point when dffs connection with fa indicates that the marked point mark, it means no infection, and subtracting the point size, each to the next level, sweep fa, fa, if marked, then the point is also mark.

Dffs final status of the search to the end than the deepest point of the deep layer is a deeper layer, or a layer have been found to have been flagged.

Record dffs answer a minimum output

Complete AC Code

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n,m,ans;
struct node{
    int to,nxt;
}e[805];
int head[305],cnt,fa[305],siz[305],deep[305],madep,in[305];
vector<int> k[305];
inline void add(int from,int to){
    e[++cnt]=(node){to,head[from]};
    head[from]=cnt;
}
void dfs(int x,int f,int dep){
    fa[x]=f;siz[x]=1;deep[x]=dep;
    madep=max(madep,dep);
    for(int i=head[x];i;i=e[i].nxt)
        if(e[i].to!=f){
            dfs(e[i].to,x,dep+1);
            siz[x]+=siz[e[i].to];
        }
}
void dffs(int dep,int now){
    if(dep==madep+1){
        ans=min(ans,now);
        return ;
    }
    for(int i=0;i<k[dep].size();++i)
        if(in[fa[k[dep][i]]])
            in[k[dep][i]]=1;
        else in[k[dep][i]]=0;
    bool f=1;
    for(int i=0;i<k[dep].size();++i)
        if(!in[k[dep][i]])f=0;
    if(f){
        ans=min(ans,now);
        return ;
    }
    for(int i=0;i<k[dep].size();++i){
        if(in[k[dep][i]])continue;
        in[k[dep][i]]=1;
        dffs(dep+1,now-siz[k[dep][i]]);
        in[k[dep][i]]=0;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;++i){
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    dfs(1,0,1);
    for(int i=1;i<=n;++i)
        k[deep[i]].push_back(i);
    ans=n;
    dffs(2,n);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/sanjinliushi/p/11517514.html