defdfs(u, fa): global ans p = 1# need q = marbles[u] # have for v in g[u]: if v == fa: continue p2, q2 = dfs(v, u) p += p2 q += q2 ans += abs(p - q) return p, q
whileTrue: n = int(input()) if n == 0: break g = [[] for _ inrange(n + 1)] marbles = [0] * (n + 1)
for _ inrange(n): u, m, d, *nodes = map(int, input().split()) marbles[u] = m for v in nodes: # 建立無向樹 g[u].append(v) g[v].append(u) ans = 0 dfs(1, 0) # 以任意點當作根節點皆可 print(ans)
#include<bits/stdc++.h> usingnamespace std; using LL = longlong; constint N = 1e4 + 5; #define endl '\n'
int ans; vector<vector<int>> g(N); vector<int> marbles(N);
pair<int, int> dfs(int u, int fa){ int p = 1, q = marbles[u]; // (need, have) for (int v : g[u]){ if (v == fa) continue; pair<int, int> res = dfs(v, u); p += res.first; q += res.second; } ans += abs(p - q); return {p, q}; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, d, u, v; while (cin >> n && n){ ans = 0; for (int i = 1; i <= n; ++i) g[i].clear(); for (int i = 1; i <= n; ++i){ cin >> u >> m >> d; marbles[u] = m; for (int j = 0; j < d; ++j){ cin >> v; g[u].push_back(v); g[v].push_back(u); } } dfs(1, 0); cout << ans << endl; } return0; }
defdfs(u, fa): # 正數表示多餘,負數表示缺少 global ans res = marbles[u] - 1 for v in g[u]: if v == fa: continue res_v = dfs(v, u) ans += abs(res_v) res += res_v return res
whileTrue: n = int(input()) if n == 0: break g = [[] for _ inrange(n + 1)] marbles = [0] * (n + 1)
for _ inrange(n): u, m, d, *nodes = map(int, input().split()) marbles[u] = m for v in nodes: # 建立無向樹 g[u].append(v) g[v].append(u) ans = 0 dfs(1, 0) # 以任意點當作根節點皆可 print(ans)
#include<bits/stdc++.h> usingnamespace std; using LL = longlong; constint N = 1e4 + 5; #define endl '\n'
int ans; vector<vector<int>> g(N); vector<int> marbles(N);
intdfs(int u, int fa){ // 正數表示多餘,負數表示缺少 int res = marbles[u] - 1; for (int v : g[u]){ if (v == fa) continue; int res_v = dfs(v, u); ans += abs(res_v); res += res_v; } return res; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, d, u, v; while (cin >> n && n){ ans = 0; for (int i = 1; i <= n; ++i) g[i].clear(); for (int i = 1; i <= n; ++i){ cin >> u >> m >> d; marbles[u] = m; for (int j = 0; j < d; ++j){ cin >> v; g[u].push_back(v); g[v].push_back(u); } } dfs(1, 0); cout << ans << endl; } return0; }