제출 #623570

#제출 시각아이디문제언어결과실행 시간메모리
623570MinaRagy06Network (BOI15_net)C++17
0 / 100
9 ms12072 KiB
#include <bits/stdc++.h>
using namespace std;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl    '\n'
#define int     long long

vector<int> adj[500'005];
int dist[500'005];
void dfs(int i, int sum, int par)
{
    dist[i] = sum;
    for (auto nxt : adj[i]) if (nxt != par) dfs(nxt, sum+1, i);
}
signed main()
{
    lesgooo;
    int n, u, v, sz = 0;
    cin >> n;
    for (int i = 0; i < n-1; i++) cin >> u >> v, adj[--v].push_back(--u), adj[u].push_back(v);
    dfs(0, 0, -1);
    vector<pair<int, int>> a;
    for (int i = 0; i < n; i++) if (adj[i].size() == 1) sz++, a.push_back({dist[i], i});
    sort(a.begin(), a.end());
    int l = 0, r = a.size()-1;
    cout << (sz+1)/2 << endl;
    while (l < r) cout << a[l++].second+1 << " " << a[r--].second+1 << endl;
    if (l == r)
    {
        int mx = 0, x = 0;
        dfs(a[l].second, 0, -1);
        for (int i = 0; i < n; i++) if (i != l && dist[i] >= mx) mx = dist[i], x =i;
        cout << a[l].second+1 << " " << x+1 << endl;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...