Cod sursa(job #2470075)

Utilizator andreighinea1Ghinea Andrei-Robert andreighinea1 Data 8 octombrie 2019 17:54:31
Problema Mesaj4 Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
#define Nmax 100002
#define Mmax 100002

using namespace std;

FILE *f=fopen("mesaj4.in","rt");
ofstream o("mesaj4.out");

struct muchie{
    int x,y;
} sol[Mmax];

int i,n,m,x,y,nr;
bool used[Nmax];

struct node{
    int x;
    node *next;
} *g[Nmax];

void init(int x,int y){
    node *t=new node();
    t->x=y;
    t->next=g[x];
    g[x]=t;
}

void read(){
    fscanf(f,"%d%d",&n,&m);
    for(i=1;i<=m;++i){
        fscanf(f,"%d%d",&x,&y);
        init(x,y);
        init(y,x);
    }
}

void dfs(int x, int t){
    if(t){
        sol[++nr].x=t;
        sol[nr].y=x;
    }
    used[x]=true;
    int v;
    for(node *p=g[x];p;p=p->next){
        v=p->x;
        if(!used[v])
            dfs(v,x);
    }
    if(t)
        o << x << " " << t << '\n';
}

void solve(){
    o << (n-1)*2 << '\n';

    dfs(1,0);

    for(i=1;i<=nr;++i){
        o << sol[i].x << " " << sol[i].y << '\n';
    }
}

int main()
{
    read();
    solve();
    return 0;
}