제출 #605798

#제출 시각아이디문제언어결과실행 시간메모리
605798buidangnguyen05Network (BOI15_net)C++14
0 / 100
20 ms24268 KiB
//source: https://oj.uz/problem/view/BOI15_net #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e5 + 10; vector<int> adj[N]; vector<pair<int, int>> ans; vector<int> dfs(int x, int par) { if (adj[x].size() == 1) return vector<int>(1, x); vector<int> cur; for (int i : adj[x]) if (i != par) { vector<int> v = dfs(i, x); if (cur.size() < v.size()) swap(v, cur); if (cur.size() + v.size() > 2) { while (v.size()) { int p = cur.back(); cur.pop_back(); int q = v.back(); v.pop_back(); ans.emplace_back(p, q); } } for (int j : v) cur.push_back(j); } return cur; } signed main() { cin.tie(0)->sync_with_stdio(0); if (fopen("task.inp", "r")) { freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); } int n; cin >> n; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } int root = 0, save = 0; for (int i = 1; i <= n; ++i) { if (adj[i].size() > 1) root = i; else save = i; } vector<int> rem = dfs(root, 0); assert(rem.size() < 2); if (rem.size()) ans.emplace_back(rem.back(), save); cout << ans.size() << "\n"; for (pair<int, int> i : ans) cout << i.first << " " << i.second << "\n"; }

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

net.cpp: In function 'int main()':
net.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |   freopen("task.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |   freopen("task.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...