1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC target ("avx2")
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")

#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int) (x).size())
#define pb push_back
#define mp make_pair
#define int long long

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 998244353;
const ll base = 1e6 + 9;
const ll inf = 1e18;
const int MAX = 3e5 + 42;
const int LG = 20;
const int TL = 3500;

//random_device rd;
//mt19937 gen(rd());
//uniform_int_distribution<ll> dis(1, inf);

const int N = 3e5 + 42;
const int L = 20;

vector<pii> g[N];
int fi[N], fo[N], h[N];
int up[N][L];

void dfs(int u, int p){
    static int tmr = 0;
    up[u][0] = p;
    for (int l = 1; l < L; ++l){
        up[u][l] = up[up[u][l - 1]][l - 1];
    }
    fi[u] = ++tmr;
    for (auto &[v, w]: g[u]){
        if (v != p){
            h[v] = h[u] + w;
            dfs(v, u);
        }
    }
    fo[u] = ++tmr;
}

bool par(int u, int v){
    return fi[u] <= fi[v] && fo[v] <= fo[u];
}

int lca(int u, int v){
    if (par(u, v)) return u;
    if (par(v, u)) return v;
    for (int l = L - 1; ~l; --l){
        if (!par(up[u][l], v)){
            u = up[u][l];
        }
    }
    return up[u][0];
}

int dst(int u, int v){
    return h[u] + h[v] - 2 * h[lca(u, v)];
}

int sz[N], anc[N];
bool us[N];
deque <tuple<int, int, int>> s[N];

void siz(int u, int p){
    sz[u] = 1;
    for (auto &[v, w]: g[u]){
        if (v != p && !us[v]){
            siz(v, u);
            sz[u] += sz[v];
        }
    }
}

int cen(int u, int p, int n){
    for (auto &[v, w]: g[u]){
        if (v != p && !us[v] && sz[v] > n / 2){
            return cen(v, u, n);
        }
    }
    return u;
}

int cen(int u){
    siz(u, u);
    return cen(u, u, sz[u]);
}

void cdd(int u, int p){
    us[u] = 1, anc[u] = p;
    s[u].push_back({1e9 + 42, 0, -1});
    for (auto &[v, w]: g[u]){
        if (!us[v]) cdd(cen(v), u);
    }
}

void upd(int u, int d, int c, int tmr){
    int uu = u;
    while(uu != -1){
        int ds = d - dst(u, uu);
        while(true){
            auto [dd, c, tt] = s[uu].front();
            if (dd <= ds) s[uu].pop_front();
            else break;
        }
        s[uu].push_front({ds, c, tmr});
        uu = anc[uu];
    }
}

int get(int u){
    int uu = u;
    pii ans = {-1, 0};
    while(uu != -1){
        int ds = dst(u, uu);
        auto [dd, c, tmr] = *lower_bound(all(s[uu]), (tuple<int, int, int>) {ds, -1, -1});
        umax(ans, {tmr, c});
        uu = anc[uu];
    }
    return ans.s;
}

void solve(){
    int n; cin >> n;
    int m, q;
    cin >> m >> q;
    for (int i = 0; i < m; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(0, 0);
    cdd(cen(0), -1);
    bool centroid_group = (m == n - 1);
    vector<array<int, 4>> qr(q);
    for(auto &[t, a, b, c] : qr) {
        cin >> t;
        if (t == 1){
            centroid_group = 0;
            cin >> a >> b >> c;
        }
        else if(t == 2) {
            centroid_group = 0;
            cin >> a >> b;
        }
        else if(t == 3) {
            cin >> a >> b >> c;
        }
        else {
            cin >> a;
        }
    }
    if(centroid_group) {
        int index = 0;
        for(auto &[t, a, b, c] : qr) {
            assert(t != 1 && t != 2);
            if(t == 3) {
                int u = a, d = b;
                upd(--u, d, c, index);
            }
            else {
                int u = a;
                cout << get(--u) << '\n';
            }
            index++;
        }
        return;
    }
    map<pii, int> weight;
    vector<set<pii>> adj(n + 1);
    for(int i = 0; i < n; i++) {
        for(auto [j, w] : g[i]) {
            adj[i + 1].insert({j + 1, w});
            weight[{i + 1, j + 1}] = w;
        }
    }
    vector<int> col(n + 1);
    function<void(int, int, int, int)> dfs = [&](int v, int p, int s, int color) {
        if(s < 0) return;
        col[v] = color;
        for(auto [to, w] : adj[v]) {
            if(to == p) continue;
            dfs(to, v, s - w, color);
        }
    };
    for(auto &[t, a, b, c] : qr) {
        if(t == 1) {
            int u = a; int v = b; int w = c;
            adj[u].insert({v, w});
            adj[v].insert({u, w});
            weight[{u, v}] = weight[{v, u}] = w;
        }
        else if(t == 2) {
            int u = a; int v = b;
            int w = weight[{u, v}];
            adj[u].erase({v, w});
            adj[v].erase({u, w});
        }
        else if(t == 3) {
            int v = a; int dst = b; int k = c;
            dfs(v, 0, dst, k);
        }
        else {
            int u = a;
            cout << col[u] << '\n';
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int ttt = 1;
//    cin >> ttt;
    while(ttt--) {
        solve();
    }
}