QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359382 | #8215. Isomorphic Delight | installb# | WA | 98ms | 14252kb | C++20 | 7.2kb | 2024-03-20 17:12:27 | 2024-03-20 17:12:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int SZ = 18;
const int SZ = 21;
struct tree{
int n,id;
vector <pair <int,int> > E;
}; int tot = 0;
int P[2] = {998244353,1000000007};
int h[2][SZ + 5];
vector <int> G[SZ + 5];
unsigned int xorshift(unsigned int x,int id){
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x %= P[id];
return x;
}
int fa[SZ + 5];
void dfs_hash(int u,int f){
h[0][u] = h[1][u] = 1;
fa[u] = f;
for(int v : G[u]){
if(v == f) continue;
dfs_hash(v,u);
for(int id = 0;id < 2;id ++)
h[id][u] = (h[id][u] + xorshift(h[id][v],id)) % P[id];
}
}
void settr(tree x){
for(int i = 1;i <= x.n;i ++) G[i].clear();
for(auto [x,y] : x.E){
G[x].push_back(y);
G[y].push_back(x);
}
}
vector <tree> tr[SZ + 5];
tree son[SZ + 5];
void dfs_rooted(int dep,int sz){
if(sz == 0){
tree cur;
cur.n = 1;
for(int i = 1;i < dep;i ++){
cur.E.push_back({1,cur.n + 1});
for(auto [x,y] : son[i].E){
cur.E.push_back({cur.n + x,cur.n + y});
}
cur.n += son[i].n;
}
settr(cur);
dfs_hash(1,0);
for(int i = 1;i <= cur.n;i ++){
set <pair <int,int> > st; int cnt = 0;
for(int j : G[i]){
if(j != fa[i]) cnt ++,st.insert({h[0][j],h[1][j]});
}
if(st.size() < cnt) return;
}
cur.id = ++ tot;
tr[cur.n].push_back(cur);
return;
}
for(int i = 1;i <= sz;i ++){
for(tree x : tr[i]){
if(x.id <= son[dep - 1].id) continue;
son[dep] = x;
dfs_rooted(dep + 1,sz - i);
}
}
}
map <pair <int,int>,int> vis;
map <pair <pair <int,int>,pair <int,int> >,int> vis_double;
vector <tree> ans_tree;
int siz[SZ + 5],mxs[SZ + 5];
void dfs_sz(int u,int f){
siz[u] = 1;
mxs[u] = 0;
fa[u] = f;
for(int v : G[u]){
if(v == f) continue;
dfs_sz(v,u);
siz[u] += siz[v];
mxs[u] = max(mxs[u],siz[v]);
}
}
pair <int,int> calc_cen(int n){
dfs_sz(1,0);
vector <int> ret; int mi = n + 1;
for(int i = 1;i <= n;i ++) mi = min(mi,max(mxs[i],n - siz[i]));
for(int i = 1;i <= n;i ++) if(max(mxs[i],n - siz[i]) == mi) ret.push_back(i);
if(ret.size() == 1) return {ret[0],-1};
return {ret[0],ret[1]};
}
int num = 0;
void dfs(int dep,int sz,int szlim){
if(num > 1000000) return;
if(sz == 0){
tree cur;
cur.n = 1;
for(int i = 1;i < dep;i ++){
cur.E.push_back({1,cur.n + 1});
for(auto [x,y] : son[i].E){
cur.E.push_back({cur.n + x,cur.n + y});
}
cur.n += son[i].n;
}
settr(cur);
// set <pair <int,int> > st;
// for(int i = 1;i <= cur.n;i ++){
// dfs_hash(i,0);
// st.insert({h[0][i],h[1][i]});
// }
// if(st.size() < cur.n) return;
auto [u,v] = calc_cen(cur.n);
if(v == -1){
dfs_hash(u,0);
if(vis[{h[0][u],h[1][u]}]) return;
vis[{h[0][u],h[1][u]}] = 1;
}
else{
dfs_hash(u,0);
set <pair <int,int> > st; int cc = 0;
for(int w : G[u]){
if(w == v) continue;
st.insert({h[0][w],h[1][w]}); cc ++;
}
for(int w : G[v]){
if(w == u) continue;
st.insert({h[0][w],h[1][w]}); cc ++;
}
if(st.size() < cc) return;
pair <int,int> pl,pr;
pl = {h[0][u],h[1][u]};
dfs_hash(v,0);
pr = {h[0][v],h[1][v]};
if(pl > pr) swap(pl,pr);
if(vis_double[{pl,pr}]) return;
vis_double[{pl,pr}] = 1;
}
cur.id = ++ tot;
ans_tree.push_back(cur);
num += cur.n;
return;
}
for(int i = 1;i <= min(sz,szlim / 2);i ++){
for(int j = 1;j < tr[i].size();j ++) assert(tr[i][j].id > tr[i][j - 1].id);
for(tree x : tr[i]){
if(x.id <= son[dep - 1].id) continue;
son[dep] = x;
dfs(dep + 1,sz - i,szlim);
}
}
}
void init(){
tree now;
now.n = 1; now.id = ++ tot;
tr[1].push_back(now);
ans_tree.push_back(now);
for(int cur_sz = 2;cur_sz <= (SZ / 2);cur_sz ++) dfs_rooted(1,cur_sz - 1);
// for(int i = 1;i <= SZ;i ++){
// cout << "rooted tree size " << i << " : \n";
// cout << tr[i].size() << '\n';
// for(auto u : tr[i]){
// for(auto [x,y] : u.E) cout << x << ',' << y << ' ';
// cout << '\n';
// }
// }
for(int cur_sz = 7;cur_sz <= SZ;cur_sz ++){
vis.clear();
vis_double.clear();
dfs(1,cur_sz - 1,cur_sz);
}
sort(ans_tree.begin(),ans_tree.end(),[&](tree x,tree y){ return x.n < y.n; });
// for(auto tr : ans_tree){
// cout << tr.n << " : ";
// for(auto [u,v] : tr.E) cout << u << ',' << v << ' ';
// cout << '\n';
// }
// cout << "COUNT = " << ans_tree.size() << '\n';
}
void solve(){
int n; cin >> n;
vector <pair <int,int> > vec;
if(n == 1){
cout << "YES\n0\n";
}
else if(n >= 2 && n <= 5){
cout << "NO\n";
}
else if(n == 6){
cout << "YES\n";
cout << "6" << '\n';
cout << "1 2" << '\n';
cout << "2 3" << '\n';
cout << "1 3" << '\n';
cout << "3 4" << '\n';
cout << "2 5" << '\n';
cout << "5 6" << '\n';
}
else if(n == 7){
cout << "YES\n";
cout << "6" << '\n';
cout << "1 2" << '\n';
cout << "1 3" << '\n';
cout << "3 4" << '\n';
cout << "1 5" << '\n';
cout << "5 6" << '\n';
cout << "6 7" << '\n';
}
else{
int now = 0;
for(int i = 0;i < ans_tree.size();i ++){
if(n - ans_tree[i].n < ans_tree[i + 1].n){
// this is the last tree
settr(ans_tree[i]);
dfs_sz(1,0);
int u = 1;
while(1){
int id = -1;
for(int v : G[u]){
if(v == fa[u]) continue;
if(id == -1 || siz[v] > siz[id]) id = v;
}
if(id == -1) break;
u = id;
}
while(n > ans_tree[i].n){
ans_tree[i].E.push_back({u,++ ans_tree[i].n});
u = ans_tree[i].n;
}
}
for(auto [u,v] : ans_tree[i].E) vec.push_back({u + now,v + now});
now += ans_tree[i].n;
n -= ans_tree[i].n;
if(n == 0) break;
}
cout << vec.size() << '\n';
for(auto [x,y] : vec) cout << x << ' ' << y << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
init();
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 98ms
memory: 14252kb
input:
1
output:
YES 0
result:
ok Everything ok
Test #2:
score: 0
Accepted
time: 97ms
memory: 14152kb
input:
6
output:
YES 6 1 2 2 3 1 3 3 4 2 5 5 6
result:
ok Everything ok
Test #3:
score: 0
Accepted
time: 97ms
memory: 14064kb
input:
4
output:
NO
result:
ok Everything ok
Test #4:
score: 0
Accepted
time: 93ms
memory: 14112kb
input:
2
output:
NO
result:
ok Everything ok
Test #5:
score: 0
Accepted
time: 96ms
memory: 14116kb
input:
3
output:
NO
result:
ok Everything ok
Test #6:
score: 0
Accepted
time: 94ms
memory: 14164kb
input:
5
output:
NO
result:
ok Everything ok
Test #7:
score: 0
Accepted
time: 82ms
memory: 13984kb
input:
7
output:
YES 6 1 2 1 3 3 4 1 5 5 6 6 7
result:
ok Everything ok
Test #8:
score: -100
Wrong Answer
time: 90ms
memory: 14144kb
input:
8
output:
6 2 3 2 4 4 5 2 6 6 7 7 8
result:
wrong answer Token parameter [name=YES or NO] equals to "6", doesn't correspond to pattern "[A-Za-z]+"