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\n",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 }

 

posted on 2016-08-06 16:49  鱼泪儿  阅读(123)  评论(0编辑  收藏  举报

导航