Submission #748426

#TimeUsernameProblemLanguageResultExecution timeMemory
748426mariowongNetwork (BOI15_net)C++14
100 / 100
514 ms50136 KiB
#include <bits/stdc++.h> using namespace std; int deg[500005]; vector <int> edge[500005]; bool vis[500005]; vector <int> v; void dfs(int x){ vis[x]=true; if (deg[x] == 1) v.push_back(x); for (int i=0;i<edge[x].size();i++){ if (!vis[edge[x][i]]) dfs(edge[x][i]); } } int main(){ ios::sync_with_stdio(false); int n; cin >> n; for (int i=1;i<n;i++){ int u,v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); deg[u]++; deg[v]++; } int cnt=0; for (int i=1;i<=n;i++){ if (deg[i] == 1) cnt++; } cout << (cnt+1)/2 << "\n"; for (int i=1;i<=n;i++){ if (deg[i] > 1){ dfs(i); break; } } if (cnt%2 == 1) v.push_back(v[(int)v.size()-1]); for (int i=0;i+(cnt+1)/2<v.size();i++){ cout << v[i] << " " << v[i+(cnt+1)/2] << "\n"; } return 0; }

Compilation message (stderr)

net.cpp: In function 'void dfs(int)':
net.cpp:11:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |  for (int i=0;i<edge[x].size();i++){
      |               ~^~~~~~~~~~~~~~~
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+(cnt+1)/2<v.size();i++){
      |               ~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...