제출 #831256

#제출 시각아이디문제언어결과실행 시간메모리
831256CookieNetwork (BOI15_net)C++14
100 / 100
426 ms49708 KiB
#include<bits/stdc++.h> #include<fstream> //#pragma GCC optimize("Ofast,O3,unroll-loops") //#pragma GCC target("avx2") using namespace std; ifstream fin("palpath.in"); ofstream fout("palpath.out"); #define sz(a) (int)a.size() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> const ll mod = 1e9 + 7; const int mxn = 5e5 + 5, mxr = 25e3, sq = 500, mxv = 1e6 + 5, mxvv = 130, pr = 31, inf = 1e9; int n; int in[mxn + 1]; vt<int>leaf; vt<int>adj[mxn + 1]; void dfs(int s, int pre){ bool ok = 0; for(auto i: adj[s]){ if(i != pre){ dfs(i, s); ok = 1; } } if(!ok)leaf.pb(s); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; in[u]++; in[v]++; adj[u].pb(v); adj[v].pb(u); } for(int i = 1; i <= n; i++){ if(in[i] > 1){ dfs(i, -1); break; } } cout << ((sz(leaf) + 1) / 2) << "\n"; for(int i = 0; i <= sz(leaf) / 2 - 1; i++){ cout << leaf[i] << " " << leaf[i + sz(leaf) / 2] << "\n"; } if(sz(leaf) & 1){ cout << leaf[0] << " " << leaf.back(); } return(0); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...