Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <stdint.h>
- using namespace std;
- typedef uint32_t u32;
- struct s_visited
- {
- u32 a;
- u32 b;
- u32 index;
- };
- struct s_connection
- {
- u32 a;
- u32 b;
- };
- vector<s_connection> connections = {};
- bool has_path(u32 a, u32 b, vector<s_visited>* visited)
- {
- for(u32 i = 0; i < connections.size(); i++) {
- s_connection c = connections[i];
- if((c.a == a && c.b == b) || (c.b == a && c.a == b)) { return true; }
- bool skip = false;
- for(u32 j = 0; j < visited->size(); j++) {
- s_visited v = visited->at(j);
- if(v.index == i && (v.a == a && v.b == b) || (v.b == a && v.a == b)) {
- skip = true;
- break;
- }
- }
- if(skip) { continue; }
- visited->push_back({a, b, i});
- if(c.a == a) {
- if(has_path(c.b, b, visited)) { return true; }
- }
- if(c.b == a) {
- if(has_path(c.a, b, visited)) { return true; }
- }
- }
- return false;
- }
- int main()
- {
- u32 cities;
- u32 roads;
- cin >> cities >> roads;
- for(u32 i = 0; i < roads; i++) {
- u32 a;
- u32 b;
- cin >> a >> b;
- connections.push_back({a, b});
- }
- u32 curr = 1;
- vector<s_connection> added = {};
- for(u32 i = 0; i < cities; i++) {
- u32 ii = i + 1;
- if(curr == ii) { continue; }
- vector<s_visited> visited = {};
- if(!has_path(curr, ii, &visited)) {
- connections.push_back({curr, ii});
- added.push_back({curr, ii});
- }
- curr += 1;
- }
- printf("%zu\n", added.size());
- for(u32 i = 0; i < added.size(); i++) {
- printf("%i %i\n", added[i].a, added[i].b);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement