Submission #432162

# Submission time Handle Problem Language Result Execution time Memory
432162 2021-06-17T22:53:46 Z gmyu Senior Postmen (BOI14_postmen) C++14
55 / 100
500 ms 92680 KB
/*
ID: USACO_template
LANG: C++
PROG: https://oj.uz/problem/view/BOI14_postmen
*/
#include <iostream>  //cin , cout
#include <fstream>   //fin, fout
#include <stdio.h>   // scanf , pringf
#include <cstdio>
#include <algorithm> // sort , stuff
#include <stack>     // stacks
#include <queue>     // queues
#include <map>
#include <string>
#include <string.h>
#include <set>

using namespace std;

typedef pair<int, int>          pii;
typedef vector<int>             vi;     /// adjlist without weight
typedef vector<pii>             vii;    /// adjlist with weight
typedef vector<pair<int,pii>>   vpip;   /// edge with weight
typedef long long               ll;

#define mp  make_pair
#define ff  first
#define ss  second
#define pb  push_back
#define sz(x)   (int)(x).size()

const int MOD = 1e9+7;  // 998244353;
const int MX  = 2e5+5;   //
const ll  INF = 1e18;    //

#define MAXV 500007
#define MAXE 500007

const int xdir[4] = {1,0,-1,0}, ydir[4] = {0,1,0,-1}; /// 4 directions
struct NODE {
    int x, y;
    int val;
    int visited;
    bool operator< (NODE b) const { return (x == b.x) ? (y < b.y) : (x < b.x); }
};
struct EDGE {
    int from, to;
    ll weight;
    bool operator<(EDGE other) const { return weight < other.weight; }
};

bool debug;

int N, M;
vii adjlist[MAXV];
int nxtEdge[MAXV];
int visited[MAXV], walked[MAXE];
vi cycle;
int svx;

void dfs(int u) {
    visited[u] = 1;

    while(nxtEdge[u] < sz(adjlist[u])) { /// still have an edge to walk
        int j = nxtEdge[u];
        int v = adjlist[u][j].ff, e = adjlist[u][j].ss;
        nxtEdge[u]++;

        if(walked[e]) continue;

        walked[e] = 1;
        if(visited[v]) { /// find a cycle
            cycle.pb(u); svx = v; return;
        }

        dfs(v);

        /// backtrack
        cycle.pb(u);
        if(svx != u) {
            if(svx>0) return;
        } else {
            for(auto x : cycle) {
                cout << x << " ";
                visited[x] = 0;     // can re-use in other cycle
            }
            cout << endl;

            /// now use u as the starting point of a new cycle
            visited[u] = 1;
            svx = -1;
            vi().swap(cycle);
        }

    }

}

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

    cin >> N >> M;
    for(int i=1; i<=M; i++) {
        int a, b; cin >> a >> b;
        adjlist[a].pb(mp(b,i)); adjlist[b].pb(mp(a, i));
    }

    for(int i=1; i<=N; i++)
        if(nxtEdge[i] < sz(adjlist[i])) dfs(i);
        /// similar to Eulerian tour, prioritize pick edges first

    if(debug) cout << endl << "EOL" << endl;

}

/**
10 15
1 3
5 1
2 3
9 2
3 4
6 3
4 5
7 4
4 8
5 7
8 5
6 7
7 8
8 10
10 9
*/
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11984 KB Output is correct
2 Correct 8 ms 12064 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 10 ms 12108 KB Output is correct
5 Correct 8 ms 12108 KB Output is correct
6 Correct 9 ms 12208 KB Output is correct
7 Correct 17 ms 12716 KB Output is correct
8 Correct 8 ms 12312 KB Output is correct
9 Correct 55 ms 15572 KB Output is correct
10 Correct 10 ms 12200 KB Output is correct
11 Correct 9 ms 12236 KB Output is correct
12 Correct 55 ms 16064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12064 KB Output is correct
2 Correct 8 ms 12068 KB Output is correct
3 Correct 7 ms 12000 KB Output is correct
4 Correct 12 ms 12164 KB Output is correct
5 Correct 8 ms 12052 KB Output is correct
6 Correct 10 ms 12236 KB Output is correct
7 Correct 14 ms 12720 KB Output is correct
8 Correct 8 ms 12364 KB Output is correct
9 Correct 49 ms 15556 KB Output is correct
10 Correct 12 ms 12108 KB Output is correct
11 Correct 12 ms 12236 KB Output is correct
12 Correct 57 ms 16036 KB Output is correct
13 Correct 81 ms 28020 KB Output is correct
14 Correct 112 ms 17564 KB Output is correct
15 Correct 116 ms 16936 KB Output is correct
16 Correct 87 ms 28016 KB Output is correct
17 Correct 122 ms 17320 KB Output is correct
18 Correct 138 ms 20024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 11 ms 12180 KB Output is correct
5 Correct 8 ms 12076 KB Output is correct
6 Correct 11 ms 12344 KB Output is correct
7 Correct 16 ms 12680 KB Output is correct
8 Correct 9 ms 12364 KB Output is correct
9 Correct 48 ms 15576 KB Output is correct
10 Correct 11 ms 12236 KB Output is correct
11 Correct 12 ms 12212 KB Output is correct
12 Correct 56 ms 15972 KB Output is correct
13 Correct 80 ms 27992 KB Output is correct
14 Correct 116 ms 17616 KB Output is correct
15 Correct 129 ms 16984 KB Output is correct
16 Correct 84 ms 28040 KB Output is correct
17 Correct 132 ms 17352 KB Output is correct
18 Correct 119 ms 20028 KB Output is correct
19 Execution timed out 534 ms 92680 KB Time limit exceeded
20 Halted 0 ms 0 KB -