제출 #1343089

#제출 시각아이디문제언어결과실행 시간메모리
1343089nghiaxtoneriDynamic Diameter (CEOI19_diameter)C++20
31 / 100
5091 ms11180 KiB
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define MASK(n) (1LL << n)
#define PhTrNghia "diameter"

using namespace std;

const int maxn = 2e5 + 5;
const int inf = 1e18;

int n, q, w;
vector<pair <int, int>> g[maxn];
pair <int, int> edges[maxn];
int dist[maxn], weight[maxn];

namespace sub1{
    void dfs(int u, int p){
        for (pair <int, int> cur : g[u]){
            int v = cur.first;
            int w = cur.second;
            if (v == p) continue;
            dist[v] = dist[u] + w;
            dfs(v, u);
        }
    }

    int get_diameter(){
        for (int i = 1; i <= n; i++) dist[i] = 0;
        dfs(1, 0);

        int tmp1 = 1;
        for (int i = 1; i <= n; i++) if (dist[i] > dist[tmp1]) tmp1 = i;

        for (int i = 1; i <= n; i++) dist[i] = 0;
        dfs(tmp1, 0);

        int tmp2 = tmp1;
        for (int i = 1; i <= n; i++) if (dist[i] > dist[tmp2]) tmp2 = i;

        return dist[tmp2];
    }

    void solve(){
        int last = 0;

        while (q--){
            int d, e;
            cin >> d >> e;

            int id = (d + last) % (n - 1) + 1;
            int nxt_w = (e + last) % w;

            weight[id] = nxt_w;

            for (int i = 1; i <= n; i++) g[i].clear();

            for (int i = 1; i < n; i++){
                int u = edges[i].first;
                int v = edges[i].second;
                int c = weight[i];
                g[u].push_back({v, c});
                g[v].push_back({u, c});
            }

            int ans = get_diameter();
            cout << ans << endl;

            last = ans;
        }
    }
}

bool check_sub2 = 1;

namespace sub2{

    bool check(){
        return check_sub2;
    }

    multiset <int> st;

    void solve(){
        for (int i = 1; i < n; i++) st.insert(weight[i]);

        int last = 0;

        while (q--){
            int d, e;
            cin >> d >> e;

            int id = (d + last) % (n - 1) + 1;
            int nxt_w = (e + last) % w;

            st.erase(st.find(weight[id]));

            weight[id] = nxt_w;
            st.insert(weight[id]);

            int ans;

            if (st.size() == 1) ans = *st.begin();
              else {
                auto it = st.end();
                it--;
                int mxf = *it;
                it--;
                int mxs = *it;
                ans = mxf + mxs;
            }

            cout << ans << endl;
            last = ans;
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //if (fopen(PhTrNghia".INP", "r")){
        //freopen(PhTrNghia".INP", "r", stdin);
        //freopen(PhTrNghia".OUT", "w", stdout);
    //}

    cin >> n >> q >> w;

    for (int i = 1; i < n; i++){
        int u, v, c;
        cin >> u >> v >> c;
        if (u != 1) check_sub2 = 0;
        edges[i] = {u, v};
        weight[i] = c;
    }

    if (sub2::check()){
        sub2::solve();
        return 0;
    }
    sub1::solve();

    return 0;
}
/*
4 3 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
1 890

10 10 10000
1 9 1241
5 6 1630
10 5 1630
2 6 853
10 1 511
5 3 760
8 3 1076
4 10 1483
7 10 40
8 2051
5 6294
5 4168
7 1861
0 5244
6 5156
3 3001
8 5267
5 3102
8 3623
*/
#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...