#include <bits/stdc++.h>
using std::cin, std::cout, std::vector;
using u64 = std::uint64_t;
void init_io() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
}
struct Node {
unsigned color = 0;
std::map<unsigned, unsigned> edges;
};
struct BfsEntry {
unsigned x;
unsigned parent;
u64 dist;
};
class Forest {
public:
Forest(const unsigned n):nodes(n) {
bfs_queue.reserve(n);
}
void add_edge(const unsigned a, const unsigned b, unsigned dist) {
nodes[a].edges[b] = dist;
nodes[b].edges[a] = dist;
}
void remove_edge(const unsigned a, const unsigned b) {
nodes[a].edges.erase(b);
nodes[b].edges.erase(a);
}
void paint(const unsigned source, const u64 max_dist, const unsigned color) {
bfs_queue.clear();
bfs_queue.push_back(BfsEntry{source, -1u, 0});
for (unsigned i=0; i < bfs_queue.size(); ++i) {
const BfsEntry entry = bfs_queue[i];
Node &node = nodes[entry.x];
node.color = color;
for (const auto [y, d] : node.edges) {
if (y == entry.parent) continue;
const u64 dist2 = entry.dist + d;
if (dist2 > max_dist) continue;
bfs_queue.push_back(BfsEntry{y, entry.x, dist2});
}
}
}
unsigned get_color(const unsigned a) const {
return nodes[a].color;
}
private:
vector<Node> nodes;
vector<BfsEntry> bfs_queue;
};
int main() {
init_io();
unsigned num_nodes, num_edges, num_queries;
cin >> num_nodes >> num_edges >> num_queries;
Forest forest(num_nodes);
for (unsigned i = 0; i < num_edges; ++i) {
unsigned a, b, d;
cin >> a >> b >> d;
--a; --b;
forest.add_edge(a, b, d);
}
for (unsigned i = 0; i < num_queries; ++i) {
unsigned typ;
cin >> typ;
if (typ == 1) {
unsigned a, b, d;
cin >> a >> b >> d;
--a; --b;
forest.add_edge(a, b, d);
} else if (typ == 2) {
unsigned a, b;
cin >> a >> b;
--a; --b;
forest.remove_edge(a, b);
} else if (typ == 3) {
unsigned source, color;
u64 max_dist;
cin >> source >> max_dist >> color;
--source;
forest.paint(source, max_dist, color);
} else if (typ == 4) {
unsigned a;
cin >> a;
--a;
const unsigned color = forest.get_color(a);
cout << color << "\n";
} else {
throw 0;
}
}
}
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 | #include <bits/stdc++.h> using std::cin, std::cout, std::vector; using u64 = std::uint64_t; void init_io() { cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Node { unsigned color = 0; std::map<unsigned, unsigned> edges; }; struct BfsEntry { unsigned x; unsigned parent; u64 dist; }; class Forest { public: Forest(const unsigned n):nodes(n) { bfs_queue.reserve(n); } void add_edge(const unsigned a, const unsigned b, unsigned dist) { nodes[a].edges[b] = dist; nodes[b].edges[a] = dist; } void remove_edge(const unsigned a, const unsigned b) { nodes[a].edges.erase(b); nodes[b].edges.erase(a); } void paint(const unsigned source, const u64 max_dist, const unsigned color) { bfs_queue.clear(); bfs_queue.push_back(BfsEntry{source, -1u, 0}); for (unsigned i=0; i < bfs_queue.size(); ++i) { const BfsEntry entry = bfs_queue[i]; Node &node = nodes[entry.x]; node.color = color; for (const auto [y, d] : node.edges) { if (y == entry.parent) continue; const u64 dist2 = entry.dist + d; if (dist2 > max_dist) continue; bfs_queue.push_back(BfsEntry{y, entry.x, dist2}); } } } unsigned get_color(const unsigned a) const { return nodes[a].color; } private: vector<Node> nodes; vector<BfsEntry> bfs_queue; }; int main() { init_io(); unsigned num_nodes, num_edges, num_queries; cin >> num_nodes >> num_edges >> num_queries; Forest forest(num_nodes); for (unsigned i = 0; i < num_edges; ++i) { unsigned a, b, d; cin >> a >> b >> d; --a; --b; forest.add_edge(a, b, d); } for (unsigned i = 0; i < num_queries; ++i) { unsigned typ; cin >> typ; if (typ == 1) { unsigned a, b, d; cin >> a >> b >> d; --a; --b; forest.add_edge(a, b, d); } else if (typ == 2) { unsigned a, b; cin >> a >> b; --a; --b; forest.remove_edge(a, b); } else if (typ == 3) { unsigned source, color; u64 max_dist; cin >> source >> max_dist >> color; --source; forest.paint(source, max_dist, color); } else if (typ == 4) { unsigned a; cin >> a; --a; const unsigned color = forest.get_color(a); cout << color << "\n"; } else { throw 0; } } } |
English