Submission #1064020

#TimeUsernameProblemLanguageResultExecution timeMemory
1064020manhlinh1501Network (BOI15_net)C++17
0 / 100
6 ms12124 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int MAXN = 5e5 + 5; using pii = pair<int, int>; int N; vector<int> adj[MAXN]; vector<int> leaves; signed main() { #define TASK "code" if (fopen(TASK ".inp", "r")) { freopen(TASK ".inp", "r", stdin); freopen(TASK ".out", "w", stdout); } 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; adj[u].emplace_back(v); adj[v].emplace_back(u); } for(int i = 1; i <= N; i++) { if(adj[i].size() == 1) leaves.emplace_back(i); } if(leaves.size() == 1) return cout << 0, 0; if(leaves.size() == 2 or leaves.size() == 3) { cout << leaves.size() - 1 << "\n"; for(int i = 0; i < leaves.size() - 1; i++) cout << leaves[i] << " " << leaves[i + 1] << "\n"; return 0; } vector<pii> ans; for(int i = 0; i < leaves.size() / 2; i++) ans.emplace_back(leaves[i], leaves[leaves.size() - i - 1]); if(leaves.size() % 2 == 1) ans.emplace_back(leaves[leaves.size() / 2], leaves.front()); cout << ans.size() << "\n"; for(auto [x, y] : ans) cout << x << " " << y << "\n"; return (0 ^ 0); }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         for(int i = 0; i < leaves.size() - 1; i++)
      |                        ~~^~~~~~~~~~~~~~~~~~~
net.cpp:43:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for(int i = 0; i < leaves.size() / 2; i++)
      |                    ~~^~~~~~~~~~~~~~~~~~~
net.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...