Submission #1091604

#TimeUsernameProblemLanguageResultExecution timeMemory
1091604DeathIsAweNetwork (BOI15_net)C++17
0 / 100
0 ms344 KiB
#include <bits/stdc++.h> using namespace std; bool visited[500000]; int main() { int n, d1,d2; cin >> n; vector<vector<int>> graph(n); for (int i=0;i<n-1;i++) { cin >> d1 >> d2; d1--; d2--; graph[d1].push_back(d2); graph[d2].push_back(d1); visited[i] = false; } visited[n - 1] = false; vector<int> leaves; stack<int> dfs; int root, node; for (int i=0;i<n;i++) { if (graph[i].size() > 1) { dfs.push(i); root = i; break; } } while (dfs.size() > 0) { node = dfs.top(); dfs.pop(); visited[node] = true; for (int i: graph[node]) { if (!visited[i]) { dfs.push(i); } } if (graph[node].size() == 1) { leaves.push_back(node); } } cout << (n + 1) / 2 << '\n'; for (int i=0;i<leaves.size()/2;i++) { cout << leaves[i] + 1 << ' ' << leaves[i + (leaves.size() / 2)] + 1 << '\n'; } if (leaves.size() % 2 == 1) { cout << leaves[leaves.size() - 1] + 1 << leaves[leaves.size() - 1 - (leaves.size() / 2)] + 1 ; } }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:43:19: 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:20:9: warning: variable 'root' set but not used [-Wunused-but-set-variable]
   20 |     int root, node;
      |         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...