当前位置:网站首页>CF566E-Restoring Map【bitset】
CF566E-Restoring Map【bitset】
2022-06-24 07:48:00 【QuantAsk】
正题
题目链接:https://www.luogu.com.cn/problem/CF566E
题目大意
有一棵树,但是你不知道它的形态。你现在只知道距离每个点距离不超过 2 2 2的点集,但是你不知道每个点集是对应哪个点的。
现在要你求这棵树。
2 ≤ n ≤ 1000 2\leq n\leq 1000 2≤n≤1000
解题思路
考虑这样一种情况
那么 ? ? ?和 ? ′ ?' ?′的交集恰好是 x x x和 y y y,也就是所有非叶子的连边我们都可以用以上方式确定。
然后考虑怎么确定叶子的连边,对于叶子 x x x来说,包含它的集合中最小的那个肯定是它自己的集合。
这样我们就可以确定每个叶子对应的集合了,然后考虑怎么求它的父亲。
会发现我们如果把叶子的集合中的叶子去掉,那就只剩下它的父节点和它父节点连接的其他非叶子节点。
我们再处理出一个非叶子节点连边的集合,然后一个一个比较就可以找到这个点的父亲了。
然后要特判一些情况:
- 没有非叶子节点:此时 n = 2 n=2 n=2直接特判。
- 只有一个非叶子节点:此时随便找一个点都可以当非叶子节点。
- 只有两个非叶子节点:此时叶子的集合分两种情况,分别对应不同的父节点就好了。
用 b i t s e t bitset bitset优化即可做到 O ( n 3 ω ) O(\frac{n^3}{\omega}) O(ωn3)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<vector>
#define mp(x,y) make_pair(x,y)
using namespace std;
const int N=1050;
int n,k[N],f[N];;
bitset<N> b[N],g[N],c,v;
vector<pair<int,int> >e;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)f[i]=n,g[i][i]=1;k[n]=n+1;
if(n==2)return puts("1 2")&0;
for(int i=0;i<n;i++){
scanf("%d",&k[i]);
for(int j=1,x;j<=k[i];j++){
scanf("%d",&x);x--;b[i][x]=1;
f[x]=(k[i]<k[f[x]])?i:f[x];
}
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
c=b[i]&b[j];
if(c.count()==2){
int a=c._Find_first();
int b=c._Find_next(a);
e.push_back(mp(min(a,b),max(a,b)));
g[a][b]=g[b][a]=v[a]=v[b]=1;
}
}
if(v.count()==0){
for(int i=1;i<n;i++)
printf("%d %d\n",i+1,1);
return 0;
}
else if(v.count()==2){
int p=v._Find_first();
int q=v._Find_next(p);
printf("%d %d\n",p+1,q+1);
bool flag=0;
for(int i=0;i<n;i++)
if(!v[i]){
if(flag){
if(b[f[i]]==c)
printf("%d %d\n",i+1,p+1);
else printf("%d %d\n",i+1,q+1);
}
else printf("%d %d\n",i+1,p+1),c=b[f[i]],flag=1;
}
return 0;
}
for(int i=0;i<n;i++){
if(v[i])continue;b[f[i]]&=v;
for(int j=0;j<n;j++)
if(b[f[i]]==g[j]){
e.push_back(mp(min(i,j),max(i,j)));break;}
}
sort(e.begin(),e.end());
for(int i=0;i<e.size();i++)
if(!i||e[i]!=e[i-1])printf("%d %d\n",e[i].first+1,e[i].second+1);
return 0;
}
边栏推荐
猜你喜欢

Spark - LeftOuterJoin 结果条数与左表条数不一致

当程序员被问会不会修电脑时… | 每日趣闻

Mba-day25 best value problem - application problem

Yolox backbone -- implementation of cspparknet

十二、所有功能实现效果演示

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
![[pytorch basic tutorial 30] code analysis of DSSM twin tower model](/img/4a/1f290990b39c517efb2b9cef0b5fcb.png)
[pytorch basic tutorial 30] code analysis of DSSM twin tower model

4274. 后缀表达式

linux(centos7.9)安装部署mysql-cluster 7.6

嵌入式 | 硬件转软件的几条建议
随机推荐
"I can't understand Sudoku, so I choose to play Sudoku."
A tip to read on Medium for free
[pytoch basic tutorial 31] youtubednn model analysis
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
支持向量机(SVC,NuSVC,LinearSVC)
用VNC Viewer的方式远程连接无需显示屏的树莓派
Data midrange: detailed explanation of the technical stack of data acquisition and extraction
12、 Demonstration of all function realization effects
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
Threejs glow channel 01 (unrealbroompass & layers)
学习太极创客 — ESP8226 (十三)OTA
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
小白学习MySQL - 增量统计SQL的需求
当程序员被问会不会修电脑时… | 每日趣闻
Idea another line shortcut
[quantitative investment] discrete Fourier transform to calculate array period
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
threejs辉光通道01(UnrealBloomPass && layers)
YOLOX backbone——CSPDarknet的实现
Applet cloud data, data request a method to collect data