QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#712#456488#8055. Balanceucup-team3215ucup-team3215Failed.2024-07-01 01:33:362024-07-01 01:33:37

詳細信息

Extra Test:

Invalid Input

input:

1

output:


result:

FAIL Unexpected end of file - token expected (test case 1, stdin, line 2)

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456488#8055. Balanceucup-team3215AC ✓473ms38788kbC++236.7kb2024-06-28 05:14:112024-06-28 05:14:12

answer

#include <bits/stdc++.h>

using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
constexpr int INF = 1e9 + 7;

#define sz(c) ssize(c)

vi num, st;
vector<vector<pii>> ed;
vector<pii> E;
int Time;

int dfs(int at, int par, auto &&f) {
    int me = num[at] = ++Time, top = me;
    for (auto [y, e]: ed[at])
        if (e != par) {
            if (num[y]) {
                top = min(top, num[y]);
                if (num[y] < me)st.push_back(e);
            } else {
                int si = sz(st);
                int up = dfs(y, e, f);
                top = min(top, up);
                if (up == me) {
                    st.push_back(e);
                    f(vi(st.begin() + si, st.end()));
                    st.resize(si);
                } else if (up < me)st.push_back(e);
            }
        }
    return top;
}

void bicomps(auto &&f) {
    num.assign(sz(ed), 0);
    for (int i = 0; i < sz(ed); ++i)if (!num[i])dfs(i, -1, f);

}

vector<int> pred;

int find(int x) {
    return x == pred[x] ? x : pred[x] = find(pred[x]);
}

void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)return;
    pred[x] = y;
}

void solve() {
    Time = 0;
    num.clear();
    pred.clear();
    st.clear();
    ed.clear();
    E.clear();
    int n, m;
    cin >> n >> m;
    ed.resize(n);
    pred.resize(n);
    iota(pred.begin(), pred.end(), 0);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        ed[u].push_back({v, i});
        ed[v].push_back({u, i});
        E.emplace_back(u, v);
    }
    vector<int> a(n);
    for (auto &i: a)cin >> i;
    sort(a.begin(), a.end());
    if(a.front()==a.back()){
        cout << "Yes\n";
        for(auto x : a)cout << x << " ";
        cout << "\n";
        return;
    }
    bicomps([&](const vi &e) {
        for (auto id: e) {
            unite(E[id].first, E[id].second);
        }
    });
    vector<int> who(n, -1);
    vector<int> w;
    vector<vector<int>> G;
    vector<vector<int>> all;
    for (int v = 0; v < n; ++v) {
        int p = find(v);
        if (!~who[p]) {
            who[p] = G.size();
            G.emplace_back();
            all.emplace_back();
            w.emplace_back(0);
        }
        w[who[p]]++;
        all[who[p]].push_back(v);
    }
    for (int v = 0; v < n; ++v) {
        int p = find(v);
        for (auto [to, _]: ed[v]) {
            if (find(to) == p)continue;
            G[who[p]].push_back(who[find(to)]);
        }
    }
    vector<pair<int, int>> range(n + 1, {INF, -INF});
    for (int i = 0; i < n; ++i) {
        range[a[i]].first = min(range[a[i]].first, i);
        range[a[i]].second = max(range[a[i]].second, i);
    }
    vector<pair<int, int>> dp(n, {-1, -1});
    vector<int> dz(n, 0);
    vector<int> res(n, -1);
    bool solved{0};
    auto solver = [&](int s, int t) {
        if (solved)return;
        solved = 1;
        vector<char> mark(n, 0);
        vector<int> o;
        auto dfs = [&](auto &&dfs, int v, int p) -> bool {
            mark[v] = 1;
            o.push_back(v);
            if (v == t) {
                return true;
            }
            for (auto to: G[v]) {
                if (to == p)continue;
                if (dfs(dfs, to, v)) {
                    return true;
                }
            }
            mark[v] = 0;
            o.pop_back();
            return false;
        };
        dfs(dfs, s, -1);
        int ptr{0};
        auto dfs2 = [&](auto &&dfs2, int v, int p) -> void {
            for (auto x: all[v]) {
                res[x] = a[ptr++];
            }
            for (auto to: G[v]) {
                if (to == p || mark[to])continue;
                dfs2(dfs2, to, v);
            }
        };
        for (auto v: o) {
            dfs2(dfs2, v, -1);
        }
    };
    auto dfs = [&](auto &&dfs, int v, int p) -> void {
        dz[v] = w[v];
        for (auto to: G[v]) {
            if (to == p || solved)continue;
            dfs(dfs, to, v);
            dz[v] += dz[to];
        }
        for (auto to: G[v]) {
            if (to == p || solved)continue;
            auto [x, rev_x] = dp[to];
            int it = dz[v] - dz[to];
            if (~x) {
                auto [l, r] = range[a[dz[to]]];
                if (r - dz[to] + 1 >= it) {
                    dp[v].first = x;
                }
            }
            if (~rev_x) {
                int pos = n - 1 - dz[to];
                auto [l, r] = range[a[pos]];
                if (pos - l + 1 >= it) {
                    dp[v].second = rev_x;
                }
            }
        }
        for (auto to: G[v]) {
            if (to == p || solved)continue;
            auto [x, rev_x] = dp[to];
            if (~x) {
                auto [l, r] = range[a[dz[to]]];
                if (r == n - 1) {
                    solver(x, v);
                }
            }
            if (~rev_x) {
                int pos = n - 1 - dz[to];
                auto [l, r] = range[a[pos]];
                if (l == 0) {
                    solver(v, rev_x);
                }
            }
        }
        pair<int, int> mx{-1, -1};
        for (auto to: G[v]) {
            if (to == p || solved)continue;
            auto [x, rev_x] = dp[to];
            if (~x) {
                auto [l, r] = range[a[dz[to]]];
                int val = n - r - 1;
                if (mx.first >= val) {
                    solver(x, mx.second);
                }
            }
            if (~rev_x) {
                mx = max(mx, pair{dz[to], rev_x});
            }
        }
        mx = {-1, -1};
        for (auto to: views::reverse(G[v])) {
            if (to == p || solved)continue;
            auto [x, rev_x] = dp[to];
            if (~x) {
                auto [l, r] = range[a[dz[to]]];
                int val = n - r - 1;
                if (mx.first >= val) {
                    solver(x, mx.second);
                }
            }
            if (~rev_x) {
                mx = max(mx, pair{dz[to], rev_x});
            }
        }
        {
            auto [l, r] = range[a.front()];
            if (r - l + 1 >= dz[v]) {
                dp[v].first = v;
            }
        }
        {
            auto [l, r] = range[a.back()];
            if (r - l + 1 >= dz[v]) {
                dp[v].second = v;
            }
        }
    };
    dfs(dfs, 0, -1);
    if (!solved) {
        cout << "No\n";
        return;
    }
    cout << "Yes\n";
    for (auto x: res)cout << x << " ";
    cout << "\n";
}


int main() {
    cin.tie(0)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)solve();
}