제출 #30481

#제출 시각아이디문제언어결과실행 시간메모리
30481sampritiNetwork (BOI15_net)C++14
63 / 100
439 ms32100 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>

using namespace std;

vector<int> G[500001];
vector<int> leaves;

void dfs(int i, int p) {
  if(G[i].size() == 1) leaves.push_back(i);

  for(int j = 0; j < G[i].size(); j++) {
    int u = G[i][j];
    if(u == p) continue;
    dfs(u, i);
  }
}

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

  int N; cin >> N;

  for(int i = 0; i < N - 1; i++) {
    int a, b; cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs(1, 0);

  int ans = (leaves.size() + 1)/2;

  cout << ans << endl;

  for(int i = 0; i < ans; i++) {
    assert((i + leaves.size()/2) < leaves.size());
    cout << leaves[i] << " " << leaves[i + leaves.size()/2] << endl;
  }
}

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

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