Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- using namespace std;
- class Graph
- {
- public:
- const int block = -1;
- int vcount, acount;
- vector<int> IJ, H, L;
- Graph(int _vcount, vector<int>& _i, vector<int>& _j) :vcount(_vcount), acount(_i.size() * 2) {
- IJ.resize(acount);
- for (int k(0); k < acount / 2; ++k) {
- IJ[k] = _i[k];
- int ind = acount - k - 1;
- IJ[ind] = _j[k];
- }
- makeList();
- }
- void makeList() {
- H.assign(vcount, block);
- L.resize(acount);
- for (int i(0); i < acount; ++i) {
- int ind(IJ[i]);
- L[i] = H[ind];
- H[ind] = i;
- }
- }
- void addArc(int i, int j) {
- IJ.insert(IJ.begin() + acount / 2, i);
- IJ.insert(IJ.begin() + acount / 2 + 1, j);
- acount += 2;
- makeList();
- }
- void print() {
- cout << "graph G {" << endl;
- for (int k(0); k < acount / 2; ++k) {
- cout << IJ[k] + 1 << " -- " << IJ[acount - k - 1] + 1<< ";" << endl;
- }
- cout << "}";
- }
- void showBeamsOfArcs() {
- cout << "Beams of arcs: " << endl;
- vector <int> ArcsList;
- for (int i(0); i < vcount; ++i) {
- ArcsList.clear();
- for (int k(H[i]); k != -1; ) {
- ArcsList.push_back(k >= acount / 2 ? acount - k - 1 : k);
- k = L[k];
- }
- int count(ArcsList.size());
- switch (count)
- {
- case 0:
- cout << "From vertex " << i + 1 << " come no edges";
- break;
- case 1:
- cout << "From vertex " << i + 1 << " comes 1 edge: ";
- break;
- default:
- cout << "From vertex " << i + 1 << " come " << count << " edges: ";
- }
- if (count > 0) {
- for (int k(0); k < count - 1; ++k) {
- cout << ArcsList[k] + 1 << ", ";
- }
- cout << ArcsList[count - 1] + 1;
- }
- cout << endl;
- }
- }
- };
- int main()
- {
- freopen("test.txt", "r", stdin);
- freopen("test.gv", "w", stdout);
- int n, m;
- cin >> n >> m;
- vector<int> _i, _j;
- int ii, jj;
- for (int i(0); i < m; ++i) {
- cin >> ii >> jj;
- _i.push_back(ii - 1);
- _j.push_back(jj - 1);
- }
- Graph G(n, _i, _j);
- G.print();
- return 0;
- }
Add Comment
Please, Sign In to add comment