zoukankan      html  css  js  c++  java
  • light oj 1219 树上贪心

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <queue>
     5 #include <cstdio>
     6 #include <algorithm>
     7 #include <map>
     8 //#include <time.h>
     9 //#include <ext/pb_ds/assoc_container.hpp>
    10 //#include <ext/pb_ds/tree_policy.hpp>
    11 #define LL long long
    12 
    13 using namespace std;
    14 //using namespace __gnu_pbds;
    15 const int N = 1e4+10;
    16 int head[N],tot,cost[N],used[N];
    17 struct nodes
    18 {
    19     int to,next;
    20 } Edge[N*2];
    21 
    22 void init()
    23 {
    24     tot = 0;
    25     memset(head,-1,sizeof(head));
    26     memset(used,0,sizeof(used));
    27 }
    28 
    29 void add(int u,int v)
    30 {
    31     Edge[tot].to = v;
    32     Edge[tot].next = head[u];
    33     head[u] = tot++;
    34 }
    35 
    36 int sum;
    37 
    38 int dfs(int cur)
    39 {
    40     for(int i = head[cur]; i != -1; i = Edge[i].next)
    41     {
    42         int to = Edge[i].to;
    43         if(!used[to])
    44         {
    45             used[to] = 1;
    46             int aft = dfs(to);
    47             cost[cur] += aft;
    48             sum += abs(aft);
    49         }
    50     }
    51     int ans = cost[cur] - 1;
    52     cost[cur] = 1;
    53     return ans;
    54 }
    55 
    56 void solve()
    57 {
    58     int n;
    59     scanf("%d",&n);
    60     init();
    61     for(int i = 0; i < n; i++)
    62     {
    63         int th,m;
    64         scanf("%d",&th);
    65         scanf("%d %d",&cost[th],&m);
    66         for(int j = 0; j < m; j++)
    67         {
    68             int v;
    69             scanf("%d",&v);
    70             add(th,v);
    71             add(v,th);
    72         }
    73     }
    74     sum = 0;
    75     used[1] = 1;
    76     int ans = dfs(1);
    77 
    78     printf("%d
    ",sum);
    79 }
    80 
    81 int main(void)
    82 {
    83     int t,cnt = 0;
    84     scanf("%d",&t);
    85     while(t--)
    86     {
    87         printf("Case %d: ",++cnt);
    88         solve();
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    火狐下button标签子元素无法点击
    js里面的this指向
    (转载)http协议的Request Payload 和 Form Data 的区别
    (转载)http压缩 Content-Encoding: gzip
    函数的length属性
    Expires
    Etag 和 If-None-Match
    Mac下升级node到最新版本
    高级函数之函数绑定
    Java数据结构和算法day01 稀疏数组与队列
  • 原文地址:https://www.cnblogs.com/henserlinda/p/5744404.html
Copyright © 2011-2022 走看看