Cod sursa(job #2162308)

Utilizator RazorBestPricop Razvan Marius RazorBest Data 12 martie 2018 09:48:04
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.55 kb
#include <cstdio>
#include <cctype>
#include <vector>
using namespace std;

const int buf_size = 16384;
char buf_in[16385], buf_out[16385];
int poz_in = buf_size, poz_out = 0;

char get_ch()
{
    if (poz_in == buf_size)
    {
        fread(buf_in, 1, buf_size, stdin);
        poz_in = 0;
    }
    return buf_in[poz_in++];
}

int get_int()
{
    int x = 0;
    char c;
    while (!isdigit(c = get_ch()));
    x = c - '0';
    while (isdigit(c = get_ch()))
        x = x * 10 + c - '0';

    return x;
}

void print_ch(char c)
{
    if (poz_out == buf_size)
    {
        buf_out[buf_size] = 0;
        fwrite(buf_out, 1, buf_size, stdout);
        poz_out = 0;
    }
    buf_out[poz_out++] = c;
}

void print_int(int x)
{
    if (x == 0)
        print_ch('0');
    if (x < 0)
    {
        print_ch('-');
        x *= -1;
    }
    int v[20], l = 0;
    while (x)
    {
        v[l++] = x % 10;
        x /= 10;
    }
    while (l--)
        print_ch(v[l] + '0');
}

void clear_buf()
{
    fwrite(buf_out, 1, poz_out, stdout);
}

int m;
vector<pair<int, int> > adj[10];
int sol[50001], len;
bool grd[10], viz[10], M[50001], vz[50001];

void dfs(int nod)
{
    viz[nod] = 1;
    for (int i = 0; i < adj[nod].size(); i++)
    {
        int next = adj[nod][i].first;
        if (next < 0) next = -next;
        if (!viz[next])
        {
            M[adj[nod][i].second] = 1;
            dfs(next);
        }
    }
}

void euler(int nod)
{
    while (adj[nod].size())
    {
        bool ok = true;
        for (int i = 0; i < adj[nod].size() && ok; i++)
        {
            int next = adj[nod][i].first, ind = adj[nod][i].second;
            bool rev = false;

            if (next < 0)
            {
                rev = true;
                next = -next;
            }

            if (!vz[ind] && !M[ind])
            {
                print_int(ind); print_ch(' '); print_int((int)rev); print_ch('\n');//fout << ind << ' ' << rev << '\n';
                vz[ind] = 1;
                nod = next;
                ok = false;
            }
        }
        while (adj[nod].size() && ok)
        {
            int next = adj[nod].back().first, ind = adj[nod].back().second;
            bool rev = false;

            if (next < 0)
            {
                next = -next;
                rev = true;
            }

            adj[nod].pop_back();
            if (!vz[ind])
            {
                print_int(ind); print_ch(' '); print_int((int)rev); print_ch('\n');//fout << ind << ' ' << rev << '\n';
                vz[ind] = 1;
                nod = next;
                ok = false;
            }
        }
    }
}

int main()
{
    freopen("domino.in", "r", stdin);
    freopen("domino.out", "w", stdout);

    int x, y, start = -1, fin = -1;

    m = get_int();
    for (int i = 1; i <= m; i++)
    {
        x = get_int();
        y = get_int();
        adj[x].push_back(make_pair(y, i));
        if (x != y)
            adj[y].push_back(make_pair(-x, i));
        grd[x] = !grd[x]; grd[y] = !grd[y];
    }

    for (int i = 0; i < 10; i++)
    {
        if (grd[i] ^ 1)
            continue;
        if (start == -1)
            start = i;
        else if (fin == -1)
            fin = i;
        else
        {
            print_int(0);
            clear_buf();
            return 0;
        }
    }

    if (start == -1)
        start = 1;

    print_int(1); print_ch('\n');

    dfs(start);
    euler(start);

    clear_buf();
}