Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <array>
- #include <set>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 1e5 + 100;
- const int N = 1e5 + 10;
- const int MOD = 1e9 + 7;
- int dp[MAXN][2];
- vector<vector<int>> g;
- void dfs(int v, int p) {
- dp[v][0] = dp[v][1] = 0;
- for (auto to: g[v]) {
- if (to == p) continue;
- dfs(to, v);
- dp[v][0] = max({dp[v][0], dp[to][0], dp[to][1]});
- dp[v][1] = max(dp[v][1], dp[to][0]);
- }
- dp[v][1] += 1;
- }
- void get_ans(int u, int p = -1) {
- for (auto v: g[u]) {
- if (v == p) {
- continue;
- }
- if (dp[v][1] > dp[v][0]) {
- cout << u + 1 << ' ' << v + 1 << '\n';
- }
- get_ans(v, u);
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- g.resize(n);
- for (int i = 0; i < m; ++i) {
- int u, v;
- cin >> u >> v;
- --u, --v;
- g[u].emplace_back(v);
- g[v].emplace_back(u);
- }
- dfs(0, -1);
- cout << max(dp[0][0], dp[0][1]) << '\n';
- get_ans(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment