103202024-03-30 17:52:02szilPletykálkodáscpp17Accepted 100/100405ms7252 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 7001;

vector<int> g[MAXN];
vector<pair<int, int>> ans1, ans2;
bool vis[MAXN];

void dfs(int u) {
	vis[u] = true;
	for (int v : g[u]) {
		if (!vis[v]) {
			dfs(v);
			ans1.emplace_back(u, v);
		}
	}
}

void print_ans() {
	int k = ans1.size() + ans2.size();
	cout << k << "\n";
	for (auto [u, v] : ans1) {
		cout << u << " " << v << "\n";
	}
	for (auto [u, v] : ans2) {
		cout << u << " " << v << "\n";
	}
}

void case1() {
	dfs(1);
	ans2 = ans1;
	ans2.pop_back();
	reverse(ans2.begin(), ans2.end());
	print_ans();
	exit(0);
}

void case2(vector<int> cyc) {
	for (int i : cyc) vis[i] = true;
	for (int i : cyc) {
		dfs(i);
	}
	ans2 = ans1;
	ans1.emplace_back(cyc[0], cyc[1]);
	ans1.emplace_back(cyc[2], cyc[3]);
	ans1.emplace_back(cyc[0], cyc[3]);
	ans1.emplace_back(cyc[1], cyc[2]);
	reverse(ans2.begin(), ans2.end());
	print_ans();
	exit(0);
}

vector<int> mp[MAXN];

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n, m; cin >> n >> m;
	if (n == 1) {
		cout << "0\n";
		return 0;
	}
	for (int i = 0; i < m; i++) {
		int u, v; cin >> u >> v;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	for (int i = 1; i <= n; i++) {
		for (int u : g[i]) {
			for (int v : g[u]) {
				mp[v].emplace_back(u);
			}
		}
		for (int u = 1; u <= n; u++) {
			if (u == i) continue;
			if (mp[u].size() >= 2) {
				case2({i, mp[u][0], u, mp[u][1]});
			}
		}
		for (int u = 1; u <= n; u++) {
			mp[u].clear();
		}
	}
	case1();
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2540 KiB
2Accepted3ms2820 KiB
3Accepted4ms3276 KiB
subtask29/9
4Accepted3ms3312 KiB
5Accepted3ms3196 KiB
6Accepted3ms3196 KiB
subtask313/13
7Accepted133ms4744 KiB
8Accepted131ms4908 KiB
9Accepted130ms5096 KiB
subtask416/16
10Accepted7ms4804 KiB
11Accepted7ms4808 KiB
12Accepted7ms4720 KiB
subtask525/25
13Accepted3ms3756 KiB
14Accepted3ms3764 KiB
15Accepted3ms4012 KiB
16Accepted3ms3972 KiB
17Accepted3ms4232 KiB
18Accepted3ms4444 KiB
19Accepted3ms4396 KiB
subtask613/13
20Accepted4ms4656 KiB
21Accepted4ms4920 KiB
22Accepted4ms4952 KiB
23Accepted19ms5484 KiB
24Accepted16ms5120 KiB
25Accepted16ms5040 KiB
26Accepted6ms5240 KiB
subtask724/24
27Accepted12ms6828 KiB
28Accepted12ms6740 KiB
29Accepted12ms6840 KiB
30Accepted294ms7252 KiB
31Accepted349ms7064 KiB
32Accepted405ms6708 KiB
33Accepted402ms6552 KiB
34Accepted375ms6468 KiB
35Accepted17ms6692 KiB
36Accepted39ms6652 KiB
37Accepted9ms6496 KiB