Cod sursa(job #2218232)

Utilizator AlexDabuDabu Alexandru AlexDabu Data 3 iulie 2018 22:09:41
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

#define NMAX 100002

ifstream fin("dfs.in");
ofstream fout("dfs.out");

int n, m;
vector <int> vecini[NMAX];
int maxim = 0;
queue <int> chain;

void Read(void)
{
	fin >> n >> m;
	int a, b;
	for (int i = 0; i < m; i++)
	{
		fin >> a >> b;
		vecini[a].push_back(b);
		vecini[b].push_back(a);
	}
}

void LongestChain(void)
{
	int node, currNode, currLength;
	bool wasCrossed[NMAX] = { false };
	maxim = 0;
	for (int i = 1; i <= n; i++)
	{
		currLength = 0;
		if (!wasCrossed[i])
		{
			chain.push(i);
			wasCrossed[i] = true;
			currLength++;
			while (!chain.empty())
			{
				currNode = chain.front();
				chain.pop();
				for (unsigned int i = 0; i < vecini[currNode].size(); i++)
				{
					node = vecini[currNode].at(i);
					if (!wasCrossed[node])
					{
						currLength++;
						wasCrossed[node] = true;
						chain.push(node);
					}
				}
			}
		}
		if (currLength > maxim)
		{
			maxim = currLength;
		}
	}
}

int main(void)
{
	Read();
	LongestChain();
	fout << maxim;
	return 0;
}