Cod sursa(job #1479386)

Utilizator superstar1998Moldoveanu Vlad superstar1998 Data 31 august 2015 11:08:23
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;
struct nod{int x;nod* urm;};
nod* L[100001];
int n,m;
bool viz[100001];
void Adauga_Leg(nod*& p, int x)
{
    nod* c = new nod;
    c->x=x;
    c->urm=p;
    p=c;
}
void Citeste()
{
    ifstream f("dfs.in");
    f>>n>>m;
    int x,y;
    while(f>>x>>y)
    {
        Adauga_Leg(L[x],y);
        Adauga_Leg(L[y],x);
    }
    f.close();
}
void DF(int x)
{
    viz[x]=true;
    for(nod*c = L[x];c;c=c->urm)
        if(!viz[c->x]) DF(c->x);
}
void Scrie()
{
    ofstream g("dfs.out");
    g<<m;
    g.close();
}
int main()
{
    Citeste();
    m=0;
    for(int i=1;i<=n;i++)
        if(!viz[i])
        {
            DF(i);
            m++;
        }
    Scrie();
    return 0;
}