Cod sursa(job #1788282)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 25 octombrie 2016 21:09:57
Problema Santa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.61 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
} in( "santa.in" );

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
} out( "santa.out" );

const int DIM = 5e4 + 5;

int low[DIM], lev[DIM];

vector<int> g[DIM], pa, ap, st, bc, aux;
bitset<DIM> ma, op; vector< vector<int> > bcc;

void dfs( int x, int f ) {
    ma[x] = 1; st.push_back( x );
    low[x] = lev[x] = lev[f] + 1;
    for( auto y : g[x] ) {
        if( y == f ) {
            continue; }
        if( ma[y] == 1 ) {
            low[x] = min( low[x], lev[y] ); }
        else {
            dfs( y, x ); op[x] = op[x] | op[y];
            low[x] = min( low[x], low[y] );
            if( lev[x] <= low[y] ) {
                aux.clear(); int z;
                do {
                    z = st.back(); st.pop_back();
                    aux.push_back( z );
                } while( z != y );
                aux.push_back( x );
                if( op[y] == 1 ) {
                    ap.push_back( x );
                    bcc.push_back( aux ); } } } }
    return; }

bool backt( int x, int z, int cnt, int sz ) {
    ma[x] = 1; if( cnt > 1 ) pa.push_back( x );
    if( cnt == sz && (z == -1 || x == z) )
        return true;
    for( auto y : g[x] ) {
        if( ma[y] == 0 && (cnt == sz - 1 || y != z) && backt( y, z, cnt + 1, sz ) ) {
            return true; } }
    ma[x] = 0; pa.pop_back(); return false; }

int main( int argc, const char *argv[] ) {

    int n, m; in >> n >> m;
    for( int i = 1; i <= m; i ++ ) {
        int x, y; in >> x >> y;
        g[x].push_back( y );
        g[y].push_back( x ); }
    int s, e, q, ok = 1; in >> s >> e >> q;

    op[s] = 1; ap.push_back( s ); dfs( e, 0 );
    if( op[e] == 0 ) {
        ok = 0; }
    if( find( bcc[0].begin(), bcc[0].end(), q ) == bcc[0].end() ) {
        reverse( bcc[0].begin(), bcc[0].end() );
        reverse( ap.begin(), ap.end() ); }
    if( find( bcc[0].begin(), bcc[0].end(), q ) == bcc[0].end() ) {
        ok = 0; }

    ap[0] = q; ap[ap.size() - 1] = -1; pa.push_back( q );
    for( int i = 0; i < bcc.size(); i ++ ) {
        for( auto x : bcc[i] )
            ma[x] = 0;
        ok &= backt( ap[i], ap[i + 1], 1, bcc[i].size() ); }

    if( ok == 0 ) {
        out << "No chance" << "\n";
        return 0; }
    int x = pa.size(); out << x << "\n";
    for( auto x : pa ) {
        out << x << " "; }
    out << "\n";

    return 0; }