// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
/*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int numNode, numQuery, mod;
vector<int> G[N];
struct Edge {
int u, v, w;
int other(int x) {
return x ^ u ^ v;
}
} edge[N];
ll f[N], d[N], ans;
void dfs(int u, int par) {
f[u] = d[u] = 0;
ll best1 = 0, best2 = 0;
for(int i : G[u]) {
int v = edge[i].other(u);
int w = edge[i].w;
if (v == par) continue;
dfs(v, u);
maximize(f[u], f[v] + w);
maximize(d[u], d[v]);
if (best1 < f[v] + w) {
best2 = best1;
best1 = f[v] + w;
} else maximize(best2, f[v] + w);
}
maximize(d[u], best1 + best2);
maximize(d[u], f[u]);
}
void init(void) {
cin >> numNode >> numQuery >> mod;
REP(i, numNode - 1) {
int u, v, w;
cin >> u >> v >> w;
edge[i] = {u, v, w};
G[u].pb(i);
G[v].pb(i);
}
}
void process(void) {
while(numQuery--) {
int index, weight;
cin >> index >> weight;
index = (index + ans) % (numNode - 1);
weight = (weight + ans) % mod;
edge[index].w = weight;
dfs(1, -1);
cout << (ans = d[1]) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
diameter.cpp: In function 'int main()':
diameter.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |