1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef __int128 int128;
namespace FastOI { char buf[1 << 20], *p1, *p2; #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++) template<typename T> inline T read(T& x) { x = 0; int f = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x *= f; return x; } template<typename T, typename... Args> inline void read(T& x, Args &...x_) { read(x); read(x_...); return; } inline ll read() { ll x; read(x); return x; } inline void read(char *s) { scanf("%s", s + 1); } }; using namespace FastOI;
const int N = 1e5 + 10;
struct Edge { int to, nt; Edge() {} Edge(int to, int nt) : to(to), nt(nt) {} }e[N << 1]; int hd[N], cnte; void AddEdge(int u, int v) { e[++cnte] = Edge(v, hd[u]); hd[u] = cnte; }
struct Land { ll a, b, c; }a[N]; int n;
int fa[N];
int p[N], t[N]; int vis[N];
void Input() { read(n); for(int i = 1; i <= n; i++) { read(a[i].a, a[i].b, a[i].c); } int u, v; for(int i = 1; i < n; i++) { read(u, v); AddEdge(u, v); AddEdge(v, u); } }
void Dfs(int u, int f) { fa[u] = f; for(int i = hd[u]; i; i = e[i].nt) { if(e[i].to == f) continue; Dfs(e[i].to, u); } }
int128 Calc(int128 a, int128 b, int128 c) { ll x = max((int128)1, min(a + 1, (int128)ceil(1.0 * (1 - b) / c))); if(c < 0) return (x - 1) * b + x * (x - 1) / 2 * c + (a - x + 1); else if(c == 0) return a * b; else return (x - 1) + (a - x + 1) * b + (a * (a + 1) / 2 - x * (x - 1) / 2) * c; }
int update(int u) { if(vis[u]) return 0; vis[u] = 1; if(fa[u]) return update(fa[u]) + 1; return 1; }
bool Check(ll x) { for(int i = 1; i <= n; i++) { vis[i] = 0; int128 now = Calc(x, a[i].b, a[i].c); if (now < a[i].a) return false; int l = 1, r = n, best = -1; while(l <= r) { int mid = l + r >> 1; if(now - Calc(mid - 1, a[i].b, a[i].c) >= a[i].a) { l = mid + 1; best = mid; } else { r = mid - 1; } } p[i] = i, t[i] = best, vis[i] = 0; } sort(p + 1, p + n + 1, [](int A, int B) {return t[A] < t[B];}); int cnt = 0; for(int i = 1; i <= n; i++) { cnt += update(p[i]); if(cnt > t[p[i]]) return false; } return true; }
void Work() { Dfs(1, 0); ll l = n, r = 1e9, best = -1; while(l <= r) { ll mid = l + r >> 1; if(Check(mid)) { r = mid - 1; best = mid; } else { l = mid + 1; } } printf("%lld", best); }
int main() { int T = 1; while(T--) { Input(); Work(); } return 0; }
|