103182024-03-30 17:49:42szilPletykálkodáscpp17Time limit exceeded 76/1001.557s7488 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);
}

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++) {
		unordered_map<int, vector<int>> mp;
		for (int u : g[i]) {
			for (int v : g[u]) {
				mp[v].emplace_back(u);
			}
		}
		for (auto [u, vec] : mp) {
			if (u == i) continue;
			if (vec.size() >= 2) {
				case2({i, vec[0], u, vec[1]});
			}
		}
	}
	case1();
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2248 KiB
2Accepted3ms2568 KiB
3Accepted10ms3180 KiB
subtask29/9
4Accepted3ms3124 KiB
5Accepted3ms3228 KiB
6Accepted3ms3456 KiB
subtask313/13
7Accepted30ms4684 KiB
8Accepted14ms4540 KiB
9Accepted13ms4544 KiB
subtask416/16
10Accepted7ms4696 KiB
11Accepted8ms4696 KiB
12Accepted7ms4692 KiB
subtask525/25
13Accepted3ms3724 KiB
14Accepted3ms3876 KiB
15Accepted3ms3796 KiB
16Accepted3ms3924 KiB
17Accepted3ms4020 KiB
18Accepted4ms3924 KiB
19Accepted3ms4032 KiB
subtask613/13
20Accepted4ms4548 KiB
21Accepted4ms4472 KiB
22Accepted4ms4724 KiB
23Accepted97ms5184 KiB
24Accepted105ms4932 KiB
25Accepted177ms4896 KiB
26Accepted59ms4928 KiB
subtask70/24
27Accepted12ms6280 KiB
28Accepted12ms6448 KiB
29Accepted12ms6608 KiB
30Time limit exceeded1.555s4840 KiB
31Time limit exceeded1.547s4744 KiB
32Time limit exceeded1.549s4928 KiB
33Time limit exceeded1.557s5040 KiB
34Time limit exceeded1.554s5008 KiB
35Accepted57ms7380 KiB
36Accepted300ms7488 KiB
37Accepted13ms6696 KiB