Cod sursa(job #526940)

Utilizator dacyanMujdar Dacian dacyan Data 29 ianuarie 2011 23:24:20
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <vector>
#define DIM 100000
using namespace std;


vector<vector<int> > G;
long T, n, m, nr;
int x1[DIM], x2[DIM];
bool s[DIM];

void DF(int x);

int main()
{
    ifstream fin("mesaj4.in");
    fin >> n >> m;
    G.resize(n+1);
    int x, y;
    while (fin >> x >> y)
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }    
    fin.close();
    
    DF(1);
    
    ofstream fout("mesaj4.out");
    if (nr < n) fout << "-1\n";
    else
    {
        fout << 2*T << '\n';
        for (int i = T; i >= 1; --i)
                fout << x1[i] << ' ' << x2[i] << '\n';
        for (int i = 1; i <= T; ++i)
                fout << x1[i] << ' ' << x2[i] << '\n';
    }   
    fout.close();
    return 0;
}

void DF(int x)
{
    nr++;
    s[x] = true;
    for (int i = 0; i < G[x].size(); ++i)
    {
        int son = G[x][i];
        if (s[son]) continue;    
        x1[++T] = x; 
        x2[T] = son;
        DF(son);
    }
}