QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#351696 | #8055. Balance | luanmenglei | WA | 0ms | 15932kb | C++17 | 3.6kb | 2024-03-12 12:32:42 | 2024-03-12 12:32:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
namespace SOL {
using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
va_list arg;
static char pbString[512];
va_start(arg,msg);
vsprintf(pbString,msg,arg);
cerr << "[DEBUG] " << pbString << "\n";
va_end(arg);
#endif
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }
const int N = 1e5 + 10;
const int M = 2e5 + 10;
int n, m, c[N], tot, sze[N], dfn[N], cnt, low[N], a[N], fa[N], point, ans[N], puresze[N];
bool bridge[M];
vector<array<int, 2>> G[N];
vector<int> T[N], vert[N];
array<int, 2> edge[M];
void tarjan(int x, int lst) {
dfn[x] = low[x] = ++ cnt;
for (auto [y, id] : G[x]) if (id != lst) {
if (!dfn[y]) {
tarjan(y, id);
chkmin(low[x], low[y]);
if (low[y] > dfn[x])
bridge[id] = true;
} else
chkmin(low[x], dfn[y]);
}
}
void dfs1(int x) {
c[x] = tot, sze[tot] ++, vert[tot].push_back(x);
for (auto [y, id] : G[x]) if (!bridge[id] && !c[y])
dfs1(y);
}
array<int, 2> mx[N], semx[N], f[N];
void dfs2(int x, int fa) {
mx[x] = semx[x] = f[x] = { 0, x };
for (int y : T[x]) if (y != fa) {
dfs2(y, x);
sze[x] += sze[y];
}
}
void dpup(int x, int fa) {
for (int y : T[x]) if (y != fa) {
dpup(y, x);
array<int, 2> val = { mx[y][0] + (a[sze[y]] != a[sze[y] + 1]), 0 };
if (mx[x] < val) {
semx[x] = mx[x];
mx[x] = val;
} else if (semx[x] < val) {
semx[x] = val;
}
}
}
void dpdown(int x, int fa) {
for (int y : T[x]) if (y != fa) {
int z = (a[n - sze[y]] != a[n - sze[y] + 1]);
chkmax(f[y], (array<int, 2>) { f[x][0] + z, f[x][1] });
if (mx[x][1] == y)
chkmax(f[y], (array<int, 2>) { semx[x][0] + z, semx[x][1] });
else
chkmax(f[y], (array<int, 2>) { mx[x][0] + z, mx[x][1] });
dpdown(y, x);
}
chkmax(f[x], mx[x]);
}
void dfs3(int x, int _fa) {
fa[x] = _fa;
for (int y : T[x]) if (y != _fa)
dfs3(y, x);
}
void dfs4(int x, int fa, int limx, int limy) {
for (int i = 0; i < (int) vert[x].size(); i ++)
ans[vert[x][i]] = a[++ point];
for (int y : T[x]) if (y != fa && y != limx && y != limy)
dfs4(y, x, limx, limy);
}
void output(int x, int y) {
dfs3(x, 0);
int lst = 0;
while (y) {
dfs4(y, 0, fa[y], lst);
lst = y, y = fa[y];
}
}
void solve() {
cin >> n >> m;
tot = cnt = point = 0;
for (int i = 1; i <= n; i ++) {
G[i].clear(), T[i].clear(), vert[i].clear();
fa[i] = sze[i] = c[i] = dfn[i] = low[i] = 0;
}
for (int i = 1, x, y; i <= m; i ++) {
cin >> x >> y;
G[x].push_back({ y, i }), G[y].push_back({ x, i });
edge[i] = { x, y };
bridge[i] = false;
}
tarjan(1, 0);
for (int i = 1; i <= n; i ++)
if (!c[i])
tot ++, dfs1(i), puresze[tot] = sze[tot];
for (int i = 1; i <= m; i ++) {
auto [x, y] = edge[i];
x = c[x], y = c[y];
if (x == y)
continue;
T[x].push_back(y), T[y].push_back(x);
}
dfs2(1, 0);
for (int i = 1; i <= n; i ++)
cin >> a[i];
sort(a + 1, a + 1 + n);
int brk = 0;
for (int i = 1; i < n; i ++)
brk += (a[i] != a[i + 1]);
dpup(1, 0);
dpdown(1, 0);
for (int i = 1; i <= tot; i ++) {
if (f[i][0] == brk) {
cout << "Yes\n";
output(f[i][0], i);
for (int j = 1; j <= n; j ++)
cout << ans[j] << " \n"[j == n];
return;
}
}
cout << "No\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int tt; cin >> tt;
while (tt --)
SOL::solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 15932kb
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 1 2 2 3 Yes 1 1 1 2 2 No
result:
wrong answer max - min = 1, but sum of difference = 2 (test case 4)