zoukankan      html  css  js  c++  java
  • P1041 传染病控制

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 301;
    std::vector<int> son[maxn];
    vector<int> G[maxn];
    int deep[maxn], size[maxn];
    vector<int> dep[maxn];
    int vis[maxn];
    int n, p, ans = 0x3f3f3f;
    inline void dfs(int f) {
      vector<int>::iterator it;
      size[f] = 1;
      for (it = son[f].begin(); it != son[f].end(); it++) {
        int x = *it;
        deep[x] = deep[f] + 1;
        dfs(x);
        size[f] += size[*it];
      }
    }
    inline void hit(int root, int state) {
      vector<int>::iterator it;
      if (state == 1)
        vis[root] = 1;
      else
        vis[root] = 0;
      for (it = son[root].begin(); it != son[root].end(); it++) {
        hit(*it, state);
      }
    }
    inline void search(int d, int now) {
      ans = min(ans, now);
      vector<int>::iterator it;
      for (it = dep[d].begin(); it != dep[d].end(); it++) {
        if (!vis[*it]) {
          hit(*it, 1);
          search(d + 1, now - size[*it]);
          hit(*it, 0);
        }
      }
    }
    int v[maxn];
    inline void trans(int root) {
      v[root] = 1;
      vector<int>::iterator it;
      for (it = G[root].begin(); it != G[root].end(); it++) {
        if (!v[*it]) {
          son[root].push_back(*it);
          trans(*it);
        }
      }
    }
    inline int read() {
      int x = 0, f = 1;
      char ch = getchar();
      while (ch < '0' || ch > '9') {
        if (ch == '-')
          f = -1;
        ch = getchar();
      }
      while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
      }
      return x * f;
    }
    int main() {
    //  freopen("input.in", "r", stdin);
      cin >> n >> p;
      for (int i = 1; i <= p; i++) {
        int x, y;
        x = read();
        y = read();
        G[x].push_back(y);
        G[y].push_back(x);
      }
      memset(v, 0, sizeof(v));
      trans(1);
      deep[1] = 0;
      dfs(1);
      for (int i = 1; i <= n; i++) {
        dep[deep[i]].push_back(i);
      }
      search(1, n);
      printf("%d
    ", ans);
      return 0;
    }
    
  • 相关阅读:
    C# 抽象方法和虚方法的区别
    xmlhttprequest readyState 属性的五种状态
    ServiceStack破解文件
    k8s部署mysql
    docker 开放2376端口的问题
    .net core 发布到IIS 没有 web.config 文件
    1064
    docker mysql 主从同步配置
    Docker 鼠标在虚拟机与主机之间自由切换
    Socket原理解析2
  • 原文地址:https://www.cnblogs.com/gengchen/p/6071201.html
Copyright © 2011-2022 走看看