Submission #317089

# Submission time Handle Problem Language Result Execution time Memory
317089 2020-10-29T03:07:28 Z caoash Potemkin cycle (CEOI15_indcyc) C++14
10 / 100
1000 ms 231552 KB
#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

namespace output {
  void pr(int x) {
    cout << x;
  }
  void pr(long x) {
    cout << x;
  }
  void pr(ll x) {
    cout << x;
  }
  void pr(unsigned x) {
    cout << x;
  }
  void pr(unsigned long x) {
    cout << x;
  }
  void pr(unsigned long long x) {
    cout << x;
  }
  void pr(float x) {
    cout << x;
  }
  void pr(double x) {
    cout << x;
  }
  void pr(long double x) {
    cout << x;
  }
  void pr(char x) {
    cout << x;
  }
  void pr(const char * x) {
    cout << x;
  }
  void pr(const string & x) {
    cout << x;
  }
  void pr(bool x) {
    pr(x ? "true" : "false");
  }

  template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
  template < class T > void pr(const T & x);

  template < class T, class...Ts > void pr(const T & t,
    const Ts & ...ts) {
    pr(t);
    pr(ts...);
  }
  template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
    pr("{", x.f, ", ", x.s, "}");
  }
  template < class T > void pr(const T & x) {
    pr("{"); // const iterator needed for vector<bool>
    bool fst = 1;
    for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
    pr("}");
  }

  void ps() {
    pr("\n");
  } // print w/ spaces
  template < class T, class...Ts > void ps(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(" ");
    ps(ts...);
  }

  void pc() {
    cout << "]" << endl;
  } // debug w/ commas
  template < class T, class...Ts > void pc(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(", ");
    pc(ts...);
  }
  #define dbg(x...) pr("[", #x, "] = ["), pc(x);
}

#ifndef ONLINE_JUDGE
using namespace output;
#else
using namespace output;
#define dbg(x...)
#endif

const int MX = 2000;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

int n, m;

int inc(int u, int v) {
    return MX * u + v;
}

set<int> adj[MX];
vi fin[MX * MX];
bool vis[MX * MX];
vector<pi> ed;
int anc[MX * MX];
int dep[MX * MX];
bool found = false;
vi back[MX * MX];

void dfs(int v, int p) {
    vis[v] = true;
    // dbg(v, p);
    for (int to : fin[v]) {
        if (!found && to != p) {
            if (vis[to]) {
                if (dep[v] > dep[to]) back[v].pb(to);
            } else {
                dep[to] = dep[v] + 1;
                anc[to] = v;
                dfs(to, v);
                // dbg("GO", to/2000, to%2000);
            }
        }
    }
}

pi ret = mp(-1, -1);

void dfs2(int v, int p) {
    if (found) return;
    vis[v] = true;
    // dbg(v, back[v]);
    if (!back[v].empty()) {
        int best = -1;
        int mx = 0;
        for (int to : back[v]) {
            if (dep[to] > mx) {
                mx = dep[to];
                best = to;
            }
        }
        // dbg(v, best);
        ret = mp(v, best);
        found = true;
        return;
    }
    for (int to : fin[v]) {
        if (to != p && !vis[to]) {
            dfs2(to, v);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m; 
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].insert(v), adj[v].insert(u);
        ed.pb(mp(u, v));
        ed.pb(mp(v, u));
    }
    int cnt = 0;
    set<pi> nods;
    for (int i = 0; i < sz(ed); i++) {
        int u = ed[i].f, v = ed[i].s;
        for (int j = 0; j < n; j++) {
            // dbg(u, v, j, adj[v].count(j), adj[u].count(j));
            if (j != u && j != v && adj[v].count(j) && !adj[u].count(j)) {
                // dbg(u, v, v, j);
                // ps(inc(u, v), inc(v, j));
                fin[inc(u, v)].pb(inc(v, j));
                fin[inc(v, j)].pb(inc(u, v));
                nods.insert(mp(u, v));
                nods.insert(mp(v, j));
                ++cnt;
            }
        }
    }
    for (auto &x : nods) {
        // dbg(x.f, x.s);
        if (found) break;
        if (!vis[inc(x.f, x.s)]) dfs(inc(x.f, x.s), -1);
    }
    memset(vis, false, sizeof(vis));
    for (auto &x : nods) {
        // dbg(x.f, x.s);
        if (!vis[inc(x.f, x.s)]) dfs2(inc(x.f, x.s), -1);
    }
    if (!found) {
        cout << "no" << '\n';
        return 0;
    }
    /*
    dbg(ret);
    dbg(ret.f, anc[ret.f]);
    */
    int curr = ret.f;
    int to = ret.s;
    while(curr != to) {
        // dbg(curr / 2000, curr % 2000);
        cout << (curr % 2000) + 1 << " "; 
        curr = anc[curr];
    }
    cout << (to % 2000) + 1 << " ";
}

# Verdict Execution time Memory Grader output
1 Execution timed out 1062 ms 219096 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 131 ms 192376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 133 ms 192248 KB Repeated vertex
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 133 ms 193272 KB Repeated vertex
# Verdict Execution time Memory Grader output
1 Incorrect 144 ms 194040 KB Repeated vertex
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1069 ms 222712 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 222 ms 200700 KB Repeated vertex
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1065 ms 207628 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1056 ms 231552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1059 ms 199440 KB Time limit exceeded
2 Halted 0 ms 0 KB -