Submission #53896

# Submission time Handle Problem Language Result Execution time Memory
53896 2018-07-01T15:53:20 Z paulica Potemkin cycle (CEOI15_indcyc) C++14
90 / 100
78 ms 6308 KB
#include <bits/stdc++.h>
using namespace std;

#define _ << " _ " <<
#define TRACE(x) cout << #x << " = " << x << endl

#define fi first
#define se second
typedef pair<int, int> pii;

const int MaxN = 1010, MaxR = 1e5 + 10;
int n, r;

vector<pii> e[MaxN];
bool con[MaxN][MaxN];

pii edg[MaxR];
bool vis[MaxR], stk[MaxR];

//int pre[MaxR];
vector<int> path;

void dfs(int x, int y, int id) {
    vis[id] = true;
    stk[id] = true;
    path.push_back(y);

    for (pii z : e[y]) {
        if (!con[x][z.fi] && z.fi != x) {
            if (!vis[z.se]) {
                //pre[z.se] = id;
                dfs(y, z.fi, z.se);
            } else if (stk[z.se]) {
                vector<int> cyc;
              //  TRACE("Yay");

                int pos[MaxN];
                for (int i = 0; i < MaxN; i++) pos[i] = -1;
                for (int i = 0; i < path.size(); i++) {
                    if (pos[path[i]] != -1) {
                     //   TRACE(pos[path[i]] _ i);
//                        copy(path.begin() + pos[path[i]], path.begin() + i, cyc.begin());
                        //cyc = vector(path.begin() + pos[path[i]], path.begin() + i);
                        for (int j = pos[path[i]]; j < i; j++) cyc.push_back(path[j]);
                        break;
                    }
                    pos[path[i]] = i;
                }
                //for (int i : cyc) TRACE(i);
                //exit(0);
                
                int m = cyc.size();
                for (int len = 3; len < m; len++)
                    for (int i = 0; i < m; i++) {
                        if (con[cyc[i]][cyc[(i + len) % m]]) {
                            for (int j = (i + 1) % m; j != i; j = (j + 1) % m)
                                cout << cyc[j] << " ";
                            cout << cyc[i];

                            exit(0);                
                        }
                    }
            }
        }
    }

    stk[id] = false;
    path.pop_back();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> r;
    
    for (int i = 0; i < r; i++) {
        int x, y;
        cin >> x >> y;
        
        e[x].push_back({y, i});
        e[y].push_back({x, i});

        con[x][y] = con[y][x] = true;

        edg[i] = {x, y};
    }

    for (int i = 0; i < r; i++) {
        if (!vis[i]) {
            path.push_back(edg[i].fi);
            dfs(edg[i].fi, edg[i].se, i);
            path.pop_back();
        }
    }

    cout << "no";

    return 0;
}

Compilation message

indcyc.cpp: In function 'void dfs(int, int, int)':
indcyc.cpp:39:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int i = 0; i < path.size(); i++) {
                                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 636 KB Output is correct
5 Correct 2 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 688 KB Output is correct
2 Correct 2 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 764 KB Output is correct
2 Correct 3 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1148 KB Output is correct
2 Correct 3 ms 1148 KB Output is correct
3 Correct 5 ms 1276 KB Output is correct
4 Correct 4 ms 1660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1660 KB Output is correct
2 Correct 3 ms 1660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 3196 KB Output is correct
2 Incorrect 10 ms 3196 KB Wrong adjacency
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3196 KB Output is correct
2 Correct 11 ms 3196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 3836 KB Output is correct
2 Correct 78 ms 3964 KB Output is correct
3 Correct 19 ms 3964 KB Output is correct
4 Correct 36 ms 6308 KB Output is correct