Pagini recente » Cod sursa (job #3274816) | Cod sursa (job #3163317) | Cod sursa (job #115811) | Cod sursa (job #2266556) | Cod sursa (job #1974503)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int nMax = 100003;
struct Nod{
int nod;
Nod *leg;
};
Nod *G[nMax];
bool viz[nMax];
int comp;
inline void Add_edge(int x, int y) {
Nod *p;
p = new Nod;
p->nod = y;
p->leg = G[x];
G[x] = p;
}
inline void Dfs(int node) {
Nod *p;
comp++;
viz[node] = 1;
for(p = G[node]; p != NULL; p = p->leg) {
int j = p -> nod;
if(!viz[j]) {
Dfs(j);
}
}
}
int main()
{
int n, m, x, y;
Nod *p;
f >> n >> m;
for(int i = 1; i <= m; i++) {
f >> x >> y;
Add_edge(x, y);
Add_edge(y, x);
}
Dfs(1);
g << comp << "\n";
return 0;
}