File size: 2,741 Bytes
1fd0050 | 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 | #include <bits/stdc++.h>
using namespace std;
int n, m_global;
vector<int> pathh;
vector<char> visitedd;
bool dfs_forward(int u, const vector<vector<int>>& adj) {
visitedd[u] = 1;
pathh.push_back(u);
if ((int)pathh.size() == n) return true;
vector<pair<int, int>> cands;
for (int v : adj[u]) {
if (!visitedd[v]) {
int cnt = 0;
for (int w : adj[v]) {
if (!visitedd[w]) ++cnt;
}
cands.emplace_back(cnt, v);
}
}
sort(cands.begin(), cands.end());
for (auto& p : cands) {
if (dfs_forward(p.second, adj)) return true;
}
pathh.pop_back();
visitedd[u] = 0;
return false;
}
bool dfs_backward(int u, const vector<vector<int>>& back_adj) {
visitedd[u] = 1;
pathh.push_back(u);
if ((int)pathh.size() == n) return true;
vector<pair<int, int>> cands;
for (int v : back_adj[u]) {
if (!visitedd[v]) {
int cnt = 0;
for (int w : back_adj[v]) {
if (!visitedd[w]) ++cnt;
}
cands.emplace_back(cnt, v);
}
}
sort(cands.begin(), cands.end());
for (auto& p : cands) {
if (dfs_backward(p.second, back_adj)) return true;
}
pathh.pop_back();
visitedd[u] = 0;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m_global;
vector<int> dummy(10);
for (int &x : dummy) cin >> x;
vector<vector<int>> adj(n + 1);
vector<int> indeg(n + 1, 0), outdeg(n + 1, 0);
for (int i = 0; i < m_global; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
indeg[v]++;
outdeg[u]++;
}
int source = -1;
for (int i = 1; i <= n; i++) {
if (indeg[i] == 0) {
source = i;
break;
}
}
int sinkk = -1;
for (int i = 1; i <= n; i++) {
if (outdeg[i] == 0) {
sinkk = i;
break;
}
}
visitedd.assign(n + 1, 0);
bool foundd = false;
pathh.clear();
if (source != -1) {
foundd = dfs_forward(source, adj);
} else if (sinkk != -1) {
vector<vector<int>> back_adj(n + 1);
for (int u = 1; u <= n; u++) {
for (int v : adj[u]) {
back_adj[v].push_back(u);
}
}
foundd = dfs_backward(sinkk, back_adj);
if (foundd) {
reverse(pathh.begin(), pathh.end());
}
} else {
int max_od = -1;
int st = -1;
for (int i = 1; i <= n; i++) {
if (outdeg[i] > max_od || (outdeg[i] == max_od && (st == -1 || i < st))) {
max_od = outdeg[i];
st = i;
}
}
if (st != -1) {
foundd = dfs_forward(st, adj);
}
}
if (pathh.empty()) {
cout << 1 << '\n' << 1 << '\n';
} else {
cout << pathh.size() << '\n';
for (size_t j = 0; j < pathh.size(); j++) {
if (j > 0) cout << " ";
cout << pathh[j];
}
cout << '\n';
}
return 0;
} |