Pagini recente » Cod sursa (job #132191) | Cod sursa (job #1190954) | Cod sursa (job #1628801) | Cod sursa (job #2254841) | Cod sursa (job #1046050)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
////////////////////
struct nod{
int info;
vector<int*> vecini;
};
//////////////////////////////
bool vizitate[100000] = { false };
nod* a[100000];
////////////////////////
void DFS(nod *x){
vizitate[x->info] = true;
for (int i = 0; i < x->vecini.size(); i++){
if (!vizitate[((nod*)x->vecini[i])->info]){
DFS(((nod*)x->vecini[i]));
}
}
}
////////////////////////////////
int main(){
ifstream f("dfs.in");
int n=0, m=0;
f >> n >> m;
for (int i = 0; i < n; i++){
nod *n = new nod;
n->info = i;
a[i] = n;
}
for (int i = 0; i < m; i++){
int x=0, y=0;
f >> x >> y;
x--; y--;
(a[x]->vecini).push_back((int*)a[y]);
}
int contor = 0;
for (int i = 0; i < n; i++){
if (!vizitate[a[i]->info]){
contor++;
DFS(a[i]);
}
}
ofstream o("dfs.out");
o << contor;
return 0;
}