제출 #447029

#제출 시각아이디문제언어결과실행 시간메모리
447029yungyaoDynamic Diameter (CEOI19_diameter)C++17
42 / 100
5086 ms13836 KiB
using namespace std;
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <utility>
#include <memory.h>
#include <vector>
#include <bitset>

typedef pair<int,int> pii;
typedef long long LL;

#define iter(x) x.begin(),x.end()
#define F first
#define S second
#define pb push_back
#define mkp make_pair

#include <climits>
const int maxn = 1e5+10,mod = 0;

struct EDGE{
    int u,v;
    LL w;
    int con_to(int e){
        return u == e ? v : u;
    }
}edge[maxn];

vector <int> adj[maxn];
vector <pii> son[maxn];
int father[maxn];
pair<LL,LL> dp[maxn];
LL ans[maxn];

void update(pair <LL,LL> &onto,LL fr){
    if (fr > onto.F){
        onto.S = onto.F;
        onto.F = fr;
    }
    else if (fr > onto.S){
        onto.S = fr;
    }
}

void dfs(int x,int fr){
    father[x] = fr;
    for (auto i:adj[x]) if (edge[i].con_to(x) != fr){
        son[x].pb(mkp(edge[i].con_to(x),i));
        dfs(edge[i].con_to(x),x);
        ans[x] = max(ans[x],ans[edge[i].con_to(x)]);
        update(dp[x], dp[edge[i].con_to(x)].F + edge[i].w);
    }
    ans[x] = max(ans[x],dp[x].F + dp[x].S);
}

void upd(int x){
    ans[x] = 0;
    dp[x] = {};
    for (auto [i,ed]:son[x]){
        ans[x] = max(ans[x],ans[i]);
        update(dp[x], dp[i].F + edge[ed].w);
    }
    ans[x] = max(ans[x],dp[x].F + dp[x].S);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n,q,w;

    cin >> n >> q >> w;
    for (int i=0;i<n-1;++i){
        int u,v; LL c;

        cin >> u >> v >> c;
        edge[i] = {u,v,c};
        adj[u].pb(i);
        adj[v].pb(i);
    }
    dfs(1,0);

    LL last = 0;
    while (q--){
        LL d,e;

        cin >> d >> e;
        d = (d + last) % (n-1);
        e = (e + last) % w;

        edge[d].w = e;
        int u = father[edge[d].u] == edge[d].v ? edge[d].v : edge[d].u;
        while (u){
            upd(u);
            u = father[u];
        }

        last = ans[1];
        cout /*<< d << ' ' << e << ' ' */<< 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...