summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_2/exercises/matrice-adj.cc
blob: 7830f9766a7e848c2a999a5d87a1ea8e60a0c7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>

using namespace std;

template<class H>
class matrix_graph {
public:
    ~matrix_graph() {
        delete[] _m;
        delete[] _k;
    }
    matrix_graph(int len) : _len{len}, _nodes{0}, _edges{0} {
        _m = new int*[len];
        _k = new H*[len];
        for(int i = 0; i < len; ++i) {
            _m[i] = new int[len];
            _k[i] = nullptr;
            for(int j = 0; j < len; ++j)
                _m[i][j] = 0;
        }
    }

    matrix_graph<H>* add_node(H k) {
        if(_len == _nodes) return this; // no more space for new nodes
        if(_index(k) > -1) return this; // nodes already exists

        _k[_nodes++] = new H(k);

        return this;
    }

    matrix_graph<H>* add_edge(H x, H y) {
        int i = _index(x);
        int j = _index(y);
        if(i < 0 || j < 0) return this;
        if(!_m[i][j]) {
            _m[i][j] = 1;
            _edges++;
        }
        return this;
    }
    vector<vector<H>> print() {
        vector<vector<H>> v;
        for(int i = 0; i < _len; ++i) {
            v.push_back(vector<H>{*_k[i]});
            vector<H> temp;
            for(int j = 0; j < _len; ++j) {
                if(_m[i][j]) {
                    temp.push_back(*_k[j]);
                }
            }
            sort(begin(temp), end(temp));
            for(auto const& j : temp)
                v[i].push_back(j);
        }

        sort(begin(v), end(v));
        return v;
    }
private:
    int _len, _nodes, _edges;
    int** _m;
    H** _k;
    int _index(H x) {
        for(int i = 0; i < _nodes; ++i)
            if(*_k[i] == x) return i;
        return -1;
    }
};

template<class H>
string result(vector<vector<H>> v) {
    string s{""};
    ostringstream ss;
    for(auto const& i : v) {
        s += "(";
        for(auto const& j : i) {
            ss << j;
            s+= ss.str();
            s+= ' ';
            ss.str(""); ss.clear();
        }
        s.pop_back();
        s += ") ";
    }
    return s;
}

int main() {
    ifstream in("input.txt");
    ofstream out("output.txt");
    for(int ts = 0; ts < 100; ++ts) {
        int n, m;
        in >> n >> m;
        string t;
        in >> t;
        if(t == "int") {
            matrix_graph<int>* g = new matrix_graph<int>(n);
            int x,y;
            for(int i = 0; i < n; ++i) {
               in >> x;
               g->add_node(x);
            }
            char ex;
            for(int i = 0; i < m; ++i) {
                in >> ex >> x >> y >> ex;
                g->add_edge(x, y);
            }
            auto v = g->print();
            out << result(v) << endl;
            delete g;
        } else if(t == "double") {
            matrix_graph<double>* g = new matrix_graph<double>(n);
            double x,y;
            for(int i = 0; i < n; ++i) {
               in >> x;
               g->add_node(x);
            }
            char ex;
            for(int i = 0; i < m; ++i) {
                in >> ex >> x >> y >> ex;
                g->add_edge(x, y);
            }
            auto v = g->print();
            out << result(v) << endl;
            delete g;
        } else {
            matrix_graph<char>* g = new matrix_graph<char>(n);
            char x,y;
            for(int i = 0; i < n; ++i) {
               in >> x;
               g->add_node(x);
            }
            char ex;
            for(int i = 0; i < m; ++i) {
                in >> ex >> x >> y >> ex;
                g->add_edge(x, y);
            }

            auto v = g->print();
            out << result(v) << endl;
            delete g;
        }
    }
    in.close();
    out.close();
    return 0;
}