QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390334 | #8055. Balance | JerryTcl | WA | 191ms | 3772kb | C++20 | 3.9kb | 2024-04-15 12:13:39 | 2024-04-15 12:13:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
void Solve() {
static int tc = 0; ++tc;
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> src(n);
if(tc == 2969) cerr << n << " " << m << endl;
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);
if(tc == 2969) cerr << u + 1 << " " << v + 1 << endl;
}
vector<int> a(n), L(n + 1), R(n + 1); int h = n;
for(int i = 0; i < n; ++i) cin >> a[i];
if(tc == 2969) for(int i = 0; i < n; ++i) cerr << 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) {
if(!~u) u = 0; if(!~m) m = 0; if(!~v) v = 0;
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();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3764kb
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: 0
Accepted
time: 143ms
memory: 3772kb
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 Yes 1 1 Yes 1 1 Yes 1 1 Yes 1 1 1 1 No Yes 1 1 1 1 1 Yes 1 3 1 1 1 Yes 1 1 1 Yes 1 2 Yes 1 1 1 1 1 Yes 1 2 No Yes 1 1 Yes 1 1 1 Yes 1 1 Yes 1 1 1 1 Yes 1 1 Yes 2 2 2 2 2 Yes 1 1 1 1 1 Yes 1 1 Yes 1 2 1 1 No Yes 1 1 No Yes 1 1 No No No Yes 2 1 1 1 1 Ye...
result:
ok ok (100000 test cases)
Test #3:
score: -100
Wrong Answer
time: 191ms
memory: 3772kb
input:
83335 9 17 1 4 8 9 5 2 1 3 2 7 1 7 2 8 6 7 2 4 1 8 5 8 7 1 8 6 4 6 4 7 6 9 7 9 7 3 4 4 7 4 2 4 8 6 9 3 1 1 2 3 5 1 2 3 4 4 5 6 3 6 1 6 2 4 3 2 2 1 3 9 8 9 3 5 7 5 9 2 6 1 8 4 1 4 2 4 3 4 2 5 3 4 5 4 5 4 6 7 2 1 1 5 6 1 3 1 6 5 2 4 5 3 2 1 2 1 2 1 4 6 2 1 4 2 1 4 1 2 1 4 3 1 2 2 4 2 6 10 2 3 3 5 2 1 ...
output:
No No Yes 4 3 4 4 5 2 5 4 5 No Yes 2 2 4 2 No Yes 3 2 3 3 Yes 2 2 1 No Yes 1 1 1 1 1 No Yes 1 1 1 Yes 1 1 3 3 3 Yes 1 1 Yes 1 1 Yes 1 1 1 1 Yes 3 1 3 No Yes 1 1 1 1 1 1 1 1 Yes 1 1 1 1 1 1 1 No Yes 1 1 No Yes 1 1 1 1 1 Yes 2 1 1 2 1 No Yes 1 2 3 1 3 3 3 1 No No No No No No No No No No No No Yes 7 6 ...
result:
wrong answer ans finds an answer, but out doesn't (test case 2969)