Pagini recente » am_piramide | Istoria paginii runda/just_4_me/clasament | Istoria paginii runda/antrenament_1/clasament | Cod sursa (job #2002025) | Cod sursa (job #2020163)
#include <fstream>
using namespace std;
int n, m, viz[100005], cnt;
struct nod
{
int x;
nod *leg;
};
nod *v[100001];
void add(nod *&dest, int vf)
{
nod *p;
p = new nod;
p -> x = vf;
p -> leg = dest;
dest = p;
}
void citire()
{
ifstream f("dfs.in");
f >> n >> m;
int i, x, y;
for(i = 1; i <= m; i++)
{
f >> x >> y;
add(v[x], y);
add(v[y], x);
}
f.close();
}
void DFS(int vf)
{
nod *p;
viz[vf] = 1;
for(p = v[vf]; p != NULL; p = p -> leg)
if(!viz[p -> x]) DFS(p -> x);
}
int main()
{
ofstream g("dfs.out");
citire();
for(int i = 1; i <= n; i++)
if(!viz[i])
{
cnt++;
DFS(i);
}
g << cnt;
return 0;
}