File size: 4,665 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if(!(cin >> n >> m)) return 0;
vector<int> a(10);
for (int i = 0; i < 10; ++i) cin >> a[i];
vector<vector<int>> adj(n+1), radj(n+1);
vector<int> deg(n+1,0);
vector<pair<int,int>> edges;
edges.reserve(m);
for (int i = 0; i < m; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
radj[v].push_back(u);
deg[u]++;
edges.emplace_back(u,v);
}
for (int i = 1; i <= n; ++i) {
sort(adj[i].begin(), adj[i].end());
sort(radj[i].begin(), radj[i].end());
adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());
radj[i].erase(unique(radj[i].begin(), radj[i].end()), radj[i].end());
}
auto hasEdge = [&](int u, int v)->bool{
auto &vec = adj[u];
return binary_search(vec.begin(), vec.end(), v);
};
vector<int> bestPath;
bestPath.reserve(n);
auto buildFromSeed = [&](int seed)->vector<int>{
vector<int> next(n+1, 0), prev(n+1, 0);
vector<char> inPath(n+1, 0);
int head = seed, tail = seed;
inPath[seed] = 1;
bool changed = true;
while (changed) {
changed = false;
// extend tail
bool extended = true;
while (extended) {
extended = false;
for (int v : adj[tail]) {
if (!inPath[v]) {
next[tail] = v;
prev[v] = tail;
tail = v;
inPath[v] = 1;
changed = true;
extended = true;
break;
}
}
}
// extend head (using reverse edges)
extended = true;
while (extended) {
extended = false;
for (int u : radj[head]) {
if (!inPath[u]) {
prev[head] = u;
next[u] = head;
head = u;
inPath[u] = 1;
changed = true;
extended = true;
break;
}
}
}
// insertion pass
bool inserted_any = false;
for (int u = 1; u <= n; ++u) {
if (inPath[u]) continue;
for (int v : adj[u]) {
if (!inPath[v]) continue;
int a = prev[v];
if (a != 0 && hasEdge(a, u)) {
// insert u between a and v
int b = v;
int an = next[a]; // should be b
(void)an;
next[a] = u;
prev[u] = a;
next[u] = b;
prev[b] = u;
inPath[u] = 1;
inserted_any = true;
changed = true;
break;
}
}
}
if (inserted_any) {
// after insertions, try to extend ends again in next loop
}
}
// build path vector
vector<int> path;
path.reserve(n);
int cur = head;
while (cur != 0) {
path.push_back(cur);
cur = next[cur];
}
return path;
};
// choose seeds: max out-degree, some random vertices, and 1
vector<int> seeds;
seeds.push_back(1);
int maxd = 1;
for (int i = 2; i <= n; ++i) if (deg[i] > deg[maxd]) maxd = i;
seeds.push_back(maxd);
// random seeds
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> dist(1, n);
int extra = 6; // number of random tries
for (int i = 0; i < extra; ++i) seeds.push_back(dist(rng));
// deduplicate seeds
sort(seeds.begin(), seeds.end());
seeds.erase(unique(seeds.begin(), seeds.end()), seeds.end());
for (int s : seeds) {
vector<int> cand = buildFromSeed(s);
if (cand.size() > bestPath.size()) bestPath.swap(cand);
if ((int)bestPath.size() == n) break;
}
// Fallback if somehow empty
if (bestPath.empty()) bestPath.push_back(1);
cout << bestPath.size() << "\n";
for (size_t i = 0; i < bestPath.size(); ++i) {
if (i) cout << ' ';
cout << bestPath[i];
}
cout << "\n";
return 0;
} |