103172024-03-30 17:49:07szilPletykálkodáscpp17Time limit exceeded 76/1001.6s7956 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++) {
		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
1Accepted3ms1996 KiB
2Accepted3ms2308 KiB
3Accepted10ms3088 KiB
subtask29/9
4Accepted3ms3036 KiB
5Accepted3ms3252 KiB
6Accepted3ms3176 KiB
subtask313/13
7Accepted27ms4456 KiB
8Accepted13ms4304 KiB
9Accepted12ms4184 KiB
subtask416/16
10Accepted8ms4500 KiB
11Accepted8ms4516 KiB
12Accepted7ms4736 KiB
subtask525/25
13Accepted3ms3748 KiB
14Accepted3ms3936 KiB
15Accepted3ms4060 KiB
16Accepted4ms4080 KiB
17Accepted3ms4040 KiB
18Accepted4ms4036 KiB
19Accepted3ms4032 KiB
subtask613/13
20Accepted4ms4408 KiB
21Accepted4ms4456 KiB
22Accepted4ms4712 KiB
23Accepted108ms5424 KiB
24Accepted115ms5208 KiB
25Accepted193ms5032 KiB
26Accepted68ms4824 KiB
subtask70/24
27Accepted13ms6376 KiB
28Accepted12ms6604 KiB
29Accepted12ms6636 KiB
30Time limit exceeded1.544s5044 KiB
31Time limit exceeded1.574s4988 KiB
32Time limit exceeded1.567s5084 KiB
33Time limit exceeded1.6s5092 KiB
34Time limit exceeded1.562s5332 KiB
35Accepted67ms7956 KiB
36Accepted388ms7836 KiB
37Accepted14ms7224 KiB