#define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <functional> #include <random> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int N_max = 500000; const int M_max = 1000000; struct Road { int a; int b; static bool less(const Road* p, const Road* q) { return (p->a != q->a) ? p->a < q->a : p->b < q->b; } }; Road roads_buf[M_max]; Road* roads_tmp[M_max]; struct Crossroad { Road** roads; int roads_len; bool is_visited; }; Crossroad crossroads[N_max + 1]; // 1-based int crossroad_num(Crossroad& crossroad) { return &crossroad - crossroads; } bool cycle_found; std::set<int> always_present; void visit(Crossroad& crossroad, std::vector<int>& path, std::set<int>& in_path, std::set<int>& always_present) { crossroad.is_visited = true; path.push_back(crossroad_num(crossroad)); in_path.insert(crossroad_num(crossroad)); for (auto& cr : path) { debug_printf("%d ", cr); } debug_printf("\n"); auto old_visited = std::find_if( crossroad.roads, crossroad.roads + crossroad.roads_len, [](const Road* r) { return crossroads[r->b].is_visited; } ); if (old_visited == crossroad.roads + crossroad.roads_len) { for (int i=0; i < crossroad.roads_len; ++i) { Road& road = *crossroad.roads[i]; Crossroad& other_crossroad = crossroads[road.b]; assert(!other_crossroad.is_visited); visit(other_crossroad, path, in_path, always_present); } } else { cycle_found = true; for (auto it=always_present.begin(); it != always_present.end(); ) { Crossroad& cr = crossroads[*it]; if (!cr.is_visited) { it = always_present.erase(it); } else { ++it; } } } crossroad.is_visited = false; path.pop_back(); in_path.erase(crossroad_num(crossroad)); } int main() { std::ios_base::sync_with_stdio(false); int N, M; scanf("%d %d", &N, &M); for (int i=0; i < M; ++i) { int ai, bi; scanf("%d %d", &ai, &bi); Road& road = roads_buf[i]; road.a = ai; road.b = bi; roads_tmp[i] = &road; } std::sort(roads_tmp, roads_tmp + M, Road::less); int curr_crossroad = -1; for (int i=0; i < M; ++i) { Road& road = *roads_tmp[i]; if (curr_crossroad != road.a) { curr_crossroad = road.a; crossroads[curr_crossroad].roads = &roads_tmp[i]; } ++crossroads[curr_crossroad].roads_len; } for (int i=1; i <= N; ++i) { always_present.insert(i); } std::vector<int> path; std::set<int> in_path; for (int i=1; i <= N; ++i) { Crossroad& crossroad = crossroads[i]; in_path.clear(); std::for_each( crossroads + 1, crossroads + N + 1, [](Crossroad& cr) { cr.is_visited = false; } ); visit(crossroad, path, in_path, always_present); } if (cycle_found) { printf("%d\n", always_present.size()); for (auto& cr : always_present) { printf("%d ", cr); }; printf("\n"); } else { printf("NIE\n"); } }
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 | #define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <functional> #include <random> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int N_max = 500000; const int M_max = 1000000; struct Road { int a; int b; static bool less(const Road* p, const Road* q) { return (p->a != q->a) ? p->a < q->a : p->b < q->b; } }; Road roads_buf[M_max]; Road* roads_tmp[M_max]; struct Crossroad { Road** roads; int roads_len; bool is_visited; }; Crossroad crossroads[N_max + 1]; // 1-based int crossroad_num(Crossroad& crossroad) { return &crossroad - crossroads; } bool cycle_found; std::set<int> always_present; void visit(Crossroad& crossroad, std::vector<int>& path, std::set<int>& in_path, std::set<int>& always_present) { crossroad.is_visited = true; path.push_back(crossroad_num(crossroad)); in_path.insert(crossroad_num(crossroad)); for (auto& cr : path) { debug_printf("%d ", cr); } debug_printf("\n"); auto old_visited = std::find_if( crossroad.roads, crossroad.roads + crossroad.roads_len, [](const Road* r) { return crossroads[r->b].is_visited; } ); if (old_visited == crossroad.roads + crossroad.roads_len) { for (int i=0; i < crossroad.roads_len; ++i) { Road& road = *crossroad.roads[i]; Crossroad& other_crossroad = crossroads[road.b]; assert(!other_crossroad.is_visited); visit(other_crossroad, path, in_path, always_present); } } else { cycle_found = true; for (auto it=always_present.begin(); it != always_present.end(); ) { Crossroad& cr = crossroads[*it]; if (!cr.is_visited) { it = always_present.erase(it); } else { ++it; } } } crossroad.is_visited = false; path.pop_back(); in_path.erase(crossroad_num(crossroad)); } int main() { std::ios_base::sync_with_stdio(false); int N, M; scanf("%d %d", &N, &M); for (int i=0; i < M; ++i) { int ai, bi; scanf("%d %d", &ai, &bi); Road& road = roads_buf[i]; road.a = ai; road.b = bi; roads_tmp[i] = &road; } std::sort(roads_tmp, roads_tmp + M, Road::less); int curr_crossroad = -1; for (int i=0; i < M; ++i) { Road& road = *roads_tmp[i]; if (curr_crossroad != road.a) { curr_crossroad = road.a; crossroads[curr_crossroad].roads = &roads_tmp[i]; } ++crossroads[curr_crossroad].roads_len; } for (int i=1; i <= N; ++i) { always_present.insert(i); } std::vector<int> path; std::set<int> in_path; for (int i=1; i <= N; ++i) { Crossroad& crossroad = crossroads[i]; in_path.clear(); std::for_each( crossroads + 1, crossroads + N + 1, [](Crossroad& cr) { cr.is_visited = false; } ); visit(crossroad, path, in_path, always_present); } if (cycle_found) { printf("%d\n", always_present.size()); for (auto& cr : always_present) { printf("%d ", cr); }; printf("\n"); } else { printf("NIE\n"); } } |