Submission #1270579

#TimeUsernameProblemLanguageResultExecution timeMemory
1270579nathan4690Network (BOI15_net)C++20
100 / 100
328 ms41924 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name "TEST"
using namespace std;
const ll maxn = 1e6+5, inf=1e18;

int n, sp[maxn], timer = 1;
vector<int> G[maxn], all;

bool cmp(int u, int v){
    return sp[u] < sp[v];
}

void dfs(int u, int p){
    sp[u] = timer++;
    for(int c: G[u]){
        if(c != p){
            dfs(c, u);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n;
    f1(i,n-1){
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    if(n == 2){
        cout << "1\n1 2\n";
        return 0;
    }
    int root = 0;
    f1(i,n) {
        if(G[i].size() == 1) all.push_back(i);
        else root = i;
    }
    dfs(root, 0);
    sort(all.begin(), all.end(), cmp);
    cout << (all.size() + 1) / 2 << '\n';
    for(int i=0;i+i<all.size();i++){
        int j = i + all.size() / 2;
        cout << all[i] << ' ' << all[j] << '\n';
    }
    return 0;
}

Compilation message (stderr)

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