제출 #656190

#제출 시각아이디문제언어결과실행 시간메모리
656190DorostNetwork (BOI15_net)C++17
100 / 100
425 ms60976 KiB
/* * In the name of GOD */ #include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair <int, int> pii; #define F first #define S second #define mk make_pair const int N = 501234; int st[N], ts[N], cnt = 0; vector <int> g[N], k; void dfs(int v, int p) { st[v] = cnt; ts[cnt] = v; cnt++; if (g[v].size() == 1) k.push_back(st[v]); for (auto u : g[v]) { if (u != p) { dfs(u, v); } } } int32_t main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(); int n; cin >> n; int root; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); if (g[u].size() >= 2) root = u; if (g[v].size() >= 2) root = v; } dfs(root, -1); cout << ((int)k.size() + 1) / 2 << '\n'; sort(k.begin(), k.end()); for (int i = 0; i < ((int)k.size() + 1) / 2; i++) cout << ts[k[i]] << ' ' << ts[k[i + ((int)k.size()) / 2]] << '\n'; }

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

net.cpp: In function 'int32_t main()':
net.cpp:46:5: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |  dfs(root, -1);
      |  ~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...