Created
October 26, 2014 10:09
-
-
Save sparfenyuk/318b43314771f350390b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <iterator> | |
| #include <set> | |
| #include <vector> | |
| #include <algorithm> | |
| #include <cassert> | |
| #include <cmath> | |
| using namespace std; | |
| typedef unsigned long long input_type; | |
| typedef vector<vector<input_type> > InputContainer; | |
| void prepare_graph(InputContainer& container, input_type n) | |
| { | |
| container.resize(n); | |
| } | |
| void add_edge(InputContainer& container, input_type from_point, input_type to_point) | |
| { | |
| assert(from_point >= 1 && from_point <= 1000); | |
| assert(to_point >= 1 && to_point <= 1000); | |
| container[from_point - 1].push_back(to_point - 1); | |
| } | |
| bool explore_cycle(const InputContainer& container, const input_type index, vector<bool>& v, vector<input_type>& path) | |
| { | |
| v[index] = true; | |
| path.push_back(index); | |
| const vector<input_type>& a = container[index]; | |
| for (input_type i = 0; i < a.size(); ++i) { | |
| const input_type& next_index = a[i]; | |
| if (!v[next_index]) { | |
| if (explore_cycle(container, next_index, v, path)) | |
| return true; | |
| } | |
| else { | |
| if (find(path.begin(), path.end(), next_index) != path.end()) { | |
| return true; | |
| } | |
| } | |
| } | |
| path.pop_back(); | |
| return false; | |
| } | |
| bool has_cycle(const InputContainer& container) | |
| { | |
| vector<bool> v(container.size(), false); | |
| for (input_type i = 0; i < v.size(); ++i) { | |
| if (!v[i]) { | |
| vector<input_type> path; | |
| if (explore_cycle(container, i, v, path)) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| int main() | |
| { | |
| // { | |
| // InputContainer input; | |
| // prepare_graph(input, 3); | |
| // add_edge(input, 1, 2); | |
| // add_edge(input, 1, 3); | |
| // add_edge(input, 2, 3); | |
| // | |
| // assert(has_cycle(input) == false); | |
| // } | |
| // { | |
| // InputContainer input; | |
| // prepare_graph(input, 5); | |
| // add_edge(input, 1, 2); | |
| // add_edge(input, 1, 3); | |
| // add_edge(input, 3, 4); | |
| // add_edge(input, 4, 3); | |
| // | |
| // assert(has_cycle(input) == !false); | |
| // } | |
| // { | |
| // InputContainer input; | |
| // prepare_graph(input, 1000); | |
| // add_edge(input, 1000, 2); | |
| // for (input_type i = 1; i < 1000; ++i) { | |
| // add_edge(input, i, i+1); | |
| // } | |
| // assert(has_cycle(input) == !false); | |
| // } | |
| // { | |
| // input_type n = 6; | |
| // | |
| // InputContainer input; | |
| // prepare_graph(input, n); | |
| // add_edge(input, 1, 2); | |
| // add_edge(input, 2, 3); | |
| // add_edge(input, 3, 4); | |
| // add_edge(input, 3, 5); | |
| // add_edge(input, 5, 6); | |
| // add_edge(input, 5, 1); | |
| // assert(has_cycle(input) == true); | |
| // } | |
| // { | |
| // input_type n = 4; | |
| // | |
| // InputContainer input; | |
| // prepare_graph(input, n); | |
| // add_edge(input, 1, 2); | |
| // add_edge(input, 4, 1); | |
| // add_edge(input, 2, 3); | |
| // add_edge(input, 3, 1); | |
| // assert(has_cycle(input) == true); | |
| // } | |
| // return 0; | |
| input_type n = 0, m = 0; | |
| cin >> n >> m; | |
| InputContainer input; | |
| prepare_graph(input, n); | |
| for (input_type i = 0; i < m; ++i) { | |
| input_type first, second; | |
| cin >> first >> second; | |
| add_edge(input, first, second); | |
| } | |
| cout << (has_cycle(input) ? 1 : 0); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment