Submission #1278872

#TimeUsernameProblemLanguageResultExecution timeMemory
1278872hoa208Network (BOI15_net)C++20
100 / 100
310 ms50572 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
const   ll mod = 1e9 + 7;
const   ll INF = 1e18;
//--------------------------------------------------------------------

const ll N = 5e5 + 10;
vector<ll> g[N], vt;
void dfs(ll u, ll p) {
    if(g[u].size() == 1) {
        vt.push_back(u);
    }
    for(auto v : g[u]) {
        if(v == p) continue;
        dfs(v, u);
    }
}
void hbmt() {
    ll n;
    cin >> n;
    FOR(i, 1, n - 1) {
        ll u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    ll k = (vt.size() + 1) / 2;
    ll e = vt.size() / 2;
    cout << k << '\n';
    for(int i = 0; i < e; i++) {
        cout << vt[i] << ' ' << vt[i + e] << '\n';
    }
    if(vt.size() % 2 == 1) {
        cout << vt.back() << ' ' << vt[0] << '\n';
    }
}

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

    #define NAME "hbmt"
    if(fopen(NAME".inp", "r")) {
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }
    
    //int t;cin>>t;while(t--)
    hbmt();
    return 0;
}

Compilation message (stderr)

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