当前位置:网站首页>CF566E-Restoring Map【bitset】
CF566E-Restoring Map【bitset】
2022-06-24 09:23:00 【QuantAsk】
On the subject
Topic link :https://www.luogu.com.cn/problem/CF566E
The main idea of the topic
There is a tree. , But you don't know its shape . Now you only know that the distance from each point is no more than 2 2 2 Point set of , But you don't know which point each point set corresponds to .
Now I want you to beg the tree .
2 ≤ n ≤ 1000 2\leq n\leq 1000 2≤n≤1000
Their thinking
Consider a situation like this 
that ? ? ? and ? ′ ?' ?′ The intersection of is exactly x x x and y y y, That is, we can determine all the edges of non leaves in the above way .
Then consider how to determine the edge of the leaf , For leaves x x x Come on , The smallest of the sets containing it must be its own set .
So we can determine the set corresponding to each leaf , Then consider how to ask its father .
We will find that if we remove the leaves from the set of leaves , That leaves only its parent node and other non leaf nodes connected to its parent node .
We then deal with a set of non leaf nodes with edges , Then one by one, we can find the father of this point .
Then we have to judge some special situations :
- No non leaf nodes : here n = 2 n=2 n=2 Direct special judgment .
- There is only one non leaf node : At this point, any point can be used as a non leaf node .
- There are only two non leaf nodes : At this point, the set of leaves can be divided into two cases , Just correspond to different parent nodes .
use b i t s e t bitset bitset Optimization can achieve 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;
}
边栏推荐
- Microblog writing - flow chart - sequence chart - Gantt chart - Mermaid flow chart - good results
- 金仓KFS replicator安装(Oracle-KES)
- Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
- 小白学习MySQL - 增量统计SQL的需求
- Numpy numpy中的np.c_和np.r_详解
- Easyexcel single sheet and multi sheet writing
- Squid代理服务器应用
- 【LeetCode】541. Reverse string II
- NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
- Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
猜你喜欢

Zero foundation self-study SQL course | related sub query

MySQL data (Linux Environment) scheduled backup

【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试

2022.6.13-6.19 AI行业周刊(第102期):职业发展

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

Redis implements a globally unique ID

2022.06.23 (traversal of lc_144,94145\
Depens:*** but it is not going to be installed
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

浮点数表示法(总结自CS61C和CMU CSAPP)
随机推荐
4274. suffix expression
[Niuke] convert string to integer
牛客网 实现简单计算器功能
2021-05-20computed and watch applications and differences
零基础自学SQL课程 | HAVING子句
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
Numpy numpy中的np.c_和np.r_详解
Implementation process of tcpdump packet capturing
2138. splitting a string into groups of length k
Data middle office: overview of data governance
CDGA|到底怎么才能做好数据治理呢?
[noi simulation] pendulum (linear algebra, Du Jiao sieve)
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
linux(centos7.9)安装部署mysql-cluster 7.6
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
深入了解 border
Zero foundation self-study SQL course | sub query
PM2 deploy nuxt3 JS project