답안 #656281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656281 2022-11-06T17:15:27 Z ilia_rr Network (BOI15_net) C++17
0 / 100
31 ms 55220 KB
// بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ
// Written by Ilia Rahbar

// #pragma GCC optimize ("O3,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target ("abm,bmi,bmi2,tbm,avx2")

#include <bits/stdc++.h>
using namespace std;

#define int int64_t
#define ai(x) array <int, x>
#define F first 
#define S second

const int MOD = 1e9 + 7, N = 1e6 + 100;

int c, n, u[N], v[N], d[N], stc, l, r, co[N], mv = 1e9, fv[N];

vector <int> g[N], st[N], ans[2];

void dfs(int inx)
{
    if (g[inx].size() == 1)
        st[stc].push_back(inx);

    for (auto i : g[inx])
    {
        if (!d[i])
        {
            d[i] = 1;

            dfs(i);
        }
    }
}

void dfs0(int inx, int par)
{
    d[inx] = d[par] + 1;

    co[inx] = (g[inx].size() == 1);

    for (auto i : g[inx])
    {
        if (d[i])
            continue;
        
        dfs0(i, inx);

        co[inx] += co[i];
    }
}       

int32_t main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n;

    for (int i = 1; i < n; i++)
        cin >> u[i] >> v[i], g[u[i]].push_back(v[i]), g[v[i]].push_back(u[i]);
    
    dfs0(1, 0);

    for (int i = 1; i <= n; i++)
    {
        if (g[i].size() > g[c].size())
            c = i;
    }

    for (int i = 1; i < n; i++)
    {
        if (d[u[i]] > d[v[i]])
            swap(u[i], v[i]);

        if (abs(co[v[i]] - (co[1] - co[v[i]])) <= mv)
        {
            mv = abs(co[v[i]] - (co[1] - co[v[i]]));
            
            if (co[v[i]] < (co[1] - co[v[i]]) && g[u[i]].size() > 1)
                c = u[i];

            else if (g[v[i]].size() > 1)
                c = v[i];
        }
    }

    memset(d, 0, sizeof d);

    d[c] = 1;

    for (auto i : g[c])
    {
        dfs(i);

        stc++;
    }

    sort(st, st+stc, [](vector <int> &x, vector <int> &y){return (x.size() < y.size());});

    for (int i = 0; i < stc; i++)
        fv[i] = st[i].size();
    
    l = 0;
    r = stc-1;

    while (st[r].size())
    {
        for (int i = l; st[r].size() && i < r; i++)
        {
            ans[0].push_back(st[r].back());
            ans[1].push_back(st[i].back());

            st[r].pop_back();
            st[i].pop_back();
        }
        
        while (l < r && st[r].empty())
            r--;

        while (l < r && st[l].empty())
            l++;

        if (l == r && st[r].size())
        {
            if (int(st[r].size()) == fv[r])
            {
                ans[0].push_back(st[r].back());

                st[r].pop_back();
                    
                ans[1].push_back(c);
            }

            while (st[r].size())
            {
                ans[0].push_back(st[r].back());
                
                st[r].pop_back();
                
                if (st[r].size())
                {
                    ans[1].push_back(st[r].back());
                    st[r].pop_back();
                }

                else
                    ans[1].push_back(c);
            }
        }
    } 

    cout << ans[0].size() << "\n";

    for (int i = 0; i < int(ans[0].size()); i++)
    {
        if (ans[0][i] > ans[1][i])
            swap(ans[0][i], ans[1][i]);

        cout << ans[0][i] << " " << ans[1][i] << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 55124 KB Output is correct
2 Correct 30 ms 55128 KB Output is correct
3 Correct 26 ms 55128 KB Output is correct
4 Correct 28 ms 55144 KB Output is correct
5 Correct 26 ms 55108 KB Output is correct
6 Correct 26 ms 55124 KB Output is correct
7 Correct 26 ms 55124 KB Output is correct
8 Correct 27 ms 55156 KB Output is correct
9 Correct 30 ms 55060 KB Output is correct
10 Correct 31 ms 55132 KB Output is correct
11 Correct 29 ms 55156 KB Output is correct
12 Correct 28 ms 55036 KB Output is correct
13 Correct 26 ms 55152 KB Output is correct
14 Correct 26 ms 55064 KB Output is correct
15 Correct 24 ms 55056 KB Output is correct
16 Correct 29 ms 55124 KB Output is correct
17 Correct 31 ms 55076 KB Output is correct
18 Correct 29 ms 55136 KB Output is correct
19 Correct 28 ms 55220 KB Output is correct
20 Incorrect 27 ms 55112 KB Invalid number of links.
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 55124 KB Output is correct
2 Correct 30 ms 55128 KB Output is correct
3 Correct 26 ms 55128 KB Output is correct
4 Correct 28 ms 55144 KB Output is correct
5 Correct 26 ms 55108 KB Output is correct
6 Correct 26 ms 55124 KB Output is correct
7 Correct 26 ms 55124 KB Output is correct
8 Correct 27 ms 55156 KB Output is correct
9 Correct 30 ms 55060 KB Output is correct
10 Correct 31 ms 55132 KB Output is correct
11 Correct 29 ms 55156 KB Output is correct
12 Correct 28 ms 55036 KB Output is correct
13 Correct 26 ms 55152 KB Output is correct
14 Correct 26 ms 55064 KB Output is correct
15 Correct 24 ms 55056 KB Output is correct
16 Correct 29 ms 55124 KB Output is correct
17 Correct 31 ms 55076 KB Output is correct
18 Correct 29 ms 55136 KB Output is correct
19 Correct 28 ms 55220 KB Output is correct
20 Incorrect 27 ms 55112 KB Invalid number of links.
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 55124 KB Output is correct
2 Correct 30 ms 55128 KB Output is correct
3 Correct 26 ms 55128 KB Output is correct
4 Correct 28 ms 55144 KB Output is correct
5 Correct 26 ms 55108 KB Output is correct
6 Correct 26 ms 55124 KB Output is correct
7 Correct 26 ms 55124 KB Output is correct
8 Correct 27 ms 55156 KB Output is correct
9 Correct 30 ms 55060 KB Output is correct
10 Correct 31 ms 55132 KB Output is correct
11 Correct 29 ms 55156 KB Output is correct
12 Correct 28 ms 55036 KB Output is correct
13 Correct 26 ms 55152 KB Output is correct
14 Correct 26 ms 55064 KB Output is correct
15 Correct 24 ms 55056 KB Output is correct
16 Correct 29 ms 55124 KB Output is correct
17 Correct 31 ms 55076 KB Output is correct
18 Correct 29 ms 55136 KB Output is correct
19 Correct 28 ms 55220 KB Output is correct
20 Incorrect 27 ms 55112 KB Invalid number of links.
21 Halted 0 ms 0 KB -