#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //#include // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i) #define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i) #define Sort(v) sort(v.begin(), v.end()) #define rSort(v) sort(v.rbegin(), v.rend()) #define Reverse(v) reverse(v.begin(), v.end()) #define Lower_bound(v, y) \ distance(v.begin(), lower_bound(v.begin(), v.end(), y)) #define Upper_bound(v, y) \ distance(v.begin(), upper_bound(v.begin(), v.end(), y)) #define pb push_back using ll = long long; using ull = unsigned long long; ll mod = 1000000007; using P = pair; using T = tuple; using vll = vector; using vP = vector

; using vT = vector; using vvll = vector; using vvvll = vector; using vvP = vector>; using dqll = deque; ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } /* Macros reg. ends here */ const ll INF = 1LL << 50; ll solve(ll X,ll n,ll m,vector u,vector v,vector s) { // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); vvP to(n); ll sol = -INF; rep(i, m) { to[u[i]].emplace_back(v[i], s[i]); to[v[i]].emplace_back(u[i], s[i]); } vll as(n, 0), bs(n, 0); // ax + b queue q; q.push(0); as[0] = 1, bs[0] = 0; while (!q.empty()) { ll u = q.front(); q.pop(); ll a = as[u], b = bs[u]; for (auto [v, s] : to[u]) { ll c = -a, d = s - b; if (as[v] != 0) { ll cc = as[v], dd = bs[v]; if (cc == c) { if (dd == d) continue; else { return 0; } } else { bool cond = ((d - dd) % 2 == 0) && (((d - dd) / (cc - c)) >= 1); if (cond) { ll csol = (d - dd) / (cc - c); if(sol >= 1 && csol != sol) { return 0; } if(sol == -INF) sol = csol; } else { return 0; } } } else { as[v] = c; bs[v] = d; q.push(v); } } } ll l = 1, r = X; rep(i, n){ ll a = as[i], b = bs[i]; if(a == 1){ chmax(l, 1 - b); chmin(r, X - b); } else { assert(a == -1); chmin(r, b - 1); chmax(l, b - X); } } ll ans = max(0LL, r - l + 1); if(sol >= 1) chmin(ans, 1LL); return ans; } class UnionFind{ public: vector par; vector siz; UnionFind(ll sz_):par(sz_),siz(sz_,1ll){ for(int i=0;i>n>>m>>K; vector a(m),b(m),c(m); UnionFind T(n); for(int i=0;i>a[i]>>b[i]>>c[i]; a[i]--; b[i]--; T.merge(a[i],b[i]); } vector> p(n); for(int i=0;i> h(n); for(int i=0;i u,v,s; map M; for(int j=0;j