답안 #656033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656033 2022-11-06T08:55:50 Z mhn2 Network (BOI15_net) C++17
0 / 100
18 ms 35504 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

const int N = 5e5+5;
vector<int> adj[N], lo[N], ch[N];
vector<pii> edge;

void dfs1(int v, int p) {
    for (int u : adj[v])
        if (u != p) {
            ch[v].push_back(u);
            dfs1(u, v);
        }
}

void dfs2(int v) {
    if (ch[v].size() == 0) {
        lo[v].push_back(v);
        return;
    }

    vector<int> a, b;
    for (int u : ch[v]) {
        dfs2(u);
        if (lo[u].size() == 2) {
            a.push_back(lo[u][0]);
            a.push_back(lo[u][1]);
        }
        else
            b.push_back(lo[u][0]);
    }
    for (int x : b)
        a.push_back(x);
    
    lo[v].push_back(a[0]);
    for (int i = 1; i < a.size()-1; i += 2)
        edge.push_back({a[i], a[i+1]});
    if (!(a.size() & 1))
        lo[v].push_back(a.back());
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs1(1, 1);
    dfs2(1);

    for (int x : lo[1])
        edge.push_back({1, x});
    
    cout << edge.size() << endl;
    for (pii x : edge)
        cout << x.F << ' ' << x.S << endl;
}

Compilation message

net.cpp: In function 'void dfs2(int)':
net.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 1; i < a.size()-1; i += 2)
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 35504 KB Invalid number of links.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 35504 KB Invalid number of links.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 35504 KB Invalid number of links.
2 Halted 0 ms 0 KB -