Codeforces.566E.Restoring Map(构造)
\(Description\)
对于一棵树,定义某个点的邻居集合为所有距离它不超过\(2\)的点的集合(包括它自己)。
给定\(n\)及\(n\)个点的邻居集合,要求构造一棵\(n\)个点的树,使得每个给定的集合都对应一个点。输入保证有解。
\(n\leq1000\)。
\(Solution\)
如果两个点的邻居集合大小为\(2\),那么交集中的两个点之间一定有边。这样我们就可以\(O(\frac{n^3}{w})\)确定出非叶节点以及它们之间的连边。
然后考虑叶节点应该挂到哪里。如果一个叶节点的邻居集合,和距离某个非叶节点不超过\(1\)的点的集合相同,那么这两个点之间有边。对于叶子\(x\),所有包含\(x\)的邻居集合中最小的一定就是\(x\)的邻居集合。一个点数\(\geq3\)的树,离某个点距离不超过\(1\)的点的集合是互不相同的。
需要特判非叶节点只有一个和两个的情况。
官方题解是,找出叶子\(x\)的邻居集合,如果除去\(x\)集合大小\(\geq3\),那么在集合内度数\(>1\)的点就是与\(x\)相邻的。否则集合大小是\(2\),这种情况有些难判,但是与\(x\)相邻的点一定只与一个非叶节点相连。所以我们只需要特判\(x-a-b-...\)这种情况。
还有种并查集的写法,太傻逼了看不懂了QAQ。
//296ms 100KB
#include <cstdio>
#include <cctype>
#include <bitset>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=1003;
std::bitset<N> st[N],nle,adj[N];
bool not_leaf[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
int main()
{
int n=read();
for(int i=1; i<=n; ++i)
for(int T=read(); T--; st[i][read()]=1);
int m=0;
for(int i=1; i<=n; ++i)
for(int j=i+1; j<=n; ++j)
if((st[i]&st[j]).count()==2)
{
std::bitset<N> tmp=st[i]&st[j];
int s=0,t=0;
for(int k=1; k<=n&&!t; ++k) if(tmp[k]==1) s?t=k:s=k;//可以用_Find_next。但是暴力复杂度也是对的,暴力好咯。
if(!adj[s][t])
++m, printf("%d %d\n",s,t), nle[s]=nle[t]=1,
adj[s][s]=adj[s][t]=1, adj[t][t]=adj[t][s]=1, not_leaf[s]=not_leaf[t]=1;
}
if(!m)
{
for(int i=2; i<=n; ++i) printf("%d %d\n",1,i);
return 0;
}
if(m==1)
{
int s=0,t=0,a=0,b=0;
for(int i=1; i<=n&&!t; ++i) if(not_leaf[i]) s?t=i:s=i;
for(int i=1; i<=n; ++i)
if(st[i].count()!=n)
{
for(int j=1; j<=n; ++j) !not_leaf[j]&&printf("%d %d\n",j,st[i][j]?s:t);
break;
}
return 0;
}
for(int i=1; i<=n; ++i)
if(!not_leaf[i])
{
int mn=N,p=0;
for(int j=1; j<=n; ++j) if(st[j][i]&&st[j].count()<mn) mn=st[j].count(), p=j;
std::bitset<N> tmp=st[p]≰
for(int j=1; j<=n; ++j)
if(tmp[j]&&tmp==adj[j])
{printf("%d %d\n",i,j); break;}
}
return 0;
}
Codeforces.566E.Restoring Map(构造)的更多相关文章
- Codeforces 566E - Restoring Map(bitset 优化构造)
Codeforces 题目传送门 & 洛谷题目传送门 本来说好的不做,结果今早又忍不住开了道题/qiao 我们称度为 \(1\) 的点为叶节点,度大于 \(1\) 的点为非叶节点. 首先考虑如 ...
- @codefoces - 566E@ Restoring Map
目录 @description@ @solution@ @accepted code@ @details@ @description@ 对于一棵 n 个点的树,我们称两个点是相邻的当且仅当两个点的距离 ...
- codeforces 651C(map、去重)
题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- [ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造
题意是让你用1到n的数构造24 看完题解感觉被样例骗了…… 很明显 n<4肯定不行 然后构造出来4 5的组成24的式子 把大于4(偶数)或者5(奇数)的数构造成i-(i-1)=1 之后就是无尽的 ...
- Codeforces 947F. Public Service 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF947F.html 近5K码量构造题,CF血腥残暴! 题解 这里先定义 $FT(k)$ 表示一个菊花树多 k ...
- Karen and Game CodeForces - 816C (暴力+构造)
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...
- CodeForces 404C Restore Graph (构造)
题意:让人构造一个图,满足每个结点边的数目不超过 k,然后给出每个结点到某个结点的最短距离. 析:很容易看出来如果可能的话,树是一定满足条件的,只要从头开始构造这棵树就好,中途超了int...找了好久 ...
- Vasya And The Matrix CodeForces - 1016D (思维+构造)
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the ma ...
随机推荐
- Android Studio 打开activity_main.xml不能正常显示
操作系统:Windows 10 x64 IDE:Android Studio 3.2.1 解决方法:http://www.jcodecraeer.com/a/anzhuokaifa/Android_S ...
- 修改jenkins发布账号信息
- 腾讯云Ubuntu安装可视化桌面
1.安装图形界面 sudo apt-get update 更新 1).sudo apt-get install xinit 2).sudo apt-get install gdm ( 登陆窗口,用于 ...
- PEM routines:PEM_read_bio:no start line
https://blog.csdn.net/xiejunna/article/details/71151006 在放置证书后,运行nodejs抛异常:PEM routines:PEM_read_bio ...
- 不显示TensorFlow加速指令警告
vim ~/.bashrc 在打开的文件中追加: export TF_CPP_MIN_LOG_LEVEL= 最后再执行 source ~/.bashrc
- org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083.
1.启动hive的过程中,[hadoop@slaver1 soft]$ hive --service metastore &错误如下所示: 原因:之前启动hive失败了,但是进程以及启动起来, ...
- Multidex(二)之Dex预加载优化
Multidex(二)之Dex预加载优化 https://www.jianshu.com/p/2891599511ff
- es6 let和const
一.let 1.let块作用域 if(true){ var a=1; let b=2; } console.log("a:"+a);//a:1 console.log(" ...
- .net core支持的操作系统版本
https://github.com/dotnet/core/blob/master/os-lifecycle-policy.md
- bzoj2018年5月赛
题解: 老早之前看的并没有写题解.. t1: 我刚开始想的是线段树来维护.. 看了题解发现直接二分就行了 很容易发现因数只会有30个 那么我们就统计每一种因数在这段区间的个数 然后开个vector记录 ...