QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#456477 | #8055. Balance | ucup-team3215 | WA | 143ms | 3576kb | C++23 | 6.6kb | 2024-06-28 04:54:05 | 2024-06-28 04:54:05 |
Judging History
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);
}
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<int> a(n);
for (auto &i: a)cin >> i;
sort(a.begin(), a.end());
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();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
5 5 4 1 2 2 3 3 4 4 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 2 2 3 5 6 1 2 1 2 2 3 3 4 4 5 3 5 1 2 1 2 1 2 2 1 2 1 2 1 2
output:
Yes 5 4 3 2 1 No Yes 2 3 1 2 2 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 143ms
memory: 3532kb
input:
100000 4 3 4 2 1 3 2 1 2 1 3 2 5 7 2 5 5 3 2 3 5 1 4 3 2 4 4 3 1 3 1 1 2 3 2 2 1 2 3 1 1 1 4 7 3 4 1 4 2 3 4 3 4 2 3 1 4 3 2 3 3 2 4 6 2 4 3 1 1 2 1 2 2 3 4 2 1 1 3 3 4 7 3 2 4 2 1 2 3 4 3 2 2 4 3 4 2 1 1 1 5 5 3 2 1 5 4 5 4 3 2 1 1 2 2 3 2 3 5 2 3 1 3 3 1 3 1 2 1 3 2 3 2 3 2 1 2 1 1 2 1 1 2 3 2 1 1...
output:
Yes 2 2 3 1 No Yes 1 1 1 No No Yes 2 1 1 1 No No No No No No No Yes 1 1 1 1 1 Yes 1 3 1 1 1 Yes 1 1 1 Yes 2 1 Yes 1 1 1 1 1 Yes 2 1 No No Yes 1 1 1 Yes 1 1 No No Yes 2 2 2 2 2 No Yes 1 1 Yes 1 1 1 2 No No No Yes 1 1 No No No Yes 1 1 1 1 2 Yes 1 2 1 1 No No No No Yes 3 1 3 3 2 Yes 1...
result:
wrong answer ans finds an answer, but out doesn't (test case 9)