// // Created by piotr on 14.03.2024. // #include <cassert> #include <compare> #include <cstdio> #include <queue> #include <set> #include <vector> struct DistanceEdge { int neighbor; int distance; auto operator<=>(const DistanceEdge& e) const { const auto x = distance <=> e.distance; return (x == std::strong_ordering::equal) ? (neighbor <=> e.neighbor) : x; } }; struct NeighborEdge { int neighbor; int distance; auto operator<=>(const NeighborEdge& e) const { return neighbor <=> e.neighbor; } }; struct QueueItem { int parent; int vertex; long long distance; }; int N, M, Q; std::vector<std::set<NeighborEdge>> neighbors; std::vector<std::set<DistanceEdge>> edges; std::vector<unsigned> colors; void dodaj(int a, int b, int d) { neighbors[a].insert({b, d}); neighbors[b].insert({a, d}); edges[a].insert({b, d}); edges[b].insert({a, d}); } void usun(int a, int b) { auto it = neighbors[a].find({b, 0}); int d = it->distance; neighbors[a].erase(it); neighbors[b].erase({a, d}); edges[a].erase({b, d}); edges[b].erase({a, d}); } void maluj(int start, long long range, unsigned kolor) { std::queue<QueueItem> bfs; bfs.push({-1, start, 0}); while (!bfs.empty()) { QueueItem vd = bfs.front(); bfs.pop(); colors[vd.vertex] = kolor; for (const auto& e : edges[vd.vertex]) { if (e.neighbor != vd.parent) { long long distance = vd.distance + static_cast<long long>(e.distance); if (distance > range) { break; } bfs.push({vd.vertex, e.neighbor, distance}); } } } } unsigned pytaj(int u) { return colors[u]; } int main() { assert(scanf("%d%d%d", &N, &M, &Q) == 3); neighbors.resize(N); edges.resize(N); colors.resize(N, 0); for (int m=0; m<M; ++m) { int a, b, d; assert(scanf("%d%d%d", &a, &b, &d) == 3); --a, --b; neighbors[a].insert({b, d}); neighbors[b].insert({a, d}); edges[a].insert({b, d}); edges[b].insert({a, d}); } for (int q=0; q<Q; ++q) { int t; assert(scanf("%d", &t) == 1); switch (t) { case 1: { int a, b, d; assert(scanf("%d%d%d", &a, &b, &d) == 3); dodaj(a-1, b-1, d); break; } case 2: { int a, b; assert(scanf("%d%d", &a, &b)==2); usun(a-1, b-1); break; } case 3: { int v; long long z; unsigned k; assert(scanf("%d%lld%u", &v, &z, &k)==3); maluj(v-1, z, k); break; } case 4: { int u; assert(scanf("%d", &u)==1); printf("%u\n", pytaj(u-1)); break; }} } }
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 | // // Created by piotr on 14.03.2024. // #include <cassert> #include <compare> #include <cstdio> #include <queue> #include <set> #include <vector> struct DistanceEdge { int neighbor; int distance; auto operator<=>(const DistanceEdge& e) const { const auto x = distance <=> e.distance; return (x == std::strong_ordering::equal) ? (neighbor <=> e.neighbor) : x; } }; struct NeighborEdge { int neighbor; int distance; auto operator<=>(const NeighborEdge& e) const { return neighbor <=> e.neighbor; } }; struct QueueItem { int parent; int vertex; long long distance; }; int N, M, Q; std::vector<std::set<NeighborEdge>> neighbors; std::vector<std::set<DistanceEdge>> edges; std::vector<unsigned> colors; void dodaj(int a, int b, int d) { neighbors[a].insert({b, d}); neighbors[b].insert({a, d}); edges[a].insert({b, d}); edges[b].insert({a, d}); } void usun(int a, int b) { auto it = neighbors[a].find({b, 0}); int d = it->distance; neighbors[a].erase(it); neighbors[b].erase({a, d}); edges[a].erase({b, d}); edges[b].erase({a, d}); } void maluj(int start, long long range, unsigned kolor) { std::queue<QueueItem> bfs; bfs.push({-1, start, 0}); while (!bfs.empty()) { QueueItem vd = bfs.front(); bfs.pop(); colors[vd.vertex] = kolor; for (const auto& e : edges[vd.vertex]) { if (e.neighbor != vd.parent) { long long distance = vd.distance + static_cast<long long>(e.distance); if (distance > range) { break; } bfs.push({vd.vertex, e.neighbor, distance}); } } } } unsigned pytaj(int u) { return colors[u]; } int main() { assert(scanf("%d%d%d", &N, &M, &Q) == 3); neighbors.resize(N); edges.resize(N); colors.resize(N, 0); for (int m=0; m<M; ++m) { int a, b, d; assert(scanf("%d%d%d", &a, &b, &d) == 3); --a, --b; neighbors[a].insert({b, d}); neighbors[b].insert({a, d}); edges[a].insert({b, d}); edges[b].insert({a, d}); } for (int q=0; q<Q; ++q) { int t; assert(scanf("%d", &t) == 1); switch (t) { case 1: { int a, b, d; assert(scanf("%d%d%d", &a, &b, &d) == 3); dodaj(a-1, b-1, d); break; } case 2: { int a, b; assert(scanf("%d%d", &a, &b)==2); usun(a-1, b-1); break; } case 3: { int v; long long z; unsigned k; assert(scanf("%d%lld%u", &v, &z, &k)==3); maluj(v-1, z, k); break; } case 4: { int u; assert(scanf("%d", &u)==1); printf("%u\n", pytaj(u-1)); break; }} } } |