제출 #672873

#제출 시각아이디문제언어결과실행 시간메모리
672873iosif_andrei_Network (BOI15_net)C++14
0 / 100
7 ms12052 KiB
#include <bits/stdc++.h> using namespace std; int n, h[500001]; vector <int> g[500001]; void dfs(int nod, int lvl) { h[nod] = lvl; for (auto& i : g[nod]) if (!h[i]) dfs(i, lvl + 1); } struct nod { int key; bool operator < (nod aux) { return h[key] < h[aux.key]; } }; vector <nod> v; int main() { cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } int root; for (int i = 1; i <= n; i++) if (g[i].size() == 1) { root = i; break; } dfs(root, 1); for (int i = 1; i <= n; i++) if (g[i].size() == 1) v.push_back({ i }); cout << (v.size() + 1) / 2 << '\n'; sort(v.begin(), v.end()); int i = 0, j = v.size() - 1; while (i < j) { cout << v[i].key << ' ' << v[j].key << '\n'; i++; j--; } if (i == j) if (g[v[i].key][0] == 1) cout << v[i].key << ' ' << 2; else cout << v[i].key << ' ' << 1; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

net.cpp: In function 'int main()':
net.cpp:67:8: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   67 |     if (i == j)
      |        ^
net.cpp:48:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |     dfs(root, 1);
      |     ~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...