제출 #1095870

#제출 시각아이디문제언어결과실행 시간메모리
1095870yoav_sDynamic Diameter (CEOI19_diameter)C++17
24 / 100
5071 ms16692 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()

const ll INF = 1e18;
const ll mod = 1e9 + 7;

void findFurtherest(vvp &graph, v &weights, vp &res, ll i)
{
    res[i] = p(0, i);
    for (auto x : graph[i])
    {
        if (res[x.f].f == -1)
        {
            findFurtherest(graph, weights, res, x.f);
            res[i] = max(res[i], p(res[x.f].f + weights[x.s], res[x.f].s));
        }
    }
}

ll getRes(vvp &graph, v &weights, vp &furtherest, ll i, vb &visited)
{
    if (visited[i]) return 0;
    visited[i] = true;
    ll maxi = furtherest[i].f;
    v children;
    for (auto x : graph[i])
    {
        if (!visited[x.f])
        {
            children.pb(furtherest[x.f].f + weights[x.s]);
            maxi = max(maxi, getRes(graph, weights, furtherest, x.f, visited));
        }
    }
    sort(all(children),greater<ll>());
    if (children.size() >= 2) maxi = max(maxi, children[0] + children[1]);
    return maxi;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll n, q, w;
    cin >> n >> q >> w;
    vtri edges(n - 1);
    for (ll i = 0; i < n - 1; i++)
    {
        cin >> edges[i].s.f >> edges[i].s.s >> edges[i].f;
        edges[i].s.f--; edges[i].s.s--;
    }
    v weight(n - 1);
    for (ll i = 0; i < n - 1; i++) weight[i] = edges[i].f;
    vvp graph(n);
    for (ll i = 0; i < n - 1; i++)
    {
        graph[edges[i].s.f].eb(edges[i].s.s, i);
        graph[edges[i].s.s].eb(edges[i].s.f, i);
    }
    ll last = 0;
    vp furtherest(n, p(-1,-1));
    findFurtherest(graph, weight, furtherest, 0);
    while (q--)
    {
        ll d, e;
        cin >> d >> e;
        d = (d + last) % (n - 1);
        e = (e + last) % w;
        weight[d] = e;
        furtherest = vp(n, p(-1,-1));
        findFurtherest(graph, weight, furtherest, 0);
        vb visited(n, false);
        last = getRes(graph, weight, furtherest, 0, visited);
        cout << last << "\n";
    }
    return 0;
}
#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...