This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAX_N = 5e3 + 5;
int g[MAX_N][MAX_N], dp[MAX_N][MAX_N], nxt[MAX_N][MAX_N];
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = (i == j ? 0 : 1e9);
nxt[i][j] = j;
}
}
for (int i = 0; i < m; i++) {
int st, et;
cin >> st >> et;
--st; --et;
g[st][et] = g[et][st] = 1;
dp[st][et] = dp[et][st] = 1;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(dp[i][j] > dp[i][k] + dp[k][j]) {
dp[i][j] = dp[i][k] + dp[k][j];
nxt[i][j] = nxt[i][k];
}
}
}
}
auto get_path = [&](int source, int destination) {
vector<int> path;
while(source != destination) {
path.emplace_back(source);
source = nxt[source][destination];
}
return path;
};
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
if (dp[i][j] > 1e7 || dp[j][k] > 1e7 || dp[k][i] > 1e7) continue;
vector<int> A = get_path(i, j);
vector<int> B = get_path(j, k);
vector<int> C = get_path(k, i);
vector<int> all;
for (auto to : A) all.emplace_back(to);
for (auto to : B) all.emplace_back(to);
for (auto to : C) all.emplace_back(to);
vector<int> chk = all;
sort(chk.begin(), chk.end());
chk.erase(unique(chk.begin(), chk.end()), chk.end());
if ((int)chk.size() != (int)all.size() || (int)chk.size() < 4) {
continue;
}
if (chk.size() < 4) continue;
bool valid = true;
for (int cur = 0; cur < (int) all.size(); cur++) {
int cnt = 0;
for (int to = 0; to < (int) all.size(); to++) {
if (to == cur) continue;
cnt += (dp[all[cur]][all[to]] == 1);
}
valid &= (cnt == 2);
}
if (valid) {
for (auto to : all) cout << to + 1 << ' ';
cout << '\n';
return 0;
}
}
}
}
cout << "no" << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |