@atcoder - AGC029F@ Construction of a tree
@description@
给定 N - 1 个 {1, 2, ..., N} 的子集,第 i 个为 Ei。
请构造 N - 1 条边 (u1, v1), (u2, v2), ... 使得 ui ∈ Ei 且 vi ∈ Ei,满足这 N - 1 条边构成一棵树。
@solution@
好神的题。
构造一个二分图,第 i 条边对应的点连向 Ei 中所有的点。
有解的一个充分条件是大小为 k 的边集合能够连到点集合大小 > k(否则肯定会连成环)。
注意到这个和 hall 定理挺像的。记 N(S) 表示 S 的邻集,hall 定理描述的是 |S| <= |N(S)| 等价于二分图有完美匹配,而上述充分条件为 |S| < |N(S)|。
如何把它和 hall 定理联系起来呢?如果我们把 N 个点任意去掉一个点,那么应该有 |S| <= |N(S)|。
也就是 N 个点每一个点都不是二分图的必需点,这样就能推出 |S| < |N(S)| 的结论。
跑个 dinic,左边右点。此时如果能从点 i 到达汇点 t,则 i 不是必需点。
也就是以 t 为根沿逆向边建一棵可到达 t 的生成树,如果 N 个点都在树上,则合法。
注意此时这棵生成树就是我们想要构造的树。可以发现 N 个点都在树上时 N-1 条边对应的点也在树上,而 N-1 条边一定只会和两条树边连通,就是这条边对应的两个端点。
@accepted code@
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100000;
namespace FlowGraph{
const int MAXV = 2*MAXN;
const int MAXE = 10*MAXN;
const int INF = (1 << 30);
struct edge{
int to, flow, cap;
edge *nxt, *rev;
}edges[MAXE + 5], *adj[MAXV + 5], *cur[MAXV + 5], *ecnt = edges;
void addedge(int u, int v, int c) {
edge *p = (++ecnt), *q = (++ecnt);
p->to = v, p->cap = c, p->flow = 0;
p->nxt = adj[u], adj[u] = p;
q->to = u, q->cap = 0, q->flow = 0;
q->nxt = adj[v], adj[v] = q;
p->rev = q, q->rev = p;
}
int s, t;
int fa[MAXV + 5], dis[MAXV + 5];
int que[MAXV + 5], hd, tl;
bool relabel() {
for(int i=s;i<=t;i++)
dis[i] = t + 5, cur[i] = adj[i];
dis[que[hd = tl = 1] = t] = 0;
while( hd <= tl ) {
int x = que[hd++];
for(edge *p=adj[x];p;p=p->nxt) {
if( dis[p->to] > dis[x] + 1 && p->rev->cap > p->rev->flow )
dis[p->to] = dis[x] + 1, fa[p->to] = x, que[++tl] = p->to;
}
}
return !(dis[s] == t + 5);
}
int aug(int x, int tot) {
if( x == t ) return tot;
int sum = 0;
for(edge *&p=cur[x];p;p=p->nxt) {
if( p->cap > p->flow && dis[p->to] + 1 == dis[x] ) {
int del = aug(p->to, min(tot - sum, p->cap - p->flow));
p->flow += del, p->rev->flow -= del, sum += del;
if( sum == tot ) break;
}
}
return sum;
}
int max_flow(int _s, int _t) {
int flow = 0; s = _s, t = _t;
while( relabel() )
flow += aug(s, INF);
return flow;
}
}
int a[MAXN + 5], b[MAXN + 5];
int main() {
int N; scanf("%d", &N);
for(int i=1;i<N;i++) {
int c; scanf("%d", &c);
for(int j=1;j<=c;j++) {
int w; scanf("%d", &w);
FlowGraph::addedge(N + i, w, 1);
}
}
int s = 0, t = N + N - 1 + 1;
for(int i=1;i<N;i++) FlowGraph::addedge(s, N + i, 1);
for(int i=1;i<=N;i++) FlowGraph::addedge(i, t, 1);
int f = FlowGraph::max_flow(s, t);
if( f == N - 1 ) {
FlowGraph::relabel();
for(int i=1;i<t;i++)
if( FlowGraph::dis[i] == t + 5 ) {
puts("-1");
return 0;
}
for(int i=1;i<N;i++) a[i] = FlowGraph::fa[N + i];
for(int i=1;i<=N;i++) b[FlowGraph::fa[i] - N] = i;
for(int i=1;i<N;i++) printf("%d %d\n", a[i], b[i]);
}
else puts("-1");
}
@details@
我好菜啊。
这种判定性 + 构造性问题以后又有一种新的思路了:是否所有子集满足一定条件就合法。
@atcoder - AGC029F@ Construction of a tree的更多相关文章
- 【AtCoder AGC023F】01 on Tree(贪心)
Description 给定一颗 \(n\) 个结点的树,每个点有一个点权 \(v\).点权只可能为 \(0\) 或 \(1\). 现有一个空数列,每次可以向数列尾部添加一个点 \(i\) 的点权 \ ...
- AtCoder 2376 Black and White Tree
D - Black and White Tree Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Stateme ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...
- Atcoder D - Black and White Tree(树dp+博弈)
题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_d 题意:有一棵树先手涂白色,后手涂黑色,直到不能再涂为止.涂完后再把所有黑色直接相邻的白色都变成 ...
- AtCoder AGC014E Blue and Red Tree (启发式合并)
题目链接 https://atcoder.jp/contests/agc014/tasks/agc014_e 题解 完了考场上树剖做法都没想到是不是可以退役了... 首先有一个巨难写的据说是\(O(n ...
- [AtCoder Regular Contest 083] Bichrome Tree
树形DP. 每个点有两个属性:黑色点的权值和,白色点权值和,一个知道另一个也一定知道. 因为只要子树的和它相等的点得权值和不超过x[u],u点的权值总能将其补齐. 设计状态f[u]表示以u为根的子树, ...
- AT4505-[AGC029F]Construction of a tree【构造题,hall定理,网络流】
正题 题目链接:https://www.luogu.com.cn/problem/AT4505 题目大意 给出\(n\)个点和\(n-1\)个点集\(U_i\),每个点集中选择两个点连边使得该图是一棵 ...
随机推荐
- python3.x 基础三:文件IO
打开文件的两种方式 1.直接打开文件并赋值给变量,打开后得到操作句柄,但不会自动关闭 file = open('文件名‘,'打开模式',’编码‘) fd = open('../config/file1 ...
- POJ1733
题目链接:https://vjudge.net/problem/POJ-1733 解题思路:并查集+离散化 AC代码: #include <iostream> #include <c ...
- Python 图像处理 OpenCV (5):图像的几何变换
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- WordPress 获取文章内容页特色图像地址
WordPress获取特色图像地址主要需要用到两个函数get_post_thumbnail_id和wp_get_attachment_image_src.下面是分别获取小.中.大.完整.指定图片规格的 ...
- 关于VMware问题:无法获得 VMCI 驱动程序的版本: 句柄无效。驱动程序“vmci.sys”的版本不正确
有的童鞋可能安装虚拟机时出现了下边这样的错误,莫慌,下面咋们来解决!!! 1.首先,找到你的安装虚拟机的目录下有一个.vmx的文件 找到这个文件,用编辑器打开,将该值改为FALSE即可!
- 使用 IdentityService4 集成小程序登录一种尝试
1 场景介绍 主要业务是通过 App 承载,在 App 中可以注册和登录,为了更好的发展业务引入了微信小程序,于是如何让这两个入口的用户互通便成了需要解决的问题. 看了一下其它 App 大致地思路是两 ...
- PAT 1033 To Fill or Not to Fill (25分) 贪心思想
题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...
- Rocket - interrupts - Crossing
https://mp.weixin.qq.com/s/nSX4prXFb4K5GSUhPtOUCg 简单介绍Crossing的实现. 1. IntXing 这是一个LazyModule: 1) 参数 ...
- Chisel3 - Tutorial - VendingMachineSwitch
https://mp.weixin.qq.com/s/5lcMkenM2zTy-pYOXfRjyA 演示如何使用switch/is来实现状态机. 参考链接: https://github.co ...
- Java实现蓝桥杯第八届决赛 对局匹配
标题:对局匹配 小明喜欢在一个围棋网站上找别人在线对弈.这个网站上所有注册用户都有一个积分,代表他的围棋水平. 小明发现网站的自动对局系统在匹配对手时,只会将积分差恰好是K的两名用户匹配在一起.如果两 ...