Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <unordered_map>
- using namespace std;
- long long int n, m, h = 2433137;
- vector <vector <int>> g;
- vector <int> f;
- unordered_map <long long int, pair<int, int>> d;
- pair <int, bool> rek( int u, int v) {
- if (g[u].size() > 1 || u == 1) {
- bool trig = false;
- int res = 0;
- for (auto i : g[u]) {
- if (i != v) {
- pair < int, bool> t = rek(i, u);
- f[i] = 1;
- res += t.first;
- if (!t.second) {
- if (!trig) {
- trig = true;
- res++;
- f[i] = 2;
- }
- }
- }
- }
- return { res , trig };
- }
- else {
- return {0, false};
- }
- }
- void rekk(int u, int v) {
- if (g[u].size() > 1 || u == 1) {
- bool trig = true;
- for (auto i : g[u]) {
- if (i != v) {
- rekk(i, u);
- if (f[i] == 2) {
- cout << d[u * h + i].first << " " << d[u * h + i].second << "\n";
- }
- }
- }
- }
- }
- int main()
- {
- iostream::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- cin >> n >> m;
- g.resize(n + 1, f);
- f.resize(n + 3, 0);
- for (int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- g[a].push_back(b);
- g[b].push_back(a);
- d[a * h + b] = { a, b };
- d[b * h + a] = { a, b };
- }
- cout << rek(1, 0).first << "\n";
- rekk(1, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement