723E - One-Way Reform - CodeForces Solution


constructive algorithms dfs and similar flows graphs greedy *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
//#define int long long

const int N = 2e2 + 22;
set<int> adj[N];
vector<pair<int, int>> j;
int n, m, deg[N], t, ans;

void ip() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int v, u;
		cin >> v >> u;
		deg[v]++;
		deg[u]++;
		adj[v].insert(u);
		adj[u].insert(v);
	}
}
void ch() {
	for (int i = 1; i <= n; i++) {
		if (deg[i] % 2 == 0)
			ans++;
		else {
			adj[i].insert(N - 2);
			adj[N - 2].insert(i);
		}
	}
}

void euler(int v, int p) {
	adj[v].erase(p);
	while(!adj[v].empty()) {
		int u = *adj[v].begin();
		adj[v].erase(u);
		j.push_back({v, u});
		euler(u, v);
	}
}

void op() { 
	cout << ans << endl;
	for (auto i : j) {
		int v = i.first, u = i.second;
		if (v != N - 2 && u != N - 2)
			cout << v << " " << u << endl;
	}
}

void uwu() {
	ans = 0;
	j.clear();
	for (int i = 0; i <= n; i++) {
		deg[i] = 0;
		adj[i].clear();
	}
}

void run() {
	ip();
	ch();
	for (int i = 0; i <= n; i++)
		if (!adj[i].empty())
			euler(i, 0);
	op();
	uwu();
}

signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		run();
	}
}


Comments

Submit
0 Comments
More Questions

1215C - Swap Letters
1251C - Minimize The Integer
1494B - Berland Crossword
295A - Greg and Array
1433E - Two Round Dances
1612D - X-Magic Pair
41B - Martian Dollar
906C - Party
774F - Pens And Days Of Week
598B - Queries on a String
1303B - National Project
1303D - Fill The Bag
1198B - Welfare State
1739B - Array Recovery
1739C - Card Game
1739A - Immobile Knight
1739D - Reset K Edges
817B - Makes And The Product
1452C - Two Brackets
400B - Inna and New Matrix of Candies
870B - Maximum of Maximums of Minimums
1600E - Array Game
1149A - Prefix Sum Primes
277A - Learning Languages
769A - Year of University Entrance
1738A - Glory Addicts
1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts