이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define s second
#define f first
#define pb push_back
#define left (h*2),l,(l + r)/2
#define right (h*2+1),(l+r)/2 + 1,r
#define pii pair <int,int>
#define int ll
using namespace std;
const int N = 5e5 + 3,inf = 1e9,mod = 1e9 + 7;
int deg[N],tin[N],timer;
vector <int> v[N];
void dfs(int x,int par){
tin[x] = ++timer;
for (int to: v[x]){
if (to!=par) dfs(to,x);
}
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i < n; i++){
int a,b;
cin>>a>>b;
deg[a]++;
deg[b]++;
v[a].pb(b);
v[b].pb(a);
}
dfs(1,1);
int ans=0;
vector <pii> vec;
for(int i=1;i<=n;i++){
if (deg[i] == 1) ++ans,vec.pb({tin[i],i});
}
sort(vec.begin(),vec.end());
cout<<(ans + 1)/2<<endl;
int mid = vec.size()/2;
if (ans&1) mid++;
for (int i=0;i<vec.size()/2;i++){
cout<<vec[i].s<<" "<<vec[mid++].s<<endl;
}
if (ans&1){
cout<<vec[vec.size()/2].s<<" "<<vec[0].s<<endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:51:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i=0;i<vec.size()/2;i++){
| ~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |