QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344409 | #8055. Balance | ucup-team173 | WA | 156ms | 4040kb | C++17 | 4.6kb | 2024-03-04 15:02:34 | 2024-03-04 15:02:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
#define SZ(a) (int(a.size()))
typedef long long ll;
typedef double db;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
std::mt19937_64 gen(std::chrono::system_clock::now().time_since_epoch().count());
ll get(ll l, ll r) { std::uniform_int_distribution<ll> dist(l, r); return dist(gen); }
void solve() {
int n, m;
cin >> n >> m;
vector G(n + 1, vector<pii>());
vector e(m + 1, pii());
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
G[u].pb(Mp(v, i));
G[v].pb(Mp(u, i));
e[i] = Mp(u, v);
}
vi a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a.begin() + 1, a.end());
vi fe(n + 1), dfn(n + 1);
vi low(n + 1), cut(m + 1);
vi bel(n + 1), cnt(n + 1);
int dfc = 0, tot = 0;
function<void(int)> tarjan = [&](int x) {
dfn[x] = ++dfc, low[x] = dfc;
for(auto [y, eid] : G[x]) {
if(!dfn[y]) {
fe[y] = eid;
tarjan(y);
if(low[x] > low[y]) {
low[x] = low[y];
}
} else if(fe[x] != eid && low[x] > dfn[y]) {
low[x] = dfn[y];
}
}
if(fe[x] && low[x] == dfn[x]) {
cut[fe[x]] = 1;
}
};
tarjan(1);
function<void(int, int)> dfs = [&](int x, int c) {
bel[x] = c, cnt[c]++;
for(auto [y, eid] : G[x]) {
if(bel[y] || cut[eid]) continue;
dfs(y, c);
}
};
for(int i = 1; i <= n; i++) {
if(!bel[i]) {
dfs(i, ++tot);
}
}
// cerr << "tot = " << tot << endl;
// for(int i = 1; i <= n; i++) {
// cerr << bel[i] << " \n"[i == n];
// }
vector nG(tot + 1, vi());
for(int i = 1; i <= m; i++) {
auto [u, v] = e[i];
u = bel[u], v = bel[v];
if(u == v) {
continue;
}
nG[u].pb(v);
nG[v].pb(u);
}
int diff_cnt = 0;
vi diff(n + 1);
for(int i = 1; i < n; i++) {
if(a[i] != a[i + 1]) {
diff_cnt++;
diff[i] = 1;
}
}
vector f(tot + 1, vi(2)), g(tot + 1, vi(2));
vi sz(tot + 1), ans(tot + 1), fa(tot + 1);
int fl = 0;
function<void(int, int)> dfs2 = [&](int x, int fz) {
sz[x] = cnt[x], fa[x] = fz, g[x] = {x, x};
for(auto y : nG[x]) if(y != fz && !fl) {
dfs2(y, x);
sz[x] += sz[y];
if(f[x][1] + f[y][0] + diff[sz[y]] == diff_cnt && !fl) {
fl = 1;
for(int t = g[y][0]; t != x; t = fa[t]) {
ans[t] = a[sz[t]];
}
for(int t = g[x][1]; t != x; t = fa[t]) {
ans[t] = a[n - sz[t] + 1];
}
ans[x] = a[sz[y] + 1];
}
if(f[x][0] + f[y][1] + diff[n - sz[y]] == diff_cnt && !fl) {
fl = 1;
for(int t = g[y][1]; t != x; t = fa[t]) {
ans[t] = a[n - sz[t] + 1];
}
for(int t = g[x][0]; t != x; t = fa[t]) {
ans[t] = a[sz[t]];
}
ans[x] = a[n - sz[y]];
}
if(f[y][0] + diff[sz[y]] > f[x][0]) {
f[x][0] = f[y][0] + diff[sz[y]];
g[x][0] = g[y][0];
}
if(f[y][1] + diff[n - sz[y]]) {
f[x][1] = f[y][1] + diff[n - sz[y]];
g[x][1] = g[y][1];
}
}
};
dfs2(1, 0);
if(!fl) {
cout << "No\n";
return;
}
cout << "Yes\n";
queue<int> q;
for(int i = 1; i <= tot; i++) {
if(ans[i]) q.push(i);
}
while(SZ(q)) {
int x = q.front(); q.pop();
for(int y : nG[x]) if(!ans[y]) {
ans[y] = ans[x];
q.push(y);
}
}
for(int i = 1; i <= n; i++)
cout << ans[bel[i]] << " \n"[i == n];
}
signed main() {
atexit([](){cerr << "time: " << (db)clock()/CLOCKS_PER_SEC << "s\n";});
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3932kb
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: 156ms
memory: 4040kb
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 No No No No No Yes 1 1 1 1 1 Yes 1 3 1 1 1 Yes 1 1 1 Yes 2 1 Yes 1 1 1 1 1 Yes 2 1 No No Yes 1 1 1 Yes 1 1 No No Yes 2 2 2 2 2 No Yes 1 1 Yes 1 1 1 2 No No No Yes 1 1 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 Yes 1 1 ...
result:
wrong answer ans finds an answer, but out doesn't (test case 9)