Mai intai trebuie sa te autentifici.

Cod sursa(job #1472519)

Utilizator akaprosAna Kapros akapros Data 17 august 2015 11:56:05
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define maxN 100002
using namespace std;
int n, m, i, j, used[maxN], nre;
vector < int > V[maxN];
struct edge
{
    int x;
    int y;
}sol[maxN];
void read()
{
    int x, y;
    freopen("mesaj4.in", "r", stdin);
    scanf("%d %d", &n, &m);
    while (m --)
    {
        scanf("%d %d", &x, &y);
        V[x].push_back(y);
        V[y].push_back(x);
    }
}
void dfs(int x)
{
    int i;
    used[x] = 1;
    for (i = 0;i < V[x].size(); ++ i)
        if (!used[V[x][i]])
    {
        dfs(V[x][i]);
        sol[++ nre].x = V[x][i];
        sol[nre].y = x;
    }
}
void solve()
{
    dfs(1);
}
void write()
{
    freopen("mesaj4.out", "w", stdout);
    if (nre != n - 1)
        printf("%d\n", -1);
    else
    {
        printf("%d\n", 2 * nre);
        for (i = 1; i <= nre; ++ i)
            printf("%d %d\n", sol[i].x, sol[i].y);
        for (i = nre; i >= 1; -- i)
            printf("%d %d\n", sol[i].y, sol[i].x);
    }
}
int main()
{
    read();
    solve();
    write();
    return 0;
}