Submission #922468

#TimeUsernameProblemLanguageResultExecution timeMemory
922468SNP011Dynamic Diameter (CEOI19_diameter)C++17
11 / 100
5049 ms22848 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define u_map unordered_map
#define int ll
const int maxn = 1e5 + 5, mod = 1e9 + 7;

vector<int> adj[maxn];
map<pii, int> we;
int mx, far, lastans;

void dfs(int v, int par = 0, int h = 0) {
	if (h > mx) {
		far = v;
		mx = h;
	}
	for (int u: adj[v]) {
		if (u != par) {
			dfs(u, v, h + we[{u, v}]);
		}
	}
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, q, w;
    cin >> n >> q >> w;
    vector<pii> edge;
    for (int i = 1; i < n; i++) {
    	int u, v, w;
    	cin >> u >> v >> w;
    	adj[v].pb(u);
    	adj[u].pb(v);
    	we[{u, v}] = w;
    	we[{v, u}] = w;
    	edge.pb({u, v});
    }
    while (q--) {
    	int d, e;
    	cin >> d >> e;
		d = (d + lastans) % (n - 1);
		e = (e + lastans) % w;
    	we[{edge[d].first, edge[d].second}] = e;
    	we[{edge[d].second, edge[d].first}] = e;
    	mx = 0;
    	dfs(1);
    	int dia = far;
    	mx = 0;
    	dfs(far);
    	cout << mx << '\n';
    	lastans = mx;
    }

    return 0;
}

Compilation message (stderr)

diameter.cpp: In function 'int main()':
diameter.cpp:55:10: warning: unused variable 'dia' [-Wunused-variable]
   55 |      int dia = far;
      |          ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...