답안 #79588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
79588 2018-10-15T03:51:40 Z EntityIT Network (BOI15_net) C++14
0 / 100
13 ms 12264 KB
#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, child[N], nLeaf, cur, tmp;
vector<int> gr[N];
set<ii> ver;

void prep (int u, int p) {
    for (int v : gr[u]) if (v != p) {
        prep(v, u);
        child[u] += child[v];
    }
    child[u] += (gr[u].size() == 1);
}

int findCentroid (int u, int p) {
    bool ok = 1;
    for (int v : gr[u]) if (v != p && child[v] > (nLeaf + 1) / 2) ok = 0;
    if (nLeaf - child[u] > (nLeaf + 1) / 2) ok = 0;
    if (ok) return u;
    for (int v : gr[u]) if (v != p) {
        if (child[v] > (nLeaf + 1) / 2) return findCentroid(v, u);
    }
    return u;
}

void dfs (int u, int p) {
    for (int v : gr[u]) if (v != p) dfs(v, u);
    if (gr[u].size() == 1) {
        if (ver.size() && (*ver.begin() ).fi != cur) {
            cout << u << ' ' << (*ver.begin() ).se << '\n';
            ver.erase(ver.begin() );
        }
        else ver.insert(ii(cur, 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';
    prep(1, 1);
    int cen = findCentroid(1, 1);
    vector<ii> tmp1;
    for (int u : gr[cen]) tmp1.pb(ii(child[u], u) );
    sort(tmp1.begin(), tmp1.end() ); reverse(tmp1.begin(), tmp1.end() );
    for (auto _ : tmp1) cur ++, dfs(_.se, cen);


    if (ver.size() ) {
        for (int u = 1; u <= n; ++u) if (gr[u].size() == 1 && u != (*ver.begin() ).se) tmp = u;
        cout << tmp << ' ' << (*ver.begin() ).se << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Incorrect 13 ms 12264 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Incorrect 13 ms 12264 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Incorrect 13 ms 12264 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -