QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#422568 | #8055. Balance | chenk | WA | 79ms | 50676kb | C++17 | 3.1kb | 2024-05-27 16:26:14 | 2024-05-27 16:26:15 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e6 + 5;
int n, m, k;
vector<int> adj[N], bdj[N];
int a[N], b[N], uni[N], fat[N], dep[N], siz[N], seq[N];
pair<int, int> eg[N];
int vis[N];
pair<int, int> f[N], g[N];
bool ok;
inline int find(int x) {
return x == uni[x] ? x : uni[x] = find(uni[x]);
}
inline void dfs1(int u, int fa) {
fat[u] = fa;
dep[u] = dep[fa] + 1;
for(int v : adj[u]) if(v != fa) dfs1(v, u);
}
inline void dfs3(int u, int fa, int flag) {
if(flag == 1) {
b[u] = a[seq[f[u].first + 1]];
} else {
b[u] = a[seq[g[u].first - 1]];
}
int t = flag == 1 ? f[u].second : g[u].second;
if(t != u) dfs3(t, u, flag);
}
inline void dfs4(int u, int fa) {
for(int v : bdj[u]) if(v != fa && !b[v]) {
b[v] = b[u];
dfs4(v, u);
}
}
inline void dfs2(int u, int fa) {
f[u] = {0, u}, g[u] = {k+1, u};
for(int v : bdj[u]) if(v != fa) {
dfs2(v, u);
if(ok) return;
siz[u] += siz[v];
auto x = make_pair(f[v].first, v);
auto y = make_pair(g[v].first, v);
auto vf = f[u], vg = g[u];
f[u] = max(f[u], x);
g[u] = min(g[u], y);
if(vis[siz[v]] == f[v].first + 1) {
f[u] = max(f[u], make_pair(f[v].first + 1, v));
if(f[v].first + 1 + 1 == vg.first - 1) {
ok = 1;
g[u] = vg;
dfs3(u, fa, 1);
dfs3(u, fa, -1);
return;
}
}
if(vis[n - siz[v]] + 1 == g[v].first - 1) {
g[u] = min(g[u], make_pair(g[v].first - 1, v));
if(vf.first + 1 == g[v].first - 1 - 1) {
ok = 1;
f[u] = vf;
dfs3(u, fa, 1);
dfs3(u, fa, - 1);
return;
}
}
}
if(f[u].first == k) {
ok = 1;
dfs3(u, fa, 1);
return;
}
if(g[u].first == 1) {
ok = 1;
dfs3(u, fa, -1);
}
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; ++i) adj[i].clear(), bdj[i].clear();
for(int i = 1; i <= m; ++i) cin >> eg[i].first >> eg[i].second;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) uni[i] = i;
for(int i = 1; i <= m; ++i) {
vis[i] = 0;
auto [u, v] = eg[i];
if(find(u) != find(v)) {
adj[u].push_back(v);
adj[v].push_back(u);
uni[find(u)] = find(v);
vis[i] = 1;
}
}
for(int i = 1; i <= n; ++i) uni[i] = i, siz[i] = 0;
dfs1(1, 0);
for(int i = 1; i <= m; ++i) if(!vis[i]) {
auto [u, v] = eg[i];
u = find(u), v = find(v);
while(u != v) {
if(dep[u] < dep[v]) swap(u, v);
uni[u] = find(fat[u]);
u = find(u);
}
}
for(int i = 1; i <= n; ++i) {
++siz[find(i)];
for(int j : adj[i]) if(find(i) != find(j)) bdj[find(i)].push_back(find(j));
}
sort(a + 1, a + n + 1);
a[n + 1] = -1;
int cc = 0;
for(int i = 1; i <= n; ++i) vis[i] = -1;
for(int i = 1; i <= n; ++i) if(a[i] != a[i+1]) seq[vis[i] = ++cc] = i;
k = cc;
ok = 0;
for(int i = 1; i <= n; ++i) b[i] = 0;
dfs2(find(1), 0);
if(!ok) {
cout << "No" << "\n";
} else {
cout << "Yes" << "\n";
for(int i = 1; i <= n; ++i) if(b[i]) dfs4(i, 0);
for(int i = 1; i <= n; ++i) cout << b[find(i)] << " \n"[i == n];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int t; cin >> t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 50520kb
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: 79ms
memory: 50676kb
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:
No No No No No Yes 2 1 1 1 No No No No No No No No Yes 1 3 1 1 1 No Yes 2 1 No Yes 2 1 No No No No No No No No No Yes 1 1 1 2 No No No No 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 2 1 1 No No Yes 2 2 1 No No No No No No No No No No No No No No No No No Yes 2 1 2 Yes 4 1 1 1 ...
result:
wrong answer ans finds an answer, but out doesn't (test case 1)