QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#390315 | #8055. Balance | JerryTcl | TL | 0ms | 4068kb | C++20 | 3.6kb | 2024-04-15 11:40:54 | 2024-04-15 11:40:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
void Solve() {
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> src(n);
for(int i = 1, u, v; i <= m; ++i)
cin >> u >> v, --u, --v,
src[u].emplace_back(v, i),
src[v].emplace_back(u, i);
vector<int> a(n), L(n + 1), R(n + 1); int h = n;
for(int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end()), R[n - 1] = R[n] = n;
for(int i = n; --i > 0;) R[i - 1] = a[i] == a[i - 1] ? R[i] : i;
reverse(a.begin(), a.end()), L[n - 1] = L[n] = n;
for(int i = n; --i > 0;) L[i - 1] = a[i] == a[i - 1] ? L[i] : i;
int t; vector<vector<int>> G, bcc;
{ // e-bcc tarjan
vector<int> id(n), lw(n), bel(n);
int dft = 0; t = -1; vector<int> s;
const auto &dfs = [&](auto &&sf, int x, int f) -> void {
id[x] = lw[x] = ++dft, s.push_back(x);
for(const auto [v, e] : src[x]) if(e != f) {
if(id[v]) lw[x] = min(lw[x], id[v]);
else sf(sf, v, e), lw[x] = min(lw[x], lw[v]);
}
if(id[x] != lw[x]) return; ++t;
for(int q = -1; q != x; )
bel[q = s.back()] = t, s.pop_back();
};
dfs(dfs, 0, -1), G.resize(++t), bcc.resize(t);
for(int i = 0; i < n; ++i) bcc[bel[i]].push_back(i);
for(int i = 0, I, J; i < n; ++i)
for(const auto &[j, e] : src[i])
if((I = bel[i]) < (J = bel[j]))
G[I].push_back(J), G[J].push_back(I);
}
vector<int> w(t), Q(t); vector<pair<int, int>> f(t), g(t);
for(int i = 0; i < t; ++i) w[i] = bcc[i].size();
const auto &print = [&](int u, int m, int v) {
puts("Yes"); vector<int> ret(n), s(t); s[m] = 1;
for(int i = u; i != m; i = Q[i]) s[i] = 1;
for(int i = v; i != m; i = Q[i]) s[i] = 1;
const auto &dfs = [&](auto &&sf, int x, int q) -> void {
if(~q) for(const int y : bcc[x]) ret[y] = a[--h];
for(int y : G[x]) if(y != q && !s[y]) sf(sf, y, x);
if(!~q) for(const int y : bcc[x]) ret[y] = a[--h];
for(int y : G[x]) if(y != q && s[y]) sf(sf, y, x);
};
dfs(dfs, u, -1);
for(int i = 0; i < n; ++i)
printf("%d%c", ret[i], " \n"[i == n - 1]);
};
const auto &dfs = [&](auto &&sf, int x, int q) -> int {
for(int i : G[x]) if(i != q)
{ if(sf(sf, i, Q[i] = x)) return 1; w[x] += w[i]; }
if(R[0] >= w[x]) f[x] = {1, x};
else for(int i : G[x]) if(i != q)
if(f[i].fi && R[w[i]] >= w[x]) { f[x] = {1, f[i].se}; break; }
if(L[0] >= w[x]) g[x] = {1, x};
else for(int i : G[x]) if(i != q)
if(g[i].fi && L[w[i]] >= w[x]) { g[x] = {1, g[i].se}; break; }
if(f[x].fi && R[w[x]] == n) return print(f[x].se, q, q), 1;
if(g[x].fi && L[w[x]] == n) return print(q, q, g[x].se), 1;
array<int, 3> p0 = {0, 0, 0}, p1 = {0, 0, 0};
for(int i : G[x]) if(i != q) {
array<int, 3> t = {f[i].fi, w[i], i};
if(t > p0) p1 = p0, p0 = t; else if(t > p1) p0 = t;
}
for(int i : G[x]) if(i != q && g[i].fi) {
if(p0[0] && i != p0[2] && R[p0[1]] >= n - w[i])
return print(f[p0[2]].se, x, g[i].se), 1;
if(p1[0] && i != p1[2] && R[p1[1]] >= n - w[i])
return print(f[p1[2]].se, x, g[i].se), 1;
}
return 0;
};
if(!dfs(dfs, 0, -1)) puts("No");
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int T; cin >> T; while(T--) Solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4068kb
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 1 2 3 4 5 No Yes 2 3 1 2 2 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Time Limit Exceeded
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...