QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#330927 | #8058. Binary vs Ternary | ucup-team1600# | RE | 0ms | 0kb | C++20 | 8.1kb | 2024-02-17 20:59:19 | 2024-02-17 20:59:19 |
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
mt19937 rng(4242);
const int max_n = 1e5 + 42, max_m = 2e5 + 42, inf = 1000111222;
vector<pair<int, int> > g[max_n];
vector<int> g2[max_n];
map<int, vector<int> > g3[max_n];
vector <pii> edge[max_n];
bool bridge[max_m] = {}, used[max_n] = {};
int tin[max_n] = {}, timer = 0, low[max_n] = {};
inline int dfs (int v, int p_id = -1) {
used[v] = 1;
tin[v] = timer++;
low[v] = inf;
for (auto [to, id] : edge[v]) {
if (id == p_id) {
continue;
}
if (used[to]) {
umin(low[v], tin[to]);
}
else {
umin(low[v], dfs(to, id));
if (low[to] > tin[v]) {
bridge[id] = 1;
}
}
}
return low[v];
}
int comp[max_n], am_comp[max_n], cur_comp = 0;
void dfs2(int v, int p) {
comp[v] = cur_comp;
am_comp[comp[v]]++;
for(auto& to : g[v])
if(!bridge[to.se] && to.fi != p && comp[to.fi] == -1)
dfs2(to.fi, v);
}
int subt[max_n];
int dfs3(int v, int p) {
subt[v] = am_comp[comp[v]];
for(auto& to : g2[v]) {
if(to != p) subt[v] += dfs3(to, v);
}
return subt[v];
}
set<pair<int, int> > bad_edge;
int val[max_n];
void dfs4(int v, int p) {
used[v] = true;
for(auto& to : g2[v])
if(to != p && !used[to] && !bad_edge.count({v, to})) {
val[to] = val[v];
dfs4(to, v);
}
}
void solve() {
int n, m;
cin >> n >> m;
bad_edge.clear();
for(int i = 0; i < n; i++) {
val[i] = -1;
comp[i] = -1; am_comp[i] = 0;
g[i].clear(); g2[i].clear(); g3[i].clear();
edge[i].clear();
used[i] = 0;
tin[i] = low[i] = 0;
}
for(int i = 0; i < m; i++) bridge[i] = false;
timer = 0;
vector<pair<int, int> > edg;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--; v--;
edg.pb({u, v});
edge[u].pb({v, i});
edge[v].pb({u, i});
g[u].pb({v, i});
g[v].pb({u, i});
}
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
sort(all(a));
vector<pair<int, int> > b;
for(int i = 0; i < n; i++) {
int j = i;
while(j + 1 < n && a[j + 1] == a[j]) j++;
b.pb({a[i], j - i + 1});
i = j;
}
vector<int> transitions(n + 1, -1);
int cur_sum = 0;
for(int i = 0; i < len(b); i++) {
cur_sum += b[i].se;
transitions[cur_sum] = i + 1;
}
int mx = -1;
for(int i = 0; i <= n; i++) {
umax(mx, transitions[i]);
transitions[i] = mx;
}
dfs(0);
cur_comp = 0;
for(int i = 0; i < n; i++)
if(comp[i] == -1) {
dfs2(i, i);
cur_comp++;
}
for(int i = 0; i < m; i++) {
int u = comp[edg[i].fi], v = comp[edg[i].se];
if(u != v) {
// LOG(u, v);
g2[u].pb(v);
g2[v].pb(u);
}
}
// for(int i = 0; i < n; i++) cout << comp[i] << ' ';
// cout << '\n';
dfs3(0, 0);
// for(int i = 0; i < n; i++) cout << subt[i] << ' ';
// cout << '\n';
for(int i = 0; i < m; i++) {
int u = comp[edg[i].fi], v = comp[edg[i].se];
if(u != v) {
if(subt[v] > subt[u]) swap(u, v);
int size_v = subt[v], size_u = n - subt[v];
cout << v << ' ' << u << ' ' << size_v << ' ' << size_u << '\n';
if(transitions[size_v] != -1) {
LOG(edg[i].fi, edg[i].se, v, u, transitions[size_v] - 1);
//(v, transitions[size_v] - 1) -> (u, transitions[size_v])
g3[v][transitions[size_v] - 1].pb(u);
}
if(transitions[size_u] != -1) {
LOG(edg[i].fi, edg[i].se, u, v, transitions[size_u] - 1);
//(u, transitions[size_u] - 1) -> (v, transitions[size_u])
g3[u][transitions[size_u] - 1].pb(v);
}
}
}
set<pair<pair<int, int>, int> > was;
map<pair<int, int>, int> was_from;
map<pair<pair<int, int>, int>, pair<pair<int, int>, int> > pr;
queue<pair<pair<int, int>, int> > q;
for(int i = 0; i < len(b); i++) {
was.insert({{i, 0}, -1});
q.push({{i, 0}, -1});
}
while(!q.empty()) {
auto cr = q.front(); q.pop();
int V = cr.fi.fi, MODE = cr.fi.se, FROM = cr.se;
for(auto& to : g3[V][MODE]) {
if(FROM != to && !was.count({{to, MODE + 1}, FROM}) && was_from[{to, MODE + 1}] <= 2) {
was_from[{to, MODE + 1}]++;
was.insert({{to, MODE + 1}, V});
q.push({{to, MODE + 1}, V});
pr[{{to, MODE + 1}, V}] = cr;
}
}
}
// for(int i = 0; i < n; i++) cout << comp[i] << ' ';
// cout << '\n';
for(int i = 0; i < n; i++) used[i] = false;
for(auto& x : was) {
int V = x.fi.fi, MODE = x.fi.se, FROM = x.se;
if(MODE == len(b) - 1) {
vector<int> to_go_from;
int cv = V, cmode = MODE, cfrom = FROM;
val[cv] = b[len(b) - 1 - cmode].fi;
LOG(b[cmode].fi);
while(pr.count({{cv, cmode}, cfrom})) {
int nv, nmode, nfrom;
auto to = pr[{{cv, cmode}, cfrom}];
nv = to.fi.fi, nmode = to.fi.se, nfrom = to.se;
if(nmode != cmode) {
bad_edge.insert({cv, nv});
bad_edge.insert({nv, cv});
val[nv] = b[len(b ) - 1 - nmode].fi;
}
LOG(cv, cmode, cfrom);
LOG(nv, nmode, nfrom);
cv = nv, cmode = nmode, cfrom = nfrom;
}
for(auto v : to_go_from) if(!used[v]) dfs4(v, v);
cout << "Yes\n";
for(int j = 0; j < n; j++) cout << val[comp[j]] << ' ';
cout << '\n';
return;
}
}
// for(int i = 0; i < n; i++)
// if(was.count({i, len(b) - 1})) {
// int cv = i, cmode = len(b) - 1;
// val[cv] = b[cmode].fi;
// to_go_from.pb(cv);
// LOG(cv, cmode);
// while(pr.count({cv, cmode})) {
// int nv, nmode;
// tie(nv, nmode) = pr[{cv, cmode}];
// if(nmode != cmode) {
// bad_edge.insert({cv, nv});
// bad_edge.insert({nv, cv});
// val[nv] = b[nmode].fi;
// }
// cv = nv; cmode = nmode;
// LOG(cv, cmode);
// to_go_from.pb(cv);
// }
//// for(auto v : to_go_from) if(!used[v]) dfs4(v, v);
// cout << "Yes\n";
// for(int j = 0; j < n; j++) cout << val[comp[j]] << ' ';
// cout << '\n';
// return;
// }
cout << "No\n";
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
}
/*
KIVI
*/
详细
Test #1:
score: 0
Runtime Error
input:
3 1 111 110110 1101010 1111 111111