[atAGC029F]Construction of a tree
构造一张二分图,左边是$n$个点,右边是$n-1$个集合,按照点属于集合连边
定义一组匹配的意义,即说明该点的父亲在该集合中选择
利用dinic求出二分图的最大匹配,若不为$n-1$则无解,否则考虑如何去构造一组解:
考虑左边剩下的未参与匹配的点$x$,将其作为根,并将所有含有$x$的集合所匹配的点都作为$x$的儿子,重复此过程(对于其他点,要加上一个“未被选择过”)
事实上,这个过程可以看作从源点出发在残余网络上的一棵bfs树,合法当且仅当能bfs到所有点
(能从$x$走到对应集合当且仅当其匹配的不是该集合,能从一个集合走到$y$当且仅当$y$匹配了该集合,同时$y$一定不会去选择其所匹配的集合中的点,因为一定被其父亲选择完毕)
下面,我们来证明若某一种匹配方式不能做到,则其余都不行:
由于根是任意的,换言之这个点作为根如果不行,其余点也都不行,因此确定了根
考虑调整匹配方案,一定可以通过若干次对一个序列的循环(通过将调整建为一张图可以证明)
若轮换前这一个集合中存在一个点能被搜到,按照轮换的顺序其余点都能被搜到,因此轮换前所有点都不能被搜到,即是一个内部的交换,没有意义
总复杂度为$o(n\sqrt{n})$,可以通过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define oo 0x3f3f3f3f
5 struct ji{
6 int nex,to,len;
7 }edge[N<<3];
8 queue<int>q;
9 pair<int,int>ans[N];
10 int E,n,x,y,tot,head[N<<1],work[N<<1],d[N<<1];
11 void add(int x,int y,int z){
12 edge[E].nex=head[x];
13 edge[E].to=y;
14 edge[E].len=z;
15 head[x]=E++;
16 if (E&1)add(y,x,0);
17 }
18 bool bfs(){
19 memset(d,oo,sizeof(d));
20 d[0]=0;
21 q.push(0);
22 while (!q.empty()){
23 int k=q.front();
24 q.pop();
25 for(int i=head[k];i!=-1;i=edge[i].nex)
26 if ((edge[i].len)&&(d[edge[i].to]==oo)){
27 d[edge[i].to]=d[k]+1;
28 q.push(edge[i].to);
29 }
30 }
31 return d[2*n]<oo;
32 }
33 int dfs(int k,int s){
34 if (k==2*n)return s;
35 for(int &i=work[k];i!=-1;i=edge[i].nex)
36 if ((edge[i].len)&&(d[edge[i].to]==d[k]+1)){
37 int p=dfs(edge[i].to,min(s,edge[i].len));
38 if (p){
39 edge[i].len-=p;
40 edge[i^1].len+=p;
41 return p;
42 }
43 }
44 return 0;
45 }
46 int dinic(){
47 int k,ans=0;
48 while (bfs()){
49 memcpy(work,head,sizeof(work));
50 while (k=dfs(0,oo))ans+=k;
51 }
52 return ans;
53 }
54 void bfs_build(){
55 memset(d,0,sizeof(d));
56 for(int i=head[0];i!=-1;i=edge[i].nex)
57 if (edge[i].len){
58 q.push(edge[i].to);
59 d[edge[i].to]=edge[i].to;
60 }
61 while (!q.empty()){
62 int k=q.front();
63 q.pop();
64 for(int i=head[k];i!=-1;i=edge[i].nex)
65 if ((edge[i].to)&&(edge[i].len)&&(!d[edge[i].to])){
66 if (edge[i].to>n)d[edge[i].to]=d[k];
67 else{
68 tot++;
69 ans[k-n]=make_pair(d[k],edge[i].to);
70 d[edge[i].to]=edge[i].to;
71 }
72 q.push(edge[i].to);
73 }
74 }
75 }
76 int main(){
77 scanf("%d",&n);
78 memset(head,-1,sizeof(head));
79 for(int i=1;i<=n;i++)add(0,i,1);
80 for(int i=1;i<n;i++){
81 scanf("%d",&x);
82 add(i+n,2*n,1);
83 for(int j=1;j<=x;j++){
84 scanf("%d",&y);
85 add(y,i+n,1);
86 }
87 }
88 if (dinic()<n-1){
89 printf("-1");
90 return 0;
91 }
92 bfs_build();
93 if (tot!=n-1)printf("-1");
94 else{
95 for(int i=1;i<n;i++)
96 printf("%d %d\n",ans[i].first,ans[i].second);
97 }
98 }
[atAGC029F]Construction of a tree的更多相关文章
- @atcoder - AGC029F@ Construction of a tree
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N - 1 个 {1, 2, ..., N} 的子集,第 ...
- AT4505-[AGC029F]Construction of a tree【构造题,hall定理,网络流】
正题 题目链接:https://www.luogu.com.cn/problem/AT4505 题目大意 给出\(n\)个点和\(n-1\)个点集\(U_i\),每个点集中选择两个点连边使得该图是一棵 ...
- phylogeny analysis
Multiple Alignment: MUSCLE ProbCons T-Coffee ClustalW Alignment curation: Gblocks Remove positions w ...
- 【AtCoder】AGC029(A-E)
A - Irreversible operation 题解 把每个B后面的W个数累加起来即可 代码 #include <bits/stdc++.h> #define fi first #d ...
- JS进阶 - 浏览器工作原理
一.浏览器的结构 浏览器的主要组件为: 用户界面 - 包括地址栏.前进/后退按钮.书签菜单等.除了浏览器主窗口(显示页面),其他部分都属于用户界面. 浏览器引擎 - 在用户界面和渲染引擎之间传送指令. ...
- Bounding Volume Hierarchy BVH in OpenCASCADE
Bounding Volume Hierarchy BVH in OpenCASCADE eryar@163.com Abstract. Bounding Volume Hierarchy(BVH) ...
- WC2021 题目清单
Day2 上午 <IOI题型与趣题分析> 来源 题目 完成情况 备注 IOI2002 Day1T1 Frog 已完成 IOI2002 Day1T2 Utopia IOI2002 Day1T ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- STL---Codeforces675D Tree Construction(二叉树节点的父亲节点)
Description During the programming classes Vasya was assigned a difficult problem. However, he doesn ...
随机推荐
- bzoj1407,洛谷2421 NOI2002荒岛野人
题目大意: 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,-,M.岛上住着N个野人,一开始依次住在山洞C1,C2,-,CN中,以后每年,第i个野人会沿顺时针向前走P ...
- spyglass DFT
SolvNet spyglass clock_11 内部 generated clocks 在shift mode 不被 testclock 控制. Fix View the Incremental ...
- Golang通脉之包的管理
在工程化的开发项目中,Go语言的源码复用是建立在包(package)基础之上的. 包(package)是多个Go源码的集合,是一种高级的代码复用方案,Go语言提供了很多内置包,如fmt.os.io等. ...
- 【c++ Prime 学习笔记】第11章 关联容器
关联容器的元素按照关键字来保存和访问,而顺序容器的元素是按照在容器中的位置来保存和访问 关联容器支持高效的关键字查找和访问 2种关联容器: map中的元素是关键字-值对(key-value对),关键字 ...
- Gopher们写if err != nil是否腻了?
效果 go里面没有try catch,比较类似的有panic() 和 recover()机制,但是代价太大了,他们的场景更多使用在"程序异常,无法继续往下执行了这种场景",比如配置 ...
- [no code][scrum meeting] Beta 11
$( "#cnblogs_post_body" ).catalog() 例会时间:5月26日11:30,主持者:肖思炀 下次例会时间:5月27日11:30,主持者:乔玺华 一.工作 ...
- 【二食堂】Alpha - Scrum Meeting 10
Scrum Meeting 10 例会时间:4.20 18:00~18:20 进度情况 组员 昨日进度 今日任务 李健 1. 与柴博合作完成登录注册页面issue 继续完成登录注册页面issue 柴博 ...
- Mac 系统如何利用软链接在根目录创建文件夹?
作者:泥瓦匠 出处:https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html Mac 操作系统 ...
- (六)、Docker 之 Dockerfile
1.什么是Dockerfile Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本. 2.Dockerfile解析过程 前提认知: 每条保留字指令都必须为大写字母 ...
- xUtils3的使用教程
首先在build.gradle下的dependencies下添加引用. implementation 'org.xutils:xutils:3.3.36' 然后创建一个表实体. package com ...