#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int n, head[N], cnt, S[N];
struct Edge {
int to, nxt, weight, price, delta;
} edges[N<<1];
LL dfs(int now, int pre) {
LL sum = 0;
for(int i = head[now]; i; i = edges[i].nxt)
if(edges[i].to != pre) {
LL res = dfs(edges[i].to, now);
if(res > edges[i].price) {
puts("-1");
exit(0);
}
edges[i].delta = min(edges[i].price - (int)res, edges[i].weight - 1);
sum += res + edges[i].weight - edges[i].delta;
}
return S[now] = sum;
}
LL dfs2(int now, int pre, LL lim) {
LL res = 0;
for(int i = head[now]; i; i = edges[i].nxt)
if(edges[i].to != pre) {
int add = min((LL)edges[i].delta, lim);
edges[i].delta -= add;
lim -= add;
res += add;
LL x = dfs2(edges[i].to, now, min(lim, (LL)edges[i].price - edges[i].delta - S[edges[i].to]));
res += x;
lim -= x;
}
return res;
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; ++i) {
int u, v, weight, price;
scanf("%d%d%d%d", &u, &v, &weight, &price);
edges[++cnt] = (Edge){v, head[u], weight, price};
head[u] = cnt;
edges[++cnt] = (Edge){u, head[v], weight, price};
head[v] = cnt;
}
dfs(1, 0);
printf("%d\n", n);
dfs2(1, 0, 1e18);
for(int i = 1; i <= cnt; i += 2)
printf("%d %d %d %d\n", edges[i+1].to, edges[i].to, edges[i].weight - edges[i].delta, edges[i].price - edges[i].delta); // Переименовал e в edges
return 0;
}/*1695073795.3099775*/
206. Reverse Linked List | 83. Remove Duplicates from Sorted List |
116. Populating Next Right Pointers in Each Node | 145. Binary Tree Postorder Traversal |
94. Binary Tree Inorder Traversal | 101. Symmetric Tree |
77. Combinations | 46. Permutations |
226. Invert Binary Tree | 112. Path Sum |
1556A - A Variety of Operations | 136. Single Number |
169. Majority Element | 119. Pascal's Triangle II |
409. Longest Palindrome | 1574A - Regular Bracket Sequences |
1574B - Combinatorics Homework | 1567A - Domino Disaster |
1593A - Elections | 1607A - Linear Keyboard |
EQUALCOIN Equal Coins | XOREQN Xor Equation |
MAKEPAL Weird Palindrome Making | HILLSEQ Hill Sequence |
MAXBRIDGE Maximise the bridges | WLDRPL Wildcard Replacement |
1221. Split a String in Balanced Strings | 1002. Find Common Characters |
1602A - Two Subsequences | 1555A - PizzaForces |