Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <climits>
- #include <set>
- #include <queue>
- using namespace std;
- fstream f("text.txt");
- ofstream o("branza.out");
- int n, e;
- vector<int>g[105];
- int a, b;
- bool v[105];
- int componente;
- vector<pair<int, int>>graf[105];
- int d;
- void bfs(int cv, int data)
- {
- queue<int>coada;
- vector<bool>w(105, false);
- coada.push(cv);
- int muchii = 1;
- graf[data].push_back({ cv,muchii });
- v[cv] = true;
- w[cv] = true;
- while (!coada.empty())
- {
- int nod = coada.front();
- coada.pop();
- for (size_t i = 0; i < g[nod].size(); i++)
- {
- int vecin = g[nod][i];
- if (w[vecin] == false)
- {
- coada.push(vecin);
- w[vecin] = true;
- v[vecin] = true;
- graf[data].push_back({ vecin,muchii++ });
- }
- }
- }
- }
- int main()
- {
- f >> n >> e;
- for (size_t i = 1; i <= n; i++)
- {
- f >> a >> b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- for (size_t i = 1; i <= n; i++)
- {
- if (!v[i])
- {
- bfs(i, d);
- d++;
- }
- }
- for (size_t i = 0; i < d; i++)
- {
- for (size_t j = 0; j < graf[i].size(); j++)
- {
- cout << graf[i][j].first << " ";
- }
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement