Cod sursa(job #2470114)

Utilizator andreighinea1Ghinea Andrei-Robert andreighinea1 Data 8 octombrie 2019 18:33:35
Problema Mesaj4 Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 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],sol2[Mmax];

int i,n,m,x,y,nr,nr2;
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){
        sol2[++nr2].x=t;
        sol2[nr2].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){
        sol[++nr].x=x;
        sol[nr].y=t;
    }
}

void solve(){
    dfs(1,0);

    if(!(nr+nr2)){
        o << "-1\n";
        return;
    }

    o << nr+nr2 << '\n';

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

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