poj2594
特殊的最小路径覆盖
回顾一下经典的最小路径覆盖问题是每个点都恰好被一条路径覆盖
我们把有向无环图的点拆成i,i',对于原图中边i--->j,连边i-->j'
做最大匹配,答案是原图点数-最大匹配
但这道题每个点可以被覆盖多次,所以我们考虑先dfs出每个点可以访问的点
然后拆点,如果i可以到达j,那么连边i-->j',答案是原图点数-最大匹配
为什么呢,感性的想一下,在最小路径覆盖的基础上既然每个点可以被多次经过
那么干脆我们把两个可达的点认为是直接飞过去就好了~ ~
type node=record
point,next:longint;
end; var edge:array[..] of node;
a:array[..,..] of boolean;
p,cx,cy:array[..] of longint;
v:array[..] of boolean;
x,y,n,m,i,j,ans,len:longint; procedure add(x,y:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].next:=p[x];
p[x]:=len;
end; function find(x:longint):longint;
var i,y:longint;
begin
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if not v[y] then
begin
v[y]:=true;
if (cy[y]=-) or (find(cy[y])=) then
begin
cx[x]:=y;
cy[y]:=x;
exit();
end;
end;
i:=edge[i].next;
end;
exit();
end; procedure dfs(x:longint);
var i,y:longint;
begin
i:=p[x];
v[x]:=true;
while i<>- do
begin
y:=edge[i].point;
if not v[y] then dfs(y);
i:=edge[i].next;
end;
end; begin
readln(n,m);
while (n<>) do
begin
ans:=;
len:=;
fillchar(p,sizeof(p),);
fillchar(a,sizeof(a),false);
for i:= to m do
begin
readln(x,y);
add(x,y);
a[x,y]:=true;
end;
for i:= to n do
begin
fillchar(v,sizeof(v),false);
dfs(i);
for j:= to n do
if v[j] and (i<>j) and not a[i,j] then
add(i,j);
end;
fillchar(cx,sizeof(cx),);
fillchar(cy,sizeof(cy),);
for i:= to n do
if cx[i]=- then
begin
fillchar(v,sizeof(v),false);
ans:=ans+find(i);
end;
writeln(n-ans);
readln(n,m);
end;
end.
poj2594的更多相关文章
- hdu1151+poj2594(最小路径覆盖)
传送门:hdu1151 Air Raid 题意:在一个城镇,有m个路口,和n条路,这些路都是单向的,而且路不会形成环,现在要弄一些伞兵去巡查这个城镇,伞兵只能沿着路的方向走,问最少需要多少伞兵才能把所 ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- POJ2594 Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8193 Accepted: 3358 Description Have ...
- poj2594 (最小路径覆盖 + floyd)
题目链接 http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...
- poj2594最小顶点覆盖+传递闭包
传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...
- poj2594 机器人寻找宝藏(最小路径覆盖)
题目来源:http://poj.org/problem?id=2594 参考博客:http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641. ...
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- POJ-2594
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7035 Accepted: 2 ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
随机推荐
- [转] npm命令概述
PS:问题,nvm找不到正确的下载server NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist nvm ls-remote NVM_NODEJS_ORG_MI ...
- plsql 显式游标
显式游标的处理过程包括: 声明游标,打开游标,检索游标,关闭游标. 声明游标 CURSOR c_cursor_name IS statement; 游标相当于一个查询结果集,将查询的结果放在游标里,方 ...
- kissy
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
- 表达式:使用API创建表达式树(5)
一.ConditionalExpression:表达式 生成如 IIF((a == b), "a和b相等", "a与b不相等") 式子. 使用: Paramet ...
- 利用bat批量执行脚本文件
1.读取目录文件 利用bat 的for命令读取中的sql文件 for /r %%c in (0*.sql) do echo %%c %%c 相当于变量 in() 中的为循环的范围 此句的作用是显示当前 ...
- postgresql jsonb类型查询
select * from (select * from ud_order where user_id=10 and status=2unionselect * from ud_order where ...
- 关于“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 ”
原因:在从远程服务器复制数据到本地时出现“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatas ...
- [转]Web UI 设计命名规范
来源:http://blog.bingo929.com/web-ui-design-name-convention.html 一.网站设计及基本框架结构: 1. Container “conta ...
- iOS中JavaScript和OC交互
转载自:http://www.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html 还可参考的文章:http://blog.c ...