답안 #1019297

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019297 2024-07-10T17:08:50 Z Fatonim Village (BOI20_village) C++17
0 / 100
9 ms 23900 KB
// "We create our own demons"
#include <bits/stdc++.h>

using namespace std;

#ifdef ONPC
    #include "debug.h"
#else
    #define dbg(...)
#endif

#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;

template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;

template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;

const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;

const int B = 32;
const int N = 1000 + 3;
const int logn = 20;
const int maxn = 2e5 + 7;
/////////////////////////solve/////////////////////////

vi g[maxn];
int dp[maxn][2];
vpi dp_par[maxn][2];
vi dp_swap[maxn][2];
int ans[maxn];

void solve() {
    int n;
    cin >> n;

    for (int i = 0; i < n - 1; ++i) {
        int v, u;
        cin >> v >> u;
        --v; --u;

        g[v].push_back(u);
        g[u].push_back(v);
    }

    for (int i = 0; i < n; ++i) {
        ans[i] = i;
    }

    function<void(int, int)> dfs = [&](int v, int p) {
        set <pi> s;
        for (auto u : g[v]) {
            if (u != p) {
                dfs(u, v);
                dp[v][0] += dp[u][1];
                dp_par[v][0].push_back({u, 1});
                s.insert({dp[u][0] - dp[u][1] + 2, u});
            }
        }
        if (s.empty()) dp[v][1] = inf_s;
        else {
            int u = s.begin()->second;
            dp_swap[v][1].push_back(u);
            if (dp[u][0] <= dp[u][1]) {
                dp[v][1] += dp[u][0] + 2;
                dp_par[v][1].push_back({u, 0});
            }
            else {
                dp[v][1] += dp[u][1] + 2;
                dp_par[v][1].push_back({u, 1});
            }
            s.erase(s.begin());

            for (auto& [x, u] : s) {
                if (dp[u][0] + 2 < dp[u][1]) {
                    dp[v][1] += dp[u][0] + 2;
                    dp_par[v][1].push_back({u, 0});
                    dp_swap[v][1].push_back(u);
                }
                else {
                    dp[v][1] += dp[u][1];
                    dp_par[v][1].push_back({u, 1});
                }
            }
        }
    };

    dfs(0, -1);
    cout << dp[0][1] << " -1\n";

    function<void(int, int, int)> dfs2 = [&](int v, int t, int p) {
        for (auto& [u, t2] : dp_par[v][t]) {
            dfs2(u, t2, v);
        }
        for (auto& u : dp_swap[v][t]) {
            swap(ans[v], ans[u]);
        }
    };

    dfs2(0, 1, -1);
        
    for (int i = 0; i < n; ++i) {
        cout << ans[i] + 1 << " ";
    } cout << "\n";

    for (int i = 0; i < n; ++i) {
        cout << -1 << " ";
    }

}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef ONPC
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
#ifdef ONPC
        cerr << "===========" << i << "===========" << '\n';
#endif
        solve();
    }

#ifdef ONPC
    cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 23896 KB Integer parameter [name=vi] equals to -1, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 23900 KB Integer parameter [name=vi] equals to -1, violates the range [1, 256]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 23896 KB Integer parameter [name=vi] equals to -1, violates the range [1, 4]
2 Halted 0 ms 0 KB -