Submission #1270637

#TimeUsernameProblemLanguageResultExecution timeMemory
1270637BuiDucManh123Network (BOI15_net)C++20
100 / 100
233 ms41036 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
#define ld long double
const int mod = 1e9+7;
using namespace std;
vector<int> g[500009];
int tin[500009];
int timer = 0;
void dfs(int u, int p){
    tin[u] = ++timer;
    for(int v : g[u]){
        if(v == p) continue;
        dfs(v, u);
    }
}
bool cmp(int x, int y){
    return tin[x] < tin[y];
}
signed main() {
    if (fopen(taskname".inp","r")) {
        freopen(taskname".inp","r",stdin);
        freopen(taskname".out","w",stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin >> n;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    int root = 0;
    vector<int> order;
    for(int i = 1; i <= n; i++){
        if(g[i].size() > 1){
            root = i;
        }
    }
    for(int i = 1; i <= n; i++){
        if(g[i].size() == 1){
            order.pb(i);
        }
    }
    //cout << root << "\n";
    dfs(root, root);
    sort(order.begin(), order.end(), cmp);
    cout << (order.size() + 1) / 2 << "\n";
    int sz = order.size();
    for(int i = 0; i < sz / 2; i++){
        cout << order[i] << " " << order[i + sz / 2] << "\n";
    }
    if(sz & 1){
        cout << order.back() << " " << order[0] << "\n";
    }
    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen(taskname".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen(taskname".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...