#include<bits/stdc++.h> using ll = longlong; using PII = std::pair<int, int>; constint N = 2e5 + 10; constint M = 10; const ll INF = 0x7f7f7f7f7f7f7f7f; int n; std::vector<int> e[N]; ll dfs(int u, int fa, int d) { if (e[u].size() == 1) return d; ll res = 0; for (auto v : e[u]) { if (v == fa) continue; res = std::max(dfs(v, u, d + 1), res); } return res; } voidsolve() { std::cin >> n; for (int i = 1; i <= n - 1; ++i) { int u, v; std::cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } ll ans = 0; for (auto v : e[1]) ans += dfs(v, 1, 1); std::cout << ans << '\n'; } signedmain() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int T = 1; // std::cin >> T; while (T--) solve(); return0; }
voidsolve() { std::cin >> n >> m >> q; for (int i = 1; i <= n; ++i) std::cin >> a[i]; for (int i = 1; i <= m; ++i) { int u, v, w; std::cin >> u >> v >> w; e[u].push_back({v, w}); e[v].push_back({u, w}); } for (int i = 1; i <= q; ++i) std::cin >> b[i]; for (int i = q; i >= 1; --i) { int u = b[i]; dp[u] = INF; for (auto [v, w] : e[u]) dp[u] = std::min(dp[u], dp[v] + w); } ll ans = 0; for (int i = 1; i <= n; ++i) ans = (ans + a[i] * dp[i]) % MOD; std::cout << ans << '\n'; }
#include<bits/stdc++.h> using ll = longlong; using PLL = std::pair<ll, ll>; constint N = 1e5 + 10; constint B = 600; const ll MOD = 998244353; const ll INF = LLONG_MAX; int n, m, q; ll a[N], b[N]; ll dp[N]; int deg[N]; std::multiset<PLL> ms[N]; std::vector<PLL> e[N]; std::vector<PLL> big[N]; voidsolve() { std::cin >> n >> m >> q; for (int i = 1; i <= n; ++i) std::cin >> a[i]; for (int i = 1; i <= m; ++i) { ll u, v, w; std::cin >> u >> v >> w; e[u].push_back({v, w}); e[v].push_back({u, w}); ++deg[u], ++deg[v]; } for (int i = 1; i <= q; ++i) std::cin >> b[i]; for (int u = 1; u <= n; ++u) for (auto [v, w] : e[u]) { if (deg[v] > B) { big[u].push_back({v, w}); ms[v].insert({w, u}); } } for (int i = q; i >= 1; --i) { int u = b[i]; ll backup = dp[u]; if (deg[u] <= B) { dp[u] = INF; for (auto [v, w] : e[u]) dp[u] = std::min(dp[u], dp[v] + w); } else dp[u] = (*ms[u].begin()).first;
for (auto [v, w] : big[u]) { if (deg[v] > B) { ms[v].erase({backup + w, u}); ms[v].insert({dp[u] + w, u}); } } } ll ans = 0; for (int i = 1; i <= n; ++i) ans = (ans + a[i] * dp[i]) % MOD; std::cout << ans << '\n'; } signedmain() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int T = 1; // std::cin >> T; while (T--) solve(); return0; }
#include<bits/stdc++.h> using ll = longlong; const ll MOD = 998244353; ll n, A, B, C; ll calc(ll a, ll b) { return a * (A - C) + b * (B - C) + n * C; } voidsolve() { std::cin >> n >> A >> B >> C; if (A < B) std::swap(A, B); if (A < C) std::swap(A, C); if (B < C) std::swap(B, C); ll s = n * (A + B + C); ll min_gap = LLONG_MAX, cnta = -1, cntb = -1; for (int i = 0; i <= n; ++i) { int l = 0, r = n - i; while (l < r) { int mid = l + r >> 1; if (calc(i, mid) * 3 >= s) r = mid; else l = mid + 1; } if (l >= 1 && abs(calc(i, l) * 3 - s) > abs(calc(i, l - 1) * 3 - s)) --l; ll t = abs(calc(i, l) * 3 - s); if (t < min_gap) min_gap = t, cnta = i, cntb = l; } for (int i = 1; i <= n; ++i) { if (i <= cnta) std::cout << A << std::endl; elseif (i <= cnta + cntb) std::cout << B << std::endl; else std::cout << C << std::endl; int x, y; std::cin >> x >> y; } } signedmain() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int T; std::cin >> T; while (T--) solve(); return0; }
#include<bits/stdc++.h> using ll = longlong; using PII = std::pair<int, int>; constint N = 2e5 + 10; constint M = 3; const ll INF = 0x7f7f7f7f7f7f7f7f; boolwin(char s, char t) { if (s == 'R' && t == 'S') returntrue; if (s == 'S' && t == 'P') returntrue; if (s == 'P' && t == 'R') returntrue; returnfalse; } voidsolve() { std::string str; std::cin >> str; std::stack<int> sta; int n = str.size(); for (int i = 0; i < n; ++i) { while (!sta.empty() && !win(str[sta.top()], str[i])) sta.pop(); sta.push(i); } int ans = 0; while (!sta.empty()) ans = sta.top(), sta.pop(); std::cout << str[ans] << '\n'; } signedmain() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int T = 1; std::cin >> T; while (T--) solve(); return0; }