QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#543#188567#7222. The Great Huntricky_linucup-team004Failed.2024-02-21 15:39:572024-02-21 15:39:58

詳細信息

Extra Test:

Accepted
time: 472ms
memory: 22640kb

input:

9984
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 18
20 19
21 20
22 21
23 22
24 23
25 24
26 25
27 26
28 27
29 28
30 29
31 30
32 31
33 32
34 33
35 34
36 35
37 36
38 37
39 38
40 39
41 40
42 41
43 42
44 43
45 44
46 45
47 46
48 47
49 48
50 49
51 50
52 51
53 52
...

output:

No

result:

ok 

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#188567#7222. The Great Huntucup-team004AC ✓653ms26292kbC++204.7kb2023-09-26 01:09:032023-09-26 01:09:03

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int V = 2E5;

struct Edge {
    int to;
    int cap;
};

std::vector<Edge> e;
std::vector<int> adj[V];

void addEdge(int u, int v, int cap) {
    adj[u].push_back(e.size());
    e.push_back({v, cap});
    adj[v].push_back(e.size());
    e.push_back({u, 0});
}

constexpr int inf = 1E9;

int h[V];
int tot = 0;
int cur[V];
bool bfs(int s, int t) {
    std::fill(h, h + tot, -1);
    std::queue<int> q;
    q.push(s);
    h[s] = 0;
    
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        
        for (auto i : adj[x]) {
            auto [y, cap] = e[i];
            if (cap > 0 && h[y] == -1) {
                h[y] = h[x] + 1;
                q.push(y);
            }
        }
    }
    
    return h[t] != -1;
}

int dfs(int x, int t, int f) {
    if (x == t) {
        return f;
    }
    int res = 0;
    for (auto &i = cur[x]; i < adj[x].size(); i++) {
        int j = adj[x][i];
        auto [y, cap] = e[j];
        if (cap > 0 && h[y] == h[x] + 1) {
            int a = dfs(y, t, std::min(cap, f));
            f -= a;
            res += a;
            e[j].cap -= a;
            e[j ^ 1].cap += a;
            if (f == 0) {
                break;
            }
        }
    }
    return res;
}

int flow(int s, int t) {
    int ans = 0;
    while (bfs(s, t)) {
        std::fill(cur, cur + tot, 0);
        ans += dfs(s, t, inf);
    }
    return ans;
}

constexpr int N = 1E4;

int f[N];
int g[N];

int find(int x) {
    if (f[f[x]] == f[x]) {
        return f[x];
    }
    int y = find(f[x]);
    int u = tot++;
    if (g[x] != -1) {
        addEdge(u, g[x], inf);
    }
    if (g[f[x]] != -1) {
        addEdge(u, g[f[x]], inf);
    }
    f[x] = y;
    g[x] = u;
    return y;
}

void merge(int x, int y) {
    x = find(x);
    y = find(y);
    f[y] = x;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::iota(f, f + n, 0);
    std::fill(g, g + n, -1);
    
    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    tot = 2 * n + 2;
    int s = 2 * n, t = 2 * n + 1;
    
    for (int i = 0; i < n; i++) {
        addEdge(s, n + i, 1);
        addEdge(i, t, 1);
    }
    
    const int logn = std::__lg(n);
    std::vector p(logn + 1, std::vector<int>(n, -1));
    std::vector id(logn + 1, std::vector<int>(n, -1));
    std::vector<int> dep(n);
    
    auto dfs = [&](auto self, int x) -> void {
        id[0][x] = x;
        for (int i = 0; (2 << i) <= dep[x]; i++) {
            p[i + 1][x] = p[i][p[i][x]];
            id[i + 1][x] = tot++;
            addEdge(id[i + 1][x], id[i][x], inf);
            addEdge(id[i + 1][x], id[i][p[i][x]], inf);
        }
        for (auto y : adj[x]) {
            if (y == p[0][x]) {
                continue;
            }
            dep[y] = dep[x] + 1;
            p[0][y] = x;
            self(self, y);
        }
    };
    dfs(dfs, 0);
    
    for (int i = 0; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        if (dep[u] < dep[v]) {
            std::swap(u, v);
        }
        for (int j = logn; j >= 0; j--) {
            if (dep[u] - (1 << j) >= dep[v]) {
                addEdge(n + i, id[j][u], 1);
                u = p[j][u];
            }
        }
        for (int j = logn; j >= 0; j--) {
            if (p[j][u] != p[j][v]) {
                addEdge(n + i, id[j][u], 1);
                addEdge(n + i, id[j][v], 1);
                u = p[j][u];
                v = p[j][v];
            }
        }
        if (u != v) {
            addEdge(n + i, u, 1);
            addEdge(n + i, v, 1);
            u = p[0][u], v = p[0][v];
        }
        addEdge(n + i, u, 1);
    }
    
    int ans = flow(s, t);
    if (ans < n) {
        std::cout << "No\n";
        return 0;
    }
    std::cout << "Yes\n";
    
    std::vector<int> a(n);
    std::fill(cur, cur + tot, 0);
    auto match = [&](auto self, int x) {
        if (x < n) {
            return x;
        }
        for (auto &i = cur[x]; i < ::adj[x].size(); i++) {
            int j = ::adj[x][i];
            auto [y, cap] = e[j];
            if (j % 2 == 0 && e[j ^ 1].cap > 0) {
                e[j ^ 1].cap--;
                return self(self, y);
            }
        }
        assert(false);
    };
    for (int i = 0; i < n; i++) {
        a[i] = match(match, n + i);
        std::cout << a[i] + 1 << " \n"[i == n - 1];
    }
    
    return 0;
}