Submission #401859

#TimeUsernameProblemLanguageResultExecution timeMemory
401859gevacrtNetwork (BOI15_net)C++17
0 / 100
1 ms460 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define INF INT_MAX #define MOD 1000000007 #define all(x) x.begin(), x.end() namespace CHECK_GRAPH { vi tin, low, vis; int timer = 0; bool pos = true; int dfs(int u, int p, vvi &adj){ vis[u] = 1; tin[u] = low[u] = timer++; bool so = false; for(auto v : adj[u]){ if(v == p and !so) {so = true; continue;} if(vis[v]) low[u] = min(low[u], tin[v]); else low[u] = min(low[u], dfs(v, u, adj)); } if(tin[u] == low[u] and u != p) pos = false; return low[u]; } void init(vvi &adj, int ROOT){ int N = adj.size(); tin.resize(N), low.resize(N); vis.resize(N); dfs(ROOT, ROOT, adj); } }; vvi adj; vii bb; vi solve(int u, int p){ if(adj[u].size() == 1) return {u}; vi vec; for(auto v : adj[u]){ if(v == p) continue; vi nvec = solve(v, u); while(vec.size() and nvec.size() and vec.size()+nvec.size() > 2-(u==p)){ bb.push_back({vec.back(), nvec.back()}); vec.pop_back(); nvec.pop_back(); } vec.insert(vec.end(), all(nvec)); } if(u == p and vec.size()){ bb.push_back({vec.back(), u}); } return vec; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; adj.resize(N); for(int x = 0; x < N-1; x++){ int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } int ROOT = 0; for(int x = 0; x < N; x++) if(adj[x].size() > 1) ROOT = x; solve(ROOT, ROOT); cout << bb.size() << endl; for(auto [u, v] : bb){ cout << u+1 << " " << v+1 << "\n"; adj[u].push_back(v); adj[v].push_back(u); } CHECK_GRAPH::init(adj, ROOT); assert(CHECK_GRAPH::pos); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...