Pagini recente » Cod sursa (job #775736) | Cod sursa (job #1985289) | Cod sursa (job #2703188) | Cod sursa (job #2767056) | Cod sursa (job #923765)
Cod sursa(job #923765)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
int n;
vector <int> G[100010];
queue <int> Q1, Q2;
bool viz[100010];
inline void Read()
{
ifstream f ("nivele2.in");
f>>n;
int x, y;
for (int i = 1; i<n; i++)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
f.close();
}
inline void Solve()
{
ofstream g("nivele2.out");
int x, nivel = 1;
bool gata = false;
vector <int>::iterator it;
Q1.push(1);
while (!gata)
{
g<<"nivelul "<<nivel<<":";
while (!Q1.empty())
{
x = Q1.front();
Q1.pop();
viz[x] = true;
g<<" "<<x;
for (it = G[x].begin(); it!=G[x].end(); it++)
if (viz[*it] == false)
Q2.push(*it);
}
while (!Q2.empty())
{
Q1.push(Q2.front());
Q2.pop();
}
g<<"\n";
if (Q1.empty())
gata = true;
}
g.close();
}
int main()
{
Read();
Solve();
return 0;
}