Pagini recente » Cod sursa (job #2256020) | Cod sursa (job #1074296) | Cod sursa (job #2198806) | Cod sursa (job #1831139) | Cod sursa (job #2139573)
#include <cstdio>
#include <vector>
int vertices, edges, u, v, conexParts;
std :: vector<int> adj[100001]; bool visited[100001];
void DFS(int current)
{
visited[current] = true;
for(int child : adj[current])
{
if(!visited[child]) DFS(child);
}
}
__attribute__((always_inline)) void read(int &num)
{
static char inBuffer[0x2AB980];
static unsigned int p = 0x2AB97F; num = 0x0;
while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
{
++p != 0x2AB980 || (fread(inBuffer, 0x1, 0x2AB980, stdin), p = 0x0);
}
while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
{
num = num * 0xA + inBuffer[p] - 0x30;
++p != 0x2AB980 || (fread(inBuffer, 0x1, 0x2AB980, stdin), p = 0x0);
}
}
int main()
{
freopen("dfs.in", "r", stdin);
freopen("dfs.out", "w", stdout);
read(vertices); read(edges);
for(int i = edges; i; --i)
{
read(u), read(v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = vertices; i; --i)
{
if(!visited[i])
{
DFS(i);
++conexParts;
}
}
printf("%d", conexParts);
return 0;
}