Submission #606591

#TimeUsernameProblemLanguageResultExecution timeMemory
606591nguyentunglamNetwork (BOI15_net)C++14
100 / 100
498 ms68212 KiB
#include<bits/stdc++.h> #define forin(i, a, b) for(int i = a; i <= b; i++) #define forde(i, a, b) for(int i = a; i >= b; i--) #define forv(a, b) for(auto & a : b) #define fi first #define se second using namespace std; const int N = 5e5 + 10; int n; vector<int> adj[N]; vector<pair<int, int>> ans; vector<int> dfs(int u, int par) { vector<int> f; if (adj[u].size() == 1) { f.push_back(u); return f; } forv(v, adj[u]) if (v != par) { vector<int> g = dfs(v, u); if (f.size() < g.size()) swap(f, g); while (f.size() + g.size() > 2) { ans.push_back({f.back(), g.back()}); f.pop_back(); g.pop_back(); } forv(t, g) f.push_back(t); } return f; } int main() { cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen ("bluehouse.inp", "r")) { freopen ("bluehouse.inp", "r", stdin); freopen ("bluehouse.out", "w", stdout); } cin >> n; forin(i, 1, n - 1) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } forin(i, 1, n) if (adj[i].size() > 1) { vector<int> t = dfs(i, 0); if (t.size() >= 2) { int u = t.back(); t.pop_back(); int v = t.back(); t.pop_back(); ans.push_back({u, v}); } if (!t.empty()) ans.push_back({i, t[0]}); break; } cout << ans.size() << "\n"; forv(v, ans) cout << v.fi << " " << v.se << "\n"; }

Compilation message (stderr)

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