2017ICPC南宁M The Maximum Unreachable Node Set (偏序集最长反链)
题意:给你一张DAG,让你选取最多的点,使得这些点之间互相不可达。
思路:此问题和最小路径可重复点覆盖等价,先在原图上跑一边传递闭包,然后把每个点拆成两个点i, i + n, 原图中的边(a, b)变成(a, b + n),跑一变网络流, 答案就是n - maxflow;
代码:
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 305;
const int maxm = 100010;
bitset<maxn> b[maxn];
queue<int> q;
int head[maxn], ver[maxm], Next[maxm], edge[maxm], d[maxn];
vector<int> G[maxn];
int n, m, s, t, tot, maxflow;
bool v[maxn];
void dfs(int x) {
if(v[x]) return;
b[x][x] = 1;
for (auto y : G[x]) {
dfs(y);
b[x] |= b[y];
}
v[x] = 1;
return;
}
void add(int x, int y, int z) {
ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
}
bool bfs() {
memset(d, 0, sizeof(d));
while(q.size()) q.pop();
q.push(s);d[s] = 1;
while(q.size()) {
int x= q.front();
q.pop();
for (int i = head[x]; i; i = Next[i]) {
if(edge[i] && !d[ver[i]]) {
q.push(ver[i]);
d[ver[i]] = d[x] + 1;
if(ver[i] == t) return 1;
}
}
}
return 0;
} int dinic(int x, int flow) {
if(x == t) return flow;
int rest = flow, k;
for (int i = head[x]; i && rest; i = Next[i]) {
if(edge[i] && d[ver[i]] == d[x] + 1) {
k = dinic(ver[i], min(rest, edge[i]));
if(!k) d[ver[i]] = 0;
edge[i] -= k;
edge[i ^ 1] += k;
rest -= k;
}
}
return flow - rest;
}
int main() {
int T, x, y;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
tot = 1;
s = n * 2 + 1, t = n * 2 + 2;
for (int i = 1; i <= n; i++)
b[i].reset();
memset(head, 0, sizeof(head));
for (int i = 1; i <= n; i++) {
G[i].clear();
v[i] = 0;
}
maxflow = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
}
for (int i = 1; i <= n; i++) {
if(!v[i]) {
dfs(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if(i == j) continue;
if(b[i][j] == 1) {
add(i, j + n, 1);
}
}
}
for (int i = 1; i <= n; i++) {
add(s, i, 1);
add(i + n, t, 1);
}
int flow = 0;
while(bfs())
while(flow = dinic(s, INF)) maxflow += flow;
printf("%d\n", n - maxflow);
}
return 0;
}
2017ICPC南宁M The Maximum Unreachable Node Set (偏序集最长反链)的更多相关文章
- The Maximum Unreachable Node Set
题目描述 In this problem, we would like to talk about unreachable sets of a directed acyclic graph G = ( ...
- 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】
题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...
- The Maximum Unreachable Node Set 【17南宁区域赛】 【二分匹配】
题目链接 https://nanti.jisuanke.com/t/19979 题意 给出n个点 m 条边 求选出最大的点数使得这个点集之间 任意两点不可达 题目中给的边是有向边 思路 这道题 实际上 ...
- ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)
题目链接:https://nanti.jisuanke.com/t/19979 题意:给出一个 n 个点,m 条边的 DAG,选出最大的子集使得其中结点两两不能到达. 题解:参考自:https://b ...
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...
- 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)
Let SSS be a sequence of integers s1s_{1}s1, s2s_{2}s2, ........., sns_{n}sn Each integer i ...
- 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)
You are given a list of train stations, say from the station 111 to the station 100100100. The passe ...
- 2017ICPC南宁补题
https://www.cnblogs.com/2462478392Lee/p/11650548.html https://www.cnblogs.com/2462478392Lee/p/116501 ...
随机推荐
- uploadify加ASP.NET MVC3.0上传文件(可多条)
页面代码: <div id="fileQueuePlug"></div> <input type="file" name=&quo ...
- Oracle--SQL程序优化案例一
下面是存储过程的一部分程序: PROCEDURE SAP_MAN_ROUTING_SO (CITEM_ID VARCHAR2, C ...
- android开源框架thinkinandroid相关研究
和命令相关的类有: TAICommand:接口文件,一个命令接口所有命令需要从此实现,还有以下几种方法: TACommandExecutor 命令的实现类,其中含有commands这个成员变量.大部分 ...
- Tomcat 的work 目录
1 用tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序(发布到tomcat的项目里含的类,会被编译成.class后才发布过来,源文件没有发布过来,但这里的 ...
- 获取oracle数据库对象定义
在oracle中,使用DBMS_METADATA包中的GET_DDL函数来获得对应对象的定义语句.GET_DDL函数的定义如下: DBMS_METADATA.GET_DDL ( object_type ...
- sqlite时间类型
SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...
- mysql语句练习50题
为了练习sql语句,在网上找了一些题,自己做了一遍,收益颇多.很多地方换一种思路,有更好的写法,欢迎指正. 题目地址:https://blog.csdn.net/fashion2014/article ...
- 裸BFS题若干
1poj 3278 http://poj.org/problem?id=3278 #include<math.h> #include<algorithm> #include&l ...
- python字典小知识
字典的小知识dic = {"name": "tom", "age": 23, "price": 110}# 01:提取键 ...
- 【Android Studio安装部署系列】十三、Android studio添加和删除Module 2
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 新建.导入.删除Module是常见的操作,这里简单介绍下. 新建Module File——New——New Module... 选中 ...