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
/**
 * Patryk Kisielewski
 * 
 * Potyczki Algorytmiczne 2024
 * Zadanie: KOL - Kolorowy las [A]
*/
#include <unordered_map>
#include <set>
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <bitset>

using namespace std;

const long long MAX_DISTANCE = 1'000'000'000'000'000LL;

struct Query
{
    int type;
    long a, b, k;
    long long d = 0, z;
};

struct Node
{
    Node();
    Node* parent;
    unordered_map<Node*, long long> edges;
    long color;
};

Node::Node() : parent(this), color(0L) {}

long n, m, q;
Node* tree;
long global_color = 0;
bool no_mods;
bool no_distance;

void changeRoot(Node* a) {
    if (a->parent == a) {
        return;
    }
    changeRoot(a->parent);
    if (no_distance) {
        a->color = a->parent->color;
    }
    a->parent->parent = a;
    a->parent = a;
}

Node* getRoot(Node* a) {
    while (a->parent != a) {
        a = a->parent;
    }
    return a;
}

void addEdgeBase(Node* a, Node* b, long long d) {
    changeRoot(a);
    a->parent = b;
    a->edges.emplace(make_pair(b, d));
    b->edges.emplace(make_pair(a, d));
}

void removeEdgeBase(Node* a, Node* b) {
    a->edges.erase(b);
    b->edges.erase(a);
    if (a->parent == b) {
        a->parent = a;
    } else {
        b->parent = b;
    }
}

void colorBase(Node* prev, Node* current, long long z, int k) {
    if (z < 0) return;
    current->color = k;
    for (auto& next : current->edges) {
        if (next.first != prev) {
            colorBase(current, next.first, z - next.second, k);
        }
    }
}

long getColorBase(Node* node) {
    return node->color;
}

void removeEdgeNoDistance(Node* a, Node* b) {
    a->edges.erase(b);
    b->edges.erase(a);
    if (a->parent == b) {
        a->color = getRoot(a)->color;
        a->parent = a;
    } else {
        b->color = getRoot(b)->color;
        b->parent = b;
    }
}

void colorNoDistance(Node* prev, Node* current, long long z, int k) {
    getRoot(current)->color = k;
}

long getColorNoDistance(Node* node) {
    return getRoot(node)->color;
}

void colorNoModsAndDistance(Node* prev, Node* current, long long z, int k) {
    global_color = k;
}

long getColorNoModsAndDistance(Node* node) {
    return global_color;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    void (*addEdge)(Node* a, Node* b, long long d) = addEdgeBase;
    void (*removeEdge)(Node* a, Node* b) = removeEdgeBase;
    void (*color)(Node* prev, Node* current, long long z, int k) = colorBase;
    long (*getColor)(Node* a) = getColorBase;

    cin >> n >> m >> q;

    tree = new Node[n+1];

    long a, b;
    long long d;

    for (int i = 0; i < m; ++i) {
        cin >> a >> b >> d;
        addEdgeBase(tree + a, tree + b, d);
    }

    vector<Query> queries(q);

    no_mods = (m + 1 == n);
    no_distance = true;

    for (auto& query : queries) {
        cin >> query.type;
        switch (query.type) {
            case 1:
                cin >> query.a >> query.b >> query.d;
                no_mods = false;
                break;
            case 2:
                cin >> query.a >> query.b;
                no_mods = false;
                break;
            case 3:
                cin >> query.a >> query.z >> query.k;
                if (query.k != MAX_DISTANCE) no_distance = false;
                break;
            case 4:
                cin >> query.a;
        }
    }

    if (no_distance) {
        removeEdge = removeEdgeNoDistance;
        if (no_mods) {
            color = colorNoModsAndDistance;
            getColor = getColorNoModsAndDistance;
        } else {
            color = colorNoDistance;
            getColor = getColorNoDistance;
        }
    }

    for (auto& query : queries) {
        switch (query.type) {
            case 1:
                addEdge(tree + query.a, tree + query.b, query.d);
                break;
            case 2:
                removeEdge(tree + query.a, tree + query.b);
                break;
            case 3:
                color(nullptr, tree + query.a, query.z, query.k);
                break;
            case 4:
                cout << getColor(tree + query.a) << '\n';
        }
    }

    return 0;
}