#include #include #include #include using ull = unsigned long long; int main() { ull N, M; std::cin >> N >> M; std::map> groove_map; for (ull i = 0; i < M; i += 1) { ull xp, yp, xq, yq; std::cin >> xp >> yp >> xq >> yq; groove_map[yq].emplace(xp, xq); // groove_map[yq].emplace(xq, xp); } std::unordered_map> hunter_pos; hunter_pos.reserve(N); for (ull i = 1; i <= N; i += 1) { hunter_pos.emplace(i, std::vector{i}); } for (const auto &groove_line: groove_map) { for (const auto &grove: groove_line.second) { auto [x1, x2] = grove; std::vector x2tmp; std::vector x1tmp; // Move all hunter ids from x1 to x2tmp if (auto found = hunter_pos.find(x1); found != hunter_pos.end()) { for (ull moving: found->second) { x2tmp.push_back(moving); } } // Move all hunter ids from x2 to x1tmp if (auto found = hunter_pos.find(x2); found != hunter_pos.end()) { for (ull moving: found->second) { x1tmp.push_back(moving); } } hunter_pos[x2] = x2tmp; hunter_pos[x1] = x1tmp; } } std::vector output(N + 1, 0); for (const auto &[end_pos, hunter]: hunter_pos) { for (auto h: hunter) { output[h] = end_pos; } } bool first = true; for (auto o: output) { if (first) { first = false; continue; } std::cout << o << std::endl; } return 0; }