제출 #79683

#제출 시각아이디문제언어결과실행 시간메모리
79683EntityITNetwork (BOI15_net)C++14
100 / 100
737 ms42312 KiB
#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
typedef pair<int, int> ii;

const int N = 5e5 + 5;
int n, nLeaf;
vector<int> gr[N], vec;

void dfs (int u, int p) {
    for (int v : gr[u]) if (v != p) dfs(v, u);
    if (gr[u].size() == 1) vec.pb(u);
}

int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int _ = 1; _ < n; ++_) {
        int u, v; cin >> u >> v;
        gr[u].pb(v); gr[v].pb(u);
    }

    for (int u = 1; u <= n; ++u) nLeaf += (gr[u].size() == 1);
    cout << (nLeaf + 1) / 2 << '\n';

    for (int u = 1; u <= n; ++u) if (gr[u].size() > 1) {
        dfs(u, u);
        break ;
    }

    for (int i = 0; i < vec.size() - (nLeaf + 1) / 2; ++i) cout << vec[i] << ' ' << vec[i + (nLeaf + 1) / 2] << '\n';
    if (nLeaf % 2) cout << vec[0] << ' ' << vec[nLeaf / 2];

    return 0;
}

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

net.cpp: In function 'int main()':
net.cpp:35:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vec.size() - (nLeaf + 1) / 2; ++i) cout << vec[i] << ' ' << vec[i + (nLeaf + 1) / 2] << '\n';
                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...