QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#438815 | #8055. Balance | stasio6 | WA | 62ms | 91604kb | C++14 | 6.0kb | 2024-06-11 08:12:47 | 2024-06-11 08:12:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
template<class A, class B> void cmx(A& x, B y) {x=max<A>(x, y);}
template<class A, class B> void cmn(A& x, B y) {x=min<A>(x, y);}
typedef pair<int, int> pii;
typedef vector<int> vi;
int gl[1000002], low[1000002];
int ojc[1000002], si[1000002];
pii prefix[1000002], sufix[1000002];
int trees[1000002], oo[1000002];
vector<pii> edge[1000002];
vi tree[1000002];
vi gr[1000002];
int vals[1000002];
int res[1000002];
void aktu(int n) {
if (ojc[n] == ojc[ojc[n]])
return;
aktu(ojc[n]);
ojc[n] = ojc[ojc[n]];
}
void dfs_bcc(int n, int o, int oj) {
low[n] = gl[n];
for (auto [v, i] : edge[n]) {
if (i == o)
continue;
if (gl[v] == 0) {
gl[v] = gl[n] + 1;
dfs_bcc(v, i, n);
cmn(low[n], low[v]);
} else {
cmn(low[n], gl[v]);
}
}
// Check if bridge
if (low[n] != gl[n] && oj != -1) {
aktu(n);
aktu(oj);
if (ojc[n] != ojc[oj])
si[ojc[n]] += si[ojc[oj]];
ojc[ojc[oj]] = ojc[n];
}
}
int gl_n;
int ok, preff, suff;
void dfs(int n, int o) {
trees[n] = si[n];
oo[n] = o;
for (auto v : tree[n]) {
if (v == o)
continue;
dfs(v, n);
trees[n] += trees[v];
}
prefix[n] = sufix[n] = {0, gl_n+1};
// Prefix / suffix
if (vals[0] == vals[trees[n]-1])
prefix[n] = {1, gl_n+1};
if (vals[gl_n-1] == vals[gl_n-trees[n]])
sufix[n] = {1, gl_n+1};
for (int v : tree[n]) {
if (v == o)
continue;
if (prefix[v].FS && vals[trees[v]] == vals[trees[n]-1])
prefix[n] = {1, v};
if (sufix[v].FS && vals[gl_n-1-trees[v]] == vals[gl_n-trees[n]])
sufix[n] = {1, v};
}
pii big_pref={0, gl_n+1}, s_big_pref={0, gl_n+1}, big_suf={0, gl_n+1}, s_big_suf={0, gl_n+1};
for (int v : tree[n]) {
if (v == o)
continue;
if (prefix[v].FS) {
if (trees[v] > big_pref.FS)
s_big_pref = big_pref, big_pref = {trees[v], v};
else
cmx(s_big_pref, pii{trees[v], v});
}
if (sufix[v].FS) {
if (trees[v] > big_suf.FS)
s_big_suf = big_suf, big_suf = {trees[v], v};
else
cmx(s_big_suf, pii{trees[v], v});
}
}
if (big_pref.SD != big_suf.SD && vals[big_pref.FS] == vals[gl_n-1-big_suf.FS]) {
ok = n;
preff = big_pref.SD;
suff = big_suf.SD;
}
if (vals[s_big_pref.FS] == vals[gl_n-1-big_suf.FS]) {
ok = n;
preff = s_big_pref.SD;
suff = big_suf.SD;
}
if (vals[big_pref.FS] == vals[gl_n-1-s_big_suf.FS]) {
ok = n;
preff = big_pref.SD;
suff = s_big_suf.SD;
}
}
void sett(int n, int o1, int o2, int val) {
for (auto vv : gr[n])
res[vv] = val;
for (auto v : tree[n]) {
if (v != o1 && v != o2)
sett(v, n, n, val);
}
}
void recover(int n, bool pref) {
// cerr << n << "recover\n";
if (n == gl_n+1)
return;
if (pref) {
sett(n, oo[n], prefix[n].SD, vals[trees[prefix[n].SD]]);
recover(prefix[n].SD, pref);
} else {
sett(n, oo[n], sufix[n].SD, vals[gl_n - 1 - trees[sufix[n].SD]]);
recover(sufix[n].SD, pref);
}
}
void fun(int ii, bool samp) {
ok = -1;
int n, m;
cin >> n >> m;
if (ii == 294)
cout << n << " " << m << "\n";
gl_n=n;
for (int i = 0; i <= n+2; i++) {
ojc[i] = i;
si[i] = 1;
gl[i] = 0;
edge[i].clear();
tree[i].clear();
gr[i].clear();
low[i] = 1000000001;
prefix[i] = sufix[i] = {0, n+1};
trees[i] = 0; oo[i] = -1;
vals[i] = 0;
res[i] = 0;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
if (ii == 294)
cout << a << " " << b << "\n";
edge[a].PB({b, i});
edge[b].PB({a, i});
}
gl[1] = 1;
dfs_bcc(1, -1, -1);
vi vs;
for (int i = 1; i <= n; i++) {
aktu(i);
gr[ojc[i]].PB(i);
if (ojc[i] == i)
vs.PB(i);
}
// for (int i = 1; i <= n; i++) {
// if (gr[i].empty())
// continue;
// cerr << i << "(" << si[i] << "): ";
// for (auto v : gr[i])
// cerr << v << " ";
// cerr << "\n";
// }
// cerr << "\n";
map<pii, int> mapa;
for (int v = 1; v <= n; v++) {
for (auto [u, _] : edge[v]) {
int ou = ojc[u], ov = ojc[v];
if (ou <= ov)
continue;
if (mapa[{ou, ov}])
continue;
mapa[{ou, ov}] = 1;
tree[ou].PB(ov);
tree[ov].PB(ou);
// cerr << ou << " " << ov << "\n";
}
}
// cerr << "\n";
for (int i = 0; i < n; i++) {
cin >> vals[i];
if (ii == 294)
cout << vals[i] << "\n";
}
if (ii != 294 && !samp)
return;
// return;
sort(vals, vals+n);
dfs(vs[0], -1);
if (ok == -1) {
cout << "No\n";
} else {
cout << "Yes\n";
for (int i = 1; i <= n; i++) {
res[i] = vals[trees[preff]];
}
// cerr << ok << " " << preff << " " << suff << "\n";
recover(preff, 1);
recover(suff, 0);
for (int i = 1; i <= n; i++) {
cout << res[i] << " ";
}
cout << "\n";
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
fun(i, t==5);
}
}
详细
Test #1:
score: 100
Accepted
time: 11ms
memory: 91548kb
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 1 2 2 3 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 62ms
memory: 91604kb
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:
4 4 4 3 3 4 3 1 2 1 2 2 2 1 Yes 2 2 2 2
result:
wrong answer Token parameter [name=yesno] equals to "4", doesn't correspond to pattern "Yes|No" (test case 1)