Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Node {
- Node* next_;
- Node() : next_(nullptr) {};
- Node(Node* next) : next_(next) {};
- };
- int main() {
- int nodes_count;
- cin >> nodes_count;
- vector<Node> list(1 + nodes_count);
- for (int i = 1; i <= nodes_count; ++i) {
- int next_node_idx;
- cin >> next_node_idx;
- list[i].next_ = &list[next_node_idx];
- }
- //рассматриваю случай, что нет петель
- Node* turtle = &list[1];
- Node* rabbit = &list[2];
- while (turtle && rabbit && turtle != rabbit && turtle->next_ && rabbit->next_ && rabbit->next_->next_) {
- turtle = turtle->next_;
- rabbit = rabbit->next_->next_;
- }
- if (!turtle || !rabbit) {
- cout << -1;
- return 0;
- }
- int circle_size = 1;
- turtle = turtle->next_;
- while (turtle != rabbit) {
- circle_size++;
- turtle = turtle->next_;
- }
- cout << circle_size;
- return 0;
- }
- /*
- test1
- 6
- 3 4 5 5 6 2
- 4
- это не пройдет сейчас
- test2
- 3
- 2 3
- -1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement