This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
ll n, W;
int d[maxn]; ll val[maxn], dp[maxn][3];
vector<pll> gshit[maxn];
vector<int> graph[maxn];
void dfs1(int u, int p) {
for(auto &[v, w] : gshit[u]) {
if(v == p) continue;
d[v] = d[u] + 1;
val[v] = w;
dfs1(v, u);
}
}
void dfs(int u, int p) {
ll mx1=0, mx2=0;
for(int &v : graph[u]) {
if(v == p) continue;
dfs(v, u);
dp[u][0] = max(dp[u][0], dp[v][0] + val[v]);
dp[u][2] = max(dp[u][2], dp[v][2]);
if(dp[v][0] + val[v] >= mx1) {
mx2 = mx1;
mx1 = dp[v][0] + val[v];
} else if(dp[v][0] + val[v] >= mx2) {
mx2 = dp[v][0] + val[v];
}
}
dp[u][1] = mx1 + mx2;
dp[u][2] = max(dp[u][2], max(dp[u][0], dp[u][1]));
}
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int q;
cin >> n >> q >> W;
vector<array<ll, 3> > edges;
for(int i=0; i<n-1; i++) {
ll a, b, w;
cin >> a >> b >> w;
edges.push_back({ a, b, w });
gshit[a].push_back({ b, w });
gshit[b].push_back({ a, w });
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs1(1, 1);
for(auto &[a, b, w] : edges) if(d[a] > d[b]) swap(a, b);
if(n <= 5000 && q <= 5000) {
ll last = 0;
while(q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % W;
val[edges[d][1]] = e;
for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 0;
dfs(1, 1);
cout << dp[1][2] << '\n';
last = dp[1][2];
}
return 0;
}
multiset<ll> ms;
for(auto &[a, b, w] : edges) ms.insert(w);
ll last = 0;
while(q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % W;
ms.erase(ms.find(edges[d][2]));
edges[d][2] = e;
ms.insert(e);
auto it = --ms.end();
ll ans = *it;
if(it != ms.begin()) ans += *(--it);
cout << ans << '\n';
last = ans;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |