이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*CP template version 3.0 - Laconic*/
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fs first
#define sc second
//use struct instead of pair<int, ii>
#define int long long
#define show(v) {for (auto i : v) cout << i << " "; cout << endl;}
#define showlr(v, l, r) {for (int i = l; i <= r; i++) cout << v[i] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define SUnique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define forlr(i, l, r) for (int i = l; i <= r; i++)
#define forrl(i, r, l) for (int i = r; i >= l; i--)
#pragma GCC Optimize("O2")
#define endl "\n"
#define precise(x) cout << fixed << setprecision(x);
using namespace std;
const int N = 2e5 + 100;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int LOG = 25;
const long long LINF = 1e15 + 100;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
ostream& operator << (ostream &os, ii a) {
os << a.fs << ' ' << a.sc;
return os;
}
int binPow(int a, int b, int m = LLONG_MAX) {
int prod = 1;
a %= m;
while (b) {
if (b & 1) prod = prod * a % m;
a = a * a % m;
b /= 2;
}
return prod;
}
void setIO(string s) {
if (s.empty()) return;
freopen((s + ".inp").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
//Try Brute Force (maybe there is a pattern)
//Try DP (Weird constraints (sum of all elements <= 10000), small constraints)
//Try Graph (a pair of number, condition between elements)
//Try Geometry (Same as graph, for coordinate)
//Try switching between problem
//Put down what you have on paper
//IMPORTANT: WRITE DOWN THE FORMULA
int n, m, q, k;
int arr[N];
vector<int> adj[N];
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
vector<int> leaf;
void dfs(int u, int pre) {
if (adj[u].size() == 1) {
leaf.push_back(u);
if (u != pre) return;
}
for (int v : adj[u]) {
if (v == pre) continue;
dfs(v, u);
}
}
int deg[N];
ii edges[N];
void solve() {
cin >> n;
forlr(i, 1, n - 1) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
vector<ii> res;
int sz = leaf.size();
if (sz == 2) {
cout << 1 << endl;
cout << leaf[0] << " " << leaf[1] << endl;
return;
}
if (sz <= 1) {
cout << 0 << endl;
return;
}
forlr(i, 0, (sz - 1) / 2) res.push_back({leaf[i], leaf[i + sz / 2]});
cout << res.size() << endl;
for (ii i : res) cout << i << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T = 1;
bool multiTest = 0;
if (multiTest) cin >> T;
while (T--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp:20: warning: ignoring '#pragma GCC Optimize' [-Wunknown-pragmas]
20 | #pragma GCC Optimize("O2")
|
net.cpp: In function 'void setIO(std::string)':
net.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen((s + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |