Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdint.h>
- using namespace std;
- typedef uint32_t u32;
- struct s_connection {
- u32 a;
- u32 b;
- };
- static bool walked[100000];
- static u32 connection_count = 0;
- static u32 added_count = 0;
- static s_connection connections[200000];
- static s_connection added[200000];
- static u32 cities, roads;
- int main() {
- cin >> cities >> roads;
- for(u32 i = 0; i < roads; i++) {
- u32 a, b;
- cin >> a >> b;
- connections[connection_count++] = {a, b};
- }
- walked[1] = true;
- for(u32 city_i = 1; city_i < cities; city_i++) {
- u32 curr = city_i;
- u32 target = city_i + 1;
- if(walked[target]) {
- continue;
- }
- bool reset_loop = true;
- while(reset_loop) {
- reset_loop = false;
- for(int connection_i = 0; connection_i < (int)connection_count; connection_i++) {
- s_connection c = connections[connection_i];
- if(walked[c.a] && !walked[c.b]) {
- walked[c.b] = true;
- reset_loop = true;
- }
- else if(walked[c.b] && !walked[c.a]) {
- walked[c.a] = true;
- reset_loop = true;
- }
- if(walked[c.a] && walked[c.b]) {
- connections[connection_i--] = connections[--connection_count];
- }
- if(walked[target]) { break; }
- }
- }
- if(!walked[target]) {
- connections[connection_count++] = {curr, target};
- added[added_count++] = {curr, target};
- walked[target] = true;
- }
- }
- cout << added_count << endl;
- for(u32 i = 0; i < added_count; i++) {
- cout << added[i].a << " " << added[i].b << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement