• CodeForces 219D Choosing Capital for Treeland (树形DP)


    题意:给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少。

    析:很明显的树形DP,两次dfs,对于边,进行处理,如果是正向就是1,反向是-1,先进行dfs,计算出向子结点的方向要反转几条边,然后再第二次考虑,从父结点和子结点考虑。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int maxn = 200000 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    struct Edge{
      int to, next, val;
    };
    Edge edge[maxn<<1];
    int head[maxn], cnt;
    int in[maxn];
    int ans[maxn], res;
    int dp[maxn];
    
    void add_edge(int u, int v, int val){
      edge[cnt].to = v;
      edge[cnt].val = val;
      edge[cnt].next = head[u];
      head[u] = cnt++;
    }
    
    void dfs1(int u, int fa){
      dp[u] = 0;
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == fa)  continue;
        dfs1(v, u);
        if(edge[i].val == 1)  dp[u] += dp[v];
        else dp[u] += dp[v] + 1;
      }
    }
    
    void dfs2(int u, int fa, int fv){
      ans[u] = dp[u] + fv;
      res = min(res, ans[u]);
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == fa)  continue;
        dfs2(v, u, fv+dp[u]-dp[v]+edge[i].val);
      }
    }
    
    int main(){
      scanf("%d", &n);
      memset(head, -1, sizeof head);
      for(int i = 1; i < n; ++i){
        int u, v;
        scanf("%d %d", &u, &v);
        add_edge(u, v, 1);
        add_edge(v, u, -1);
      }
      dfs1(1, -1);
      res = n - 1;
      dfs2(1, -1, 0);
      printf("%d
    ", res);
      for(int i = 1; i <= n; ++i)
        if(res == ans[i])  printf("%d ", i);
      printf("
    ");
      return 0;
    }
    

      

  • 相关阅读:
    Vue+vue-i18n实现国际化(中英文切换)
    webpack性能优化之配置dll后npm run dev出错
    日期格式快速转时间戳,获取最近一个月等处理方法
    使用window.print()实现网页打印
    前端获取服务器时间
    vue项目中main.js引入全局scss文件时报错
    项目管理【71】 | 项目集管理
    项目管理【70】 | 流程管理
    项目管理【69】 | 组织级项目管理
    项目管理【68】 | 战略管理
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7353304.html
Copyright © 2020-2023  润新知