제출 #231016

#제출 시각아이디문제언어결과실행 시간메모리
231016AlexLuchianovNetwork (BOI15_net)C++14
100 / 100
529 ms57852 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>

using namespace std;

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 500000;
vector<int> g[1 + nmax];

vector<int> sol;

void dfs(int node, int parent){
  for(int h = 0; h < g[node].size(); h++){
    int to = g[node][h];
    if(to != parent)
      dfs(to, node);
  }
  if(g[node].size() == 1)
    sol.push_back(node);
}

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for(int i = 1;i < n; i++){
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int root = 1;
  for(int i = 1;i <= n; i++)
    if(1 < g[i].size())
      root = i;
  dfs(root, 0);
  cout << (sol.size() + 1) / 2 << '\n';

  int d = sol.size() / 2;

  if(sol.size() % 2 == 1)
    cout << sol[d] << " " << root << '\n';

  for(int i = 0; i < d; i++)
    cout << sol[i] << " " << sol[i + d + sol.size() % 2] << '\n';

  return 0;
}

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

net.cpp: In function 'void dfs(int, int)':
net.cpp:19:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++){
                  ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...