正题

题目链接:https://www.luogu.com.cn/problem/AT4505


题目大意

给出\(n\)个点和\(n-1\)个点集\(U_i\),每个点集中选择两个点连边使得该图是一棵树。求方案。

\(n\in[1,10^5],\sum_{i=1}^{n-1} |U_i|\in[1,2*10^5]\)


解题思路

冬令营上讲的题目,现在来写。(而且好像我记得课上讲的做法是\(bitset\)的,还是时间久了我记岔了?)

第一眼看上去直觉像是\(hall\)定理但还是不会。

hall定理:\(2*n\)个点的二分图匹配,如果满足任意\(k\)个点都连接了不少于\(k\)个点的话,那么这张图就有完全匹配。

先套一下试试,发现满足条件的图对于它的每个子图\(S\)满足该子图是一个森林。

换句话说对于任意一个\(U\)的集合\(T\),\(G(T)\)表示选出的边连接的节点个数,那么一定有\(G(T)\geq |T|+1\)

回顾一下\(hall\)定理发现是不是很像。

可以先给每个点集选出一个各不同的点(也就是跑一次匹配),如果选不出来那么显然无解。

然后考虑另一个点的选择,从没有被选择的那个点入手,这个点可以选择任何一个包含它的点集连接出去,然后就从下一个点集开始,直到回溯回来选择下一个。如果最后能够遍历所有点就是合法的。

考虑一下正确性,如果它不能遍历所有点那么没有被遍历的点集\(T\)无论怎么连接外面,就一定有一个环不满足\(G(T)\geq |T|+1\)。如果它能遍历所有点,那么我们已经构造出一个方案,显然合法。

时间复杂度\(O(\sum_{i=1}^{n-1}|E|\sqrt n+n)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=4e5+10,inf=1e9;
struct node{
int to,next,w;
}a[N*2];
int n,s,t,tot=1,cnt,ans;
int ls[N],p[N],b[N],dep[N],cur[N];
queue<int> q;bool v[N];
void addl(int x,int y,int w){
a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=0;
return;
}
bool bfs(){
for(int i=1;i<=t;i++)
cur[i]=ls[i],dep[i]=0;
while(!q.empty())q.pop();q.push(s);
dep[s]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(dep[y]||!a[i].w)continue;
dep[y]=dep[x]+1;
if(y==t)return 1;
q.push(y);
}
}
return 0;
}
int dinic(int x,int flow){
if(x==t)return flow;
int rest=0,k;
for(int &i=cur[x];i;i=a[i].next){
int y=a[i].to;
if(dep[x]+1!=dep[y]||!a[i].w)continue;
rest+=(k=dinic(y,min(a[i].w,flow-rest)));
a[i].w-=k;a[i^1].w+=k;
if(rest==flow)return rest;
}
if(!rest)dep[x]=0;
return rest;
}
void dfs(int x){
cnt++;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(v[y])continue;
b[y]=x;v[y]=1;dfs(p[y]);
}
}
int main()
{
scanf("%d",&n);s=2*n;t=s+1;
for(int i=1;i<n;i++){
int m;scanf("%d",&m);
for(int j=1;j<=m;j++){
int x;scanf("%d",&x);
addl(x,i+n,1);
}
}
for(int i=1;i<=n;i++)addl(s,i,1);
for(int i=1;i<n;i++)addl(i+n,t,1);
while(bfs())
ans+=dinic(s,inf);
if(ans<n-1)return puts("-1")&0;
for(int x=n+1;x<s;x++)
for(int i=ls[x];i;i=a[i].next)
if(a[i].w){p[x]=a[i].to;break;}
v[s]=1;
for(int i=ls[s];i;i=a[i].next)
if(a[i].w)dfs(a[i].to);
if(cnt<n)return puts("-1")&0;
for(int x=n+1;x<s;x++)
printf("%d %d\n",p[x],b[x]);
return 0;
}

AT4505-[AGC029F]Construction of a tree【构造题,hall定理,网络流】的更多相关文章

  1. @atcoder - AGC029F@ Construction of a tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N - 1 个 {1, 2, ..., N} 的子集,第 ...

  2. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  3. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  4. AIM Tech Round 4 (Div. 1) C - Upgrading Tree 构造 + 树的重心

    C - Upgrading Tree 我发现我构造题好弱啊啊啊. 很明显能想到先找到重心, 然后我们的目标就是把所有点接到重心的儿子上,让重心的儿子子树变成菊花图, 这个先把重心到儿子的边连到 i , ...

  5. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  6. hdu4671 Backup Plan ——构造题

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...

  7. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  8. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  9. BZOJ 3097: Hash Killer I【构造题,思维题】

    3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 36 ...

随机推荐

  1. 从350ms到80ms,揭秘阿里工程师 iOS 短视频优化方案

    内容作为 App 产品新的促活点,受到了越来越多的重视与投入,短视频则是增加用户粘性.增加用户停留时长的一把利器.短视频的内容与体验直接关系到用户是否愿意长时停留,盒马也提出全链路内容视频化的规划,以 ...

  2. httpClient 下载

    private void button2_Click(object sender, EventArgs e) { get(); } private async Task get() { await D ...

  3. C++11 unique_ptr智能指针详解

    在<C++11 shared_ptr智能指针>的基础上,本节继续讲解 C++11 标准提供的另一种智能指针,即 unique_ptr 智能指针. 作为智能指针的一种,unique_ptr ...

  4. 什么是挂载,Linux挂载详解

    前面讲过,Linux 系统中"一切皆文件",所有文件都放置在以根目录为树根的树形目录结构中.在 Linux 看来,任何硬件设备也都是文件,它们各有自己的一套文件系统(文件目录结构) ...

  5. 【springcloud】springcloud与springboot的版本对应关系

    官方网址:https://start.spring.io/actuator/info 更新时间:2019-12-01 spring-cloud: "Finchley.M2": &q ...

  6. git push&pull命令详解

    git pull的作用是从一个仓库或者本地的分支拉取并且整合代码. git pull [<options>] [<repository> [<refspec>-​] ...

  7. 用宏实现HEX到ASCII ,ASCII 到HEX

    #define HEX2ASCII(value, data)  do{  \            value = (value > 0x09)?(value+0x7):value; \     ...

  8. The Second Week lucklyzpp

    The Second Week  文件通配符模式  在Linux系统中预定义的字符类 1.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录 2.复制/etc目录下 ...

  9. 学习小计: Kaggle Learn Time Series Modeling

    ARIMA模型,参数含义参考:https://www.cnblogs.com/bradleon/p/6827109.html from statsmodels.tsa.arima_model impo ...

  10. linux多次登录失败锁定账户

    2021-07-22 1.配置对系统进行失败的ssh登录尝试后锁定用户帐户 # 配置登录访问的限制 vi /etc/pam.d/system-auth 或者 vi etc/pam.d/password ...