Submission #310891

# Submission time Handle Problem Language Result Execution time Memory
310891 2020-10-08T11:22:13 Z HynDuf Potemkin cycle (CEOI15_indcyc) C++11
100 / 100
344 ms 90816 KB
#include <bits/stdc++.h>

#define task "I"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1002, NN = 1e5 + 1;
int n, m, dep[NN << 1], par[NN << 1];
bool d[N][N], in[NN << 1];
vi g[N], gr[NN << 1];
ii edge[NN];
vi res;
void dfs(int u, int p)
{
    in[u] = 1;
    par[u] = p;
    dep[u] = dep[p] + 1;
    for (int v : gr[u])
    {
        if (!dep[v]) dfs(v, u);
        else if (in[v] && res.empty())
        {
            while (u != v)
            {
                res.eb(edge[u >> 1].F);
                res.eb(edge[u >> 1].S);
                u = par[u];
            }
            res.eb(edge[v >> 1].F);
            res.eb(edge[v >> 1].S);
            return;
        }
    }
    in[u] = 0;
}
void fix()
{
    for (int i = 2; i < SZ(res); i += 2) if (res[i] != res[i - 1]) swap(res[i], res[i + 1]);

}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n >> m;
    rep(i, 0, m - 1)
    {
        int u, v;
        cin >> u >> v;
        g[u].eb(i);
        g[v].eb(i);
        d[u][v] = d[v][u] = 1;
        edge[i] = {u, v};
    }
    rep(i, 0, m - 1)
    {
        int u = edge[i].F, v = edge[i].S;
        for (int j : g[v]) if (j != i)
        {
            int id = (edge[j].F == v) ? (j << 1) : ((j << 1) | 1), w = (edge[j].F == v) ? edge[j].S : edge[j].F;
            if (d[u][w]) continue;
            gr[i << 1].eb(id);
            //cerr << "Edge: " << (i << 1) << ' ' << id << '\n';
        }
        for (int j : g[u]) if (j != i)
        {
            int id = (edge[j].F == u) ? (j << 1) : ((j << 1) | 1), w = (edge[j].F == u) ? edge[j].S : edge[j].F;
            if (d[v][w]) continue;
            gr[(i << 1) | 1].eb(id);
            //cerr << "Edge: " << ((i << 1) | 1) << ' ' << id << '\n';
        }
    }
    rep(i, 0, 2 * m - 1) if (!dep[i] && res.empty()) dfs(i, i);
    if (res.empty())
    {
        cout << "no";
        return 0;
    }
    //PR(res, 0, SZ(res) - 1);
    fix();
    if (res[1] != res[2])
    {
        swap(res[0], res[1]);
        fix();
    }
    for (int i = 0; i < SZ(res); i += 2) cout << res[i] << ' ';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5376 KB Output is correct
2 Correct 5 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5760 KB Output is correct
2 Correct 7 ms 5760 KB Output is correct
3 Correct 13 ms 7680 KB Output is correct
4 Correct 16 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6912 KB Output is correct
2 Correct 12 ms 7040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 22648 KB Output is correct
2 Correct 33 ms 9720 KB Output is correct
3 Correct 115 ms 22648 KB Output is correct
4 Correct 33 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 51192 KB Output is correct
2 Correct 148 ms 55160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 7928 KB Output is correct
2 Correct 238 ms 8952 KB Output is correct
3 Correct 306 ms 90744 KB Output is correct
4 Correct 344 ms 90816 KB Output is correct