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
#include <vector>
#include <utility>
#include <cstdio>

#define REP(a, n) for (int a = 0; a < (n); ++a)
#define FOR(a, b, c) for (int a = (b); a <= (c); ++a)

using namespace std;

typedef long long LL;

template<class T> inline int SIZE(const T &t) { return t.size(); }

///////////////////////////

#define MAXN 200005

int N;

vector<pair<int,int>> sas[MAXN];
vector<pair<pair<int,LL>,pair<int,int>>> zapyt;



///////////// CENTROIDY //////////

bool byl[MAXN];
int subtree_size[MAXN];
int Cpar[MAXN];
vector<LL> Cpardist[MAXN];
vector<pair<LL,pair<int,int>>> Ckolory[MAXN];

int bin_search(LL d, const vector<pair<LL,pair<int,int>>> &tab) { // znajdz ostatni >= d (być może -1)
    int L = -1, R = SIZE(tab);
    while (R-L>1) {
        int M = (L+R)/2;
        (tab[M].first>=d ? L : R) = M;
    }
    return L;
}


int get_subtree_size(int node, LL dist, int parent = -1) {
    Cpardist[node].push_back(dist);
    subtree_size[node] = 1;
    for (auto ss : sas[node]) {
        int child = ss.first;
	if (child == parent || byl[child])
	    continue;
	subtree_size[node] += get_subtree_size(child, dist+ss.second, node);
    }
    return subtree_size[node];
}

int get_centroid(int node, int tree_size, int parent = -1) {
    for (auto ss : sas[node]) {
        int child = ss.first;
        if (child == parent || byl[child])
            continue;
        if (subtree_size[child] * 2 > tree_size)
	    return get_centroid(child, tree_size, node);
    }
    return node;
}

void build_centroid_decomp(int node = 0, int c_par = 0, LL dist = 0) {
    int centroid = get_centroid(node, get_subtree_size(node, dist));
    byl[centroid] = 1;
    Cpar[centroid] = c_par;
    for (auto ss : sas[centroid]) {
        int child = ss.first;
        if (byl[child])
            continue;
        build_centroid_decomp(child, centroid, ss.second);
    }
}

int T;

void malujC(int a, LL d, int k) {
    int a0 = a;
    int s = SIZE(Cpardist[a])-1;
    while (a) {
        LL d2 = d-Cpardist[a0][s];
        while (!Ckolory[a].empty() && Ckolory[a].back().first<=d2)
            Ckolory[a].pop_back();
        if (d2>=0)
            Ckolory[a].emplace_back(d2, make_pair(T, k));
        a = Cpar[a];
        --s;
    }
}

int znajdz_kolor(int a) {
    int czas = 0, kol = 0;
    int a0 = a;
    int s = SIZE(Cpardist[a])-1;
    while (a) {
        LL d = Cpardist[a0][s];
        int x = bin_search(d, Ckolory[a]);
        if (x>=0 && Ckolory[a][x].second.first>czas) {
            czas = Ckolory[a][x].second.first;
            kol = Ckolory[a][x].second.second;
        }
        a = Cpar[a];
        --s;
    }
    return kol;
}

void centroidy() {
    fprintf(stderr, "CENTROIDY\n");
    FOR(a, 1, N)
        if (!byl[a])
            build_centroid_decomp(a);
    FOR(a, 1, N)
        Cpardist[a].push_back(0);
    for (auto z : zapyt) {
        T++;
        int t = z.first.first;
        LL d = z.first.second;
        int a = z.second.first;
        int b = z.second.second;
        if (t==3)
            malujC(a, d, b);
        else
            printf("%d\n", znajdz_kolor(a));
    }
    
}

///////////// RECZNE //////////

int kolor[MAXN];

inline void polacz(int a, int b, int d) {
    sas[a].emplace_back(b, d);
    sas[b].emplace_back(a, d);
}

inline void rozlacz(int a, int b) {
    REP(x, SIZE(sas[a]))
        if (sas[a][x].first==b) {
            sas[a][x] = sas[a].back();
            sas[a].pop_back();
            break;
        }
    REP(x, SIZE(sas[b]))
        if (sas[b][x].first==a) {
            sas[b][x] = sas[b].back();
            sas[b].pop_back();
            break;
        }
}

void maluj(int p, int a, LL d, int k) {
    kolor[a] = k;
    for (auto s : sas[a])
        if (s.first!=p && d>=s.second)
            maluj(a, s.first, d-s.second, k);
}

void reczne() {
    for (auto z : zapyt) {
        int t = z.first.first;
        LL d = z.first.second;
        int a = z.second.first;
        int b = z.second.second;
        if (t==1)
            polacz(a, b, d);
        else
        if (t==2)
            rozlacz(a, b);
        else
        if (t==3)
	    maluj(0, a, d, b);
        else
            printf("%d\n", kolor[a]);
    }
}

////////////////////////////////

int main() {
    int M, Q;
    scanf("%d%d%d", &N, &M, &Q);
    REP(a, M) {
        int x, y, d;
        scanf("%d%d%d", &x, &y, &d);
        sas[x].emplace_back(y, d);
        sas[y].emplace_back(x, d);
    }
    bool sa12 = 0;
    REP(q, Q) {
        int t, a, b;
        LL d;
        scanf("%d", &t);
        if (t==1)
            scanf("%d%d%lld", &a, &b, &d);
        else if (t==2)
            scanf("%d%d", &a, &b);
        else if (t==3)
            scanf("%d%lld%d", &a, &d, &b);
        else if (t==4)
            scanf("%d", &a);
        zapyt.emplace_back(make_pair(t, d), make_pair(a, b));
        if (t<=2) sa12 = 1;
    }
    if (!sa12)
        centroidy();
    else
        reczne();
}