QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380675 | #8055. Balance | pope | WA | 5ms | 14980kb | C++14 | 7.2kb | 2024-04-07 08:24:33 | 2024-04-07 08:24:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#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()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define trav(a, x) for (auto& a: x)
#define f first
#define s second
#define pb push_back
const char nl = '\n';
#ifdef DBG
void dbg_out() { cerr << nl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
#define dbg(...) dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MX = 2e5+5;
int par[MX];
int SZ[MX];
int find(int x) {
return (x==par[x]?x:par[x]=find(par[x]));
}
void merge(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (SZ[a] < SZ[b]) swap(a, b);
SZ[a] += SZ[b];
par[b] = a;
SZ[b] = 0;
}
vi gr[MX];
vector<pii> edges;
vi num, st;
vector<vector<pii>> ed;
int Time;
template <class F>
int dfs(int at, int par, F& f) {
int me = num[at] = ++Time, e, y, top=me;
for (auto pa : ed[at]) if (pa.second != par) {
tie(y, e) = pa;
if (num[y]) {
top = min(top, num[y]);
if (num[y] < me) {
st.pb(e);
}
} else {
int si = sz(st);
int up = dfs(y, e, f);
top = min(top, up);
if (up == me) {
st.pb(e);
f(vi(st.begin()+si, st.end()));
st.resize(si);
}
else if (up < me) st.pb(e);
else {
// e is a bridge
// create an edge in the graph
int a = find(edges[e].f);
int b = find(edges[e].s);
gr[a].pb(b);
gr[b].pb(a);
}
}
}
return top;
}
template <class F>
void bicomps(F f) {
num.assign(sz(ed), 0);
rep(i, 0, sz(ed)) if (!num[i]) dfs(i, -1, f);
}
struct Info {
int subtreeSize = 0;
int downLen = 0;
int downCh = -1;
int upLen = 0;
int upCh = -1;
};
Info infos[MX];
bool good[MX];
int n, m;
int best = 0;
int root, upNode, downNode;
void solve(int v, int p) {
infos[v].subtreeSize = SZ[find(v)];
vector<pii> ups;
vector<pii> downs;
trav(u, gr[v]) {
int node = find(u);
if (node == p) continue;
solve(node, v);
// check if this is a good break edge going either up or down
int valUp = infos[node].upLen;
int valDown = infos[node].downLen;
if (good[infos[node].subtreeSize]) {
// then we can add to 1 this
valUp++;
}
if (good[n-infos[node].subtreeSize]) {
valDown++;
}
ups.pb({valUp, node});
downs.pb({valDown, node});
infos[v].subtreeSize += infos[node].subtreeSize;
}
if (sz(ups) == 0) return;
// find highest two
sort(all(ups));
reverse(all(ups));
sort(all(downs));
reverse(all(downs));
// first just set the vals of v
infos[v].upLen = ups[0].f;
infos[v].upCh = ups[0].s;
infos[v].downLen = downs[0].f;
infos[v].downCh = downs[0].s;
if (sz(ups) == 1) return;
// merge the best up and best down as long as they're not the same child
// try best up and best down
// also just try best up and nothing and best down and nothing
if (ups[0].s != downs[0].s) {
int len = ups[0].f + downs[0].f;
if (len > best) {
best = len;
root = v;
upNode = ups[0].s;
downNode = downs[0].s;
}
}
if (ups[0].s != downs[1].s) {
int len = ups[0].f + downs[1].f;
if (len > best) {
best = len;
root = v;
upNode = ups[0].s;
downNode = downs[1].s;
}
}
if (ups[1].s != downs[0].s) {
int len = ups[1].f + downs[0].f;
if (len > best) {
best = len;
root = v;
upNode = ups[1].s;
downNode = downs[0].s;
}
}
int len = ups[0].f;
if (len > best) {
best = len;
root = v;
upNode = ups[0].s;
downNode = -1;
}
len = downs[0].f;
if (len > best) {
best = len;
root = v;
upNode = -1;
downNode = downs[0].s;
}
}
int vis[MX];
void mark(int v, int x, int nxt) {
if (v == nxt) return; // don't cross into next section
if (vis[v]) return;
vis[v] = x;
trav(u, gr[v]) {
mark(u, x, nxt);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int t; cin >> t;
while (t--) {
best = -1;
root = upNode = downNode = -1;
cin >> n >> m;
// reset everything
rep(i, 0, n) {
par[i] = i;
SZ[i] = 1;
}
rep(i, 0, n) {
infos[i] = Info();
}
rep(i, 0, n) vis[i] = 0;
rep(i, 0, n) gr[i].clear();
edges.clear();
int eid = 0;
ed.clear();
ed.resize(MX);
Time = 0;
num.clear();
st.clear();
rep(i, 0, m) {
int u, v; cin >> u >> v;
u--; v--;
edges.pb({u, v});
ed[u].emplace_back(v, eid);
ed[v].emplace_back(u, eid++);
}
bicomps([&](const vi& edgelist) {
// merge the whole component togther
trav(e, edgelist) {
merge(edges[e].f, edges[e].s);
}
});
int freq[n+1]; rep(i, 0, n+1) freq[i] = 0;
rep(i, 0, n) {
int x; cin >> x;
freq[x]++;
}
vector<pii> sizes;
rep(i, 1, n+1) {
if (freq[i]) sizes.pb({i, freq[i]});
}
// find all the good breaks
rep(i, 0, n) good[i] = 0;
int cur = 0;
trav(p, sizes) {
dbg(p.f, p.s);
cur += p.s;
if (cur != n) good[cur] = 1;
}
assert(cur == n);
// now dfs from random root
solve(find(0), -1);
int d = sz(sizes);
if (best != d-1) {
cout << "NO" << nl;
continue;
}
cout << "YES" << nl;
// we have a path with d-1 breaks in it
// this path is stored in root, upNode, downNode
vi path;
while (upNode != -1) {
path.pb(upNode);
upNode = infos[upNode].upCh;
}
reverse(all(path));
path.pb(root);
while (downNode != -1) {
path.pb(downNode);
downNode = infos[downNode].downCh;
}
trav(v, path) cerr << v << ' '; cerr << nl;
assert(sz(path) == d);
rep(i, 0, sz(path)) {
int nxt = -1;
if (i != sz(path)-1) nxt = path[i+1];
mark(path[i], sizes[i].f, nxt);
}
rep(i, 0, n) {
cout << vis[find(i)] << " ";
}
cout << nl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 14980kb
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:
NO NO YES 2 2 2 3 1 NO NO
result:
wrong answer Token parameter [name=yesno] equals to "NO", doesn't correspond to pattern "Yes|No" (test case 1)