Cod sursa(job #1640747)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 8 martie 2016 19:08:43
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");

int N, M, solution;
pair<int, int> S[100010];
vector<int>L[100010];
bitset<100010>v;

void DFS(int node)
{
    v[node] = 1;

    for(int i = 0; i < L[node].size(); i ++)
    {
        if(v[ L[node][i] ] == 0)
        {
            DFS( L[node][i] );
            solution ++;
            S[solution].x = node;
            S[solution].y = L[node][i];
        }
    }
}

int main()
{
    fin >> N >> M;

    for(int i = 1; i <= M; i ++)
    {
        int a, b;
        fin >> a >> b;
        L[a].push_back(b);
        L[b].push_back(a);
    }

    DFS(1);

    if(solution != N - 1)
    {
        fout << -1;
        return 0;
    }

    fout << 2 * solution << '\n';

    for(int i = 1; i <= solution; i ++)
    {
        fout << S[i].y << " " << S[i].x << '\n';
    }
    for(int i = solution; i >= 1; i --)
    {
        fout << S[i].x << " " << S[i].y << '\n';
    }


    return 0;
}