记录编号 123404 评测结果 AAAAAAAAAA
题目名称 [NOIP 2003]传染病控制 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.044 s
提交时间 2014-09-26 20:43:21 内存使用 0.33 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============

//==============var declaration=================
const int MAXN=310;
int n,p;
bool kill[MAXN];
bool TimeUp=false;
int depth[MAXN],ans=INF,maxT=0,son[MAXN];
vector <int> Edge[MAXN];
vector <int> gen[MAXN];
//==============function declaration============
void dfs(int x);
void control(int T);
void CountPatient();
void Kill(int x);
void Recovery(int x);
clock_t Start_Time=clock();
bool cmp(int a,int b){return son[a]>son[b];}
//==============main code=======================
int main()
{  
  string FileName="epidemic";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
  ios::sync_with_stdio(false); 
#ifdef DEBUG  
  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif    
  cin>>n>>p;
  For(i,1,p){
    int s,e;
    cin>>s>>e;
    Edge[s].push_back(e);
    Edge[e].push_back(s);
  }
  depth[1]=0;
  dfs(1);
  For(i,1,maxT)
    sort(gen[i].begin(),gen[i].end(),cmp);
  control(1);
  cout<<ans<<endl;
#ifdef DEBUG  
  clock_t End_Time=clock();
  printf("\n\nTime Used: %.4lf Ms\n",double(End_Time-Start_Time)/CLOCKS_PER_SEC);
#endif    
  return 0;
}
//================fuction code====================
void dfs(int x)
{
  int siz=Edge[x].size()-1;
  son[x]=1;
  gen[depth[x]].push_back(x);
  For(i,0,siz){
    int &e=Edge[x][i];
    if (depth[e]!=0) continue;
    if (e!=1){
      depth[e]=depth[x]+1;
      dfs(e);
      son[x]+=son[e];
    }
  }
  maxT=max(depth[x],maxT);
}
void control(int T)
{
  if (T>maxT){
    CountPatient();
    return;
  }
  int siz=gen[T].size()-1;
  bool suc=false;
  For(i,0,siz){
    int &e=gen[T][i];
    if(kill[e]) continue;
    Kill(e);
    suc=true;
    control(T+1);
    Recovery(e);
    clock_t End_Time=clock();
    if (double(End_Time-Start_Time)/CLOCKS_PER_SEC>=0.005)
      return ;
  }
  if (!suc)
    CountPatient();
}
void CountPatient()
{
  int cnt=0;
  queue <int> Q;
  Q.push(1);
  while (!Q.empty()){
    int x=Q.front();Q.pop();cnt++;
    int siz=Edge[x].size()-1;
    For(i,0,siz){
      int &e=Edge[x][i];
      if (depth[e]!=depth[x]+1) continue;
      if (kill[e]) continue;
      Q.push(e);
    }
  }
  if (cnt<ans)
    ans=cnt;
}
void Kill(int x)
{
  queue <int> Q;
  Q.push(x);
  while (!Q.empty()){
    int now=Q.front();Q.pop();
    kill[now]=true;
    int siz=Edge[now].size()-1;
    For(i,0,siz){
      int &e=Edge[now][i];
      if (depth[e]==depth[now]+1)
        Q.push(e);
    }
  }
}
void Recovery(int x)
{
  queue <int> Q;
  Q.push(x);
  while (!Q.empty()){
    int now=Q.front();Q.pop();
    kill[now]=false;
    int siz=Edge[now].size()-1;
    For(i,0,siz){
      int &e=Edge[now][i];
      if (depth[e]==depth[now]+1)
      Q.push(e);
    }
  }
}