Cod sursa(job #3232954)

Utilizator popescu_georgePopescu George popescu_george Data 2 iunie 2024 08:34:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.34 kb
#include<fstream>
using namespace std;
ifstream F("dfs.in");
ofstream G("dfs.out");
int i,n,m,v[100001],j,z;
int main()
{
    for(F>>n>>j,i=1;i<=n;v[i]=i,++i);
    for(;F>>i>>j;) {
        for(;i!=v[i];z=v[v[i]],v[i]=z,i=z);
        for(;j!=v[j];z=v[v[j]],v[j]=z,j=z);
        if(i!=j)
            --n,v[i]=j;
    }
    return G<<n,0;
}