QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#530794#5706. Villageisirazeev0 0ms3528kbC++203.2kb2024-08-24 17:15:492024-08-24 17:15:49

Judging History

你现在查看的是最新测评结果

  • [2024-08-24 17:15:49]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:3528kb
  • [2024-08-24 17:15:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = (int) 1e5 + 10;

vector<int> g[N];
int sz[N], used[N];

inline void sizes(int v, int p = -1) {
    sz[v] = 1;
    for (auto u: g[v]) {
        if (u != p && !used[u])
            sizes(u, v), sz[v] += sz[u];
    }
}

inline int centroid(int v, int p, int Size) {
    for (auto u: g[v]) {
        if (u != p && !used[u] && sz[u] > Size / 2)
            return centroid(u, v, Size);
    }
    return v;
}

int n, res = 0, pr[N], from[N];

vector<int> vertex;

inline void dfs(int v, int p) {
    vertex.emplace_back(v);
    for (auto u: g[v]) {
        if (u != p && !used[u])
            dfs(u, v);
    }
}

inline void solve(int v) {
    sizes(v);
    int C = centroid(v, -1, n);
    sizes(C);
    used[C] = true;
    for (auto u: g[C]) {
        if (!used[u])
            res += sz[u];
    }
    vector<vector<int>> node;
    for (auto u: g[C]) {
        if (!used[u]) {
            vertex.clear();
            dfs(u, -1);
            node.emplace_back(vertex);
        }
    }
    if (!node.empty()) {
        sort(node.begin(), node.end(), [&](vector<int> &a, vector<int> &b) {
            return a.size() < b.size();
        });
        if (sz[C] % 2 == 0) node[0].emplace_back(C);
        sort(node.begin(), node.end(), [&](vector<int> &a, vector<int> &b) {
            return a.size() < b.size();
        });
        vector<vector<int>> copy = node;
        reverse(copy.begin(), copy.end());
        for (int i = 0; i < (int) node.size() - 1; i++) {
            int j = i + 1;
            while (!node[i].empty()) {
                pr[from[node[i].back()]] = node[j].back();
                pr[from[node[j].back()]] = node[i].back();
                int t1 = from[node[i].back()], t2 = from[node[j].back()];
                from[node[i].back()] = t2, from[node[j].back()] = t1;
                node[i].pop_back(), node[j].pop_back();
            }
        }
        int i = (int) node.size() - 1;
        while (!node.back().empty()) {
            while (!copy.empty() && copy.back().empty()) copy.pop_back();
            pr[from[node[i].back()]] = copy.back().back();
            pr[from[copy.back().back()]] = node[i].back();
            int t1 = from[node[i].back()], t2 = from[copy.back().back()];
            from[node[i].back()] = t2, from[copy.back().back()] = t1;
            node[i].pop_back(), copy.back().pop_back();
        }
    }
    int son = -1;
    for (auto u: g[C]) {
        if (!used[u])
            solve(u), son = u;
    }
    if (pr[C] == C) {
        int new_son = pr[from[son]];
        pr[from[son]] = C, pr[from[C]] = new_son;
        int t1 = from[son], t2 = from[C];
        from[C] = t1, from[new_son] = t2;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u - 1].emplace_back(v - 1);
        g[v - 1].emplace_back(u - 1);
    }
    for (int i = 0; i < n; i++) pr[i] = from[i] = i;
    solve(0);
    cout << res * 2 << "\n";
    for (int i = 0; i < n; i++)
        cout << pr[i] + 1 << " ";
    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3528kb

input:

4
1 2
2 3
3 4

output:

8
3 4 1 2 

result:

wrong output format Unexpected end of file - int32 expected

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%