이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template< class T, class T2 > inline bool chkmin( T& a, const T2& b ){ return a > b ? a = b, 1 : 0; }
template< class T, class T2 > inline bool chkmax( T& a, const T2& b ){ return a < b ? a = b, 1 : 0; }
#ifndef LOCAL
#define cerr if( false )cerr
#endif
#define out(x) #x << " = " << x << " "
typedef long long ll;
const int MAX_N = 1e6 + 42;
const ll mod = 1e9 + 7;
std::vector< int > v[MAX_N];
int par[MAX_N];
std::vector< int > leaves;
void dfs( int x, int p = -1 ){
par[x] = p;
if( v[x].size() == 1 ) leaves.push_back( x );
for( auto j : v[x] ){
if( j == p ) continue;
dfs( j, x );
}
}
int main (){
#ifndef LOCAL
std::ios_base::sync_with_stdio( false ); std::cin.tie( NULL ); std::cout.tie( NULL );
#endif
int n;
std::cin >> n;
for( int i=0 ; i < n-1 ; i++ ){
int a, b;
std::cin >> a >> b;
a--; b--;
v[a].push_back( b );
v[b].push_back( a );
}
int st = 0;
//dfs( st );
for( int i=0 ; i < n ; i++ ){
if( v[i].size() == 1 ) leaves.push_back( i );
}
std::cout << ( leaves.size() +1 ) /2 << endl;
int add = leaves.size() >> 1;
for( int i=0 ; i + add < leaves.size() ; i++ ){
std::cout << leaves[i] +1 << " " << leaves[i + add] +1 << endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:55:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for( int i=0 ; i + add < leaves.size() ; i++ ){
| ~~~~~~~~^~~~~~~~~~~~~~~
net.cpp:47:6: warning: unused variable 'st' [-Wunused-variable]
47 | int st = 0;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |