# D

Apr 15th, 2021
607
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <algorithm>
2. #include <iostream>
3. #include <vector>
4.
5. using namespace std;
6.
7. struct Graph {
8.     vector<vector<int>> vexes;
9.     vector<int> color;
10.     vector<int> ac;
11.     Graph(vector<vector<int>>& v, vector<int>& c, vector<int>& a) : vexes(v), color(c), ac(a) {}
12. };
13.
14. void DFS(int cur_v, Graph& v, int& start, int& end, bool& cycle) {
15.     v.color[cur_v] = 1;
16.     for (int vex : v.vexes[cur_v]) {
17.         if (v.color[vex] == 0 && !cycle) {
18.             v.ac[vex] = cur_v;
19.             DFS(vex, v, start, end, cycle);
20.         } else if (v.color[vex] == 1 && vex != v.ac[cur_v] && !cycle) {
21.             start = cur_v;
22.             end = vex;
23.             cycle = true;
24.         }
25.     }
26.     v.color[cur_v] = 2;
27. }
28.
29. int main() {
30.     freopen("input.txt", "r", stdin);
31.     freopen("output.txt", "w", stdout);
32.     int n;
33.     cin >> n;
34.     vector<vector<int>> vexes(n + 1);
35.     vector<int> color(n + 1);
36.     vector<int> ancestors(n + 1);
37.     vector<int> answer;
38.     for (int i = 1; i <= n; ++i) {
39.         for (int j = 1; j <= n; ++j) {
40.             int x;
41.             cin >> x;
42.             if (x == 1) {
43.                 vexes[i].push_back(j);
44.                 vexes[j].push_back(i);
45.             }
46.         }
47.     }
48.     Graph graph(vexes, color, ancestors);
49.     int start = -1;
50.     int end = n + 1;
51.     bool cycle = false;
52.     for (int i = 1; i <= n; ++i) {
53.         if (graph.color[i] == 0) {
54.             DFS(i, graph, start, end, cycle);
55.         }
56.     }
57.     if (cycle) {
58.         cout << "YES\n";
59.         for (int v = start; v != end; v = graph.ac[v]) {
60.             answer.push_back(v);
61.         }
62.         cout << answer.size() + 1 << '\n';
63.         for (int vex : answer) {
64.             cout << vex << ' ';
65.         }
66.         cout << end;
67.     } else {
68.         cout << "NO\n";
69.     }
70. }
RAW Paste Data

# Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×