제출 #58653

#제출 시각아이디문제언어결과실행 시간메모리
58653SpeedOfMagicNetwork (BOI15_net)C++17
100 / 100
1079 ms89248 KiB
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/ #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") template<typename T> using v = vector<T>; #define int long long typedef string str; typedef vector<int> vint; #define rep(a, l, r) for(int a = (l); a < (r); a++) #define pb push_back #define sz(a) ((int) a.size()) const long long inf = 4611686018427387903; //2^62 - 1 #if 0 //FileIO const string fileName = ""; ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" )); ofstream fout((fileName == "" ? "output.txt" : fileName + ".out")); #define get fin>> #define put fout<< #else #define get cin>> #define put cout<< #endif #define eol put endl void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;} void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);} void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg arg,Args... args){put (arg)<<" ";debug(args...);} int getInt(){int a; get a; return a;} //code goes here const int N = 500001; vint g[N]; v<pair<int, int>> ans; vint dfs(int cur, int p = -1) { if (sz(g[cur]) == 1) return vint({cur}); else { vint pos; for (int i : g[cur]) { if (i == p) continue; vint z = dfs(i, cur); if (sz(z) == 2 && sz(pos) == 1) for (int j : z) if (pos.empty()) pos.pb(j); else { ans.pb({pos.back(), j}); pos.pop_back(); } else for (int j : z) if (sz(pos) < 2) pos.pb(j); else { ans.pb({pos.back(), j}); pos.pop_back(); } } if (p == -1) { if (sz(pos) == 2) ans.pb({pos[0], pos[1]}); else if (g[pos[0]][0] != cur) ans.pb({pos[0], cur}); else rep(i, 1, N) if (pos[0] != i && g[pos[0]][0] != i) { ans.pb({pos[0], i}); break; } } return pos; } } void run() { int n; get n; n++; rep(i, 2, n) { int v, u; read(v, u); g[v].pb(u); g[u].pb(v); } rep(i, 1, n) if (sz(g[i]) != 1) { dfs(i); break; } put sz(ans); eol; for (auto i : ans) debug(i.first, i.second); } int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...