# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1128891 | ntdaccode | Network (BOI15_net) | C++20 | 16 ms | 23876 KiB |
#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M = 1e6 + 10;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
int n;
vector<int> ke[M], node;
//vector<ii> ans;
//namespace trace
//{
// bool d[M];
// void check()
// {
// for(ii v : ans) {
// ke[v.first].pb(v.second);
// ke[v.second].pb(v.first);
// }
// for(int i = 1;i <= n; i++) {
// d[i] = false;
// }
// queue<int> q;
// d[1] = true;
// q.push(1);
// while(!q.empty()) {
// int u = q.front(); q.pop();
// for(int v : ke[u]) {
// if(!d[v]) {
// d[v] = true;
// q.push(v);
// }
// }
// }
// bool ok = 1;
// for(int i = 1;i <= n; i++) if(!d[i]) ok = 0;
// assert(ok);
// }
//
//}
void dfs(int u,int p = 0)
{
int chk = 0;
for(int v : ke[u]) {
if(v == p) continue;
chk++;
dfs(v,u);
}
if(chk == 1 && u == 1) node.pb(u);
if(chk == 0) node.pb(u);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r")) {
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n;
for(int i = 1;i <= n - 1; i++) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
dfs(1,0);
cout << (node.size() + 1)/2 << "\n";
for(int i = 0;i < node.size() - 1; i+= 2) {
cout << node[i] << " " << node[i + 1] << "\n";
//ans.pb({node[i], node[i + 1]});
}
if(node.size() % 2 == 1) {
cout << node[0] << " " << node.back() << "\n";
//ans.pb({node[0], node.back()});
}
//trace :: check();
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |