Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <set>
- #include <vector>
- #include <iterator>
- using namespace std;
- const int Vmax = 100, Emax = Vmax * 2;
- struct Graph
- {
- Graph():head(Vmax),next_el(Emax),k(0){}
- vector < set < int > > terminal;
- vector<int> head;
- vector<int> next_el;
- int k;
- int n=0;
- };
- //set <int> terminal;
- //int terminal[Emax]; //хранит вершины, в которые входят ребра;
- string line;
- void Add(int v, int u,Graph& g) //добавление ребра
- {
- g.k = g.k + 1;
- g.terminal[g.k].insert(u); //указатель куда идет ребро. Пример: 1 4<-на этот элемент
- g.next_el[g.k] = g.head[v]; // если один раз будет вершина, то next_el будет равен 0
- g.head[v] = g.k; //указатель вершины
- }
- void Read(const string& file_name,Graph& g) {
- int size = 1;
- int n, m;
- ifstream file(file_name);
- while (!file.eof()) {
- file >> n; //кол-во вершин
- file >> m; //кол-во ребер
- g.n = n;
- size += n;
- size += n;
- g.terminal.resize(size);
- for (int i = 0; i < m; i++)
- {
- int v, u;
- file >> v >> u;
- Add(v, u, g);
- Add(u, v, g);
- }
- }
- file.close();
- }
- vector<set<int>> Solve(Graph& g) {
- vector<set<int>> data;
- //cout << "Список смежности графа:";
- int j = 0;
- for (int i = 0; i < g.n + 1; i++)
- {
- set<int> temp;
- j = g.head[i];
- //if (i) cout << i << "->"; //if(i) -> если x не равно 0
- while (j > 0)
- {
- if (!g.next_el[j]) {
- for (int x : g.terminal[j]) {
- //cout << x << ", ";
- temp.insert(x);
- }
- } //(!next) -> если next = 0
- else {
- for (int x : g.terminal[j]) {
- //cout << x << ", ";
- temp.insert(x);
- }
- }
- j = g.next_el[j];
- }
- //cout << endl;
- data.push_back(temp);
- }
- return data;
- }
- void PrintSolution(const vector<set<int>>& data) {
- int counter = 0;
- for (const auto& line : data) {
- if (counter) cout << counter << "->"; //if(counter) -> если counter не равно 0
- for (int x : line) {
- cout << x << " ";
- }
- if (counter) cout << endl;
- counter++;
- }
- }
- int main()
- {
- Graph g;
- Read("second.txt", g);
- PrintSolution(Solve(g));
- }
Advertisement
Add Comment
Please, Sign In to add comment