传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了。。

传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you. 
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure. 
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point. 
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars. 
As an ICPCer, who has excellent programming skill, can your help EUC?

Input

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output

For each test of the input, print a line containing the least robots needed.

Sample Input

1 0
2 1
1 2
2 0
0 0

Sample Output

1
1
2
题意:放最小的机器人使得能够访问到所有顶点,机器人不能后退就是指有向图
题解:刚开始居然以为用拓扑能做,后来发现蠢了,还是DAG的最小顶点覆盖问题,加一个传递闭包就行了
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int n,color[N];
bool used[N],ok[N][N]; bool match(int x)
{
for(int i=;i<=n;i++)
{
if(!used[i]&&ok[x][i])
{
used[i]=;
if(color[i]==-||match(color[i]))
{
color[i]=x;
return ;
}
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int m;
while(cin>>n>>m,n||m){
memset(ok,,sizeof ok);
while(m--){
int a,b;
cin>>a>>b;
ok[a][b]=;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(!ok[i][j])
{
for(int k=;k<=n;k++)
{
if(ok[i][k]&&ok[k][j])
ok[i][j]=;
}
}
}
}
int ans=;
memset(color,-,sizeof color);
for(int i=;i<=n;i++)
{
memset(used,,sizeof used);
ans+=match(i);
}
cout<<n-ans<<endl;
}
return ;
}

poj2594最小顶点覆盖+传递闭包的更多相关文章

  1. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

  2. BZOJ 3140 消毒(最小顶点覆盖)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...

  3. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  6. hdu1054(最小顶点覆盖)

    传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...

  7. hdu 1150 Machine Schedule(最小顶点覆盖)

    pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  8. hdu1151有向图的最小顶点覆盖

    有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...

  9. hdu1054最小顶点覆盖

    最小定点覆盖是指这样一种情况: 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点.我们称集合V覆盖了G的边.最小顶点覆盖是用最少的顶点来覆盖所有的边.顶点覆盖数是最小顶点覆盖 ...

随机推荐

  1. ###服务(Service)

    Start服务开启方式 1)   创建服务 public class MyService extends Service 2)   添加注册表 <service android:name=&qu ...

  2. 使用shape来定义控件的一些显示属性

    Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结 先看下面的代码: <shape> <!-- 实心 -- ...

  3. 1.WP8.1开发_去除闪动效果,直接进入首页

    有时候希望打开软件的时候,不出现闪屏,而直接进入到第一个页面. 把第一个页面作为闪屏,可以制作一些进度条  和动画等... 很简单,有两种方法去除闪屏. 1.删除项目下 Assets 文件夹里面的Sp ...

  4. Java常量笔记

    在添加文件名的同时,文件名和内容可以不相同!! 1·Java 常量 常量就是固定不变的量,一旦被定义,它的值就不能被改变. 例实: 书中的代码不全,在这里不补充一下: 书中的源代码: public c ...

  5. jquery data属性的使用

    var func=function(){console.log("test")};$("div").data("test",func);$( ...

  6. C#.Net面试题

    点这里,有很多篇<C#..Net经典面试题02> 在线阅读本文:http://3y.uu456.com/bp_5dcve363vi7px008u2lt_1.html C#..Net经典面试 ...

  7. Java面向对象核心技能

    1.封装 封装是面向对象的三大特性之一,就是将类的状态信息隐藏在类内部,不允许外部程序直接访问,而通过该类提供的方法来实现对隐藏信息的操作和访问. 封装的好处:隐藏类的实现细节:让使用者只能通过程序规 ...

  8. Arduino String.h库函数详解

    此库中包含 1 charAT() 2 compareTo() 3 concat() 4 endsWith() 5 equals() 6 equalslgnoreCase() 7 getBytes() ...

  9. 【PAT_Basic日记】1002. 写出这个数

    #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { void print ...

  10. 串口屏与触摸屏人机界面组态软件HMIMaker介绍

    串口屏与触摸屏人机界面组态软件HMIMaker介绍 触摸屏人机界面组态软件HMIMaker,是一款基于ARM架构的嵌入式控制系统开发的嵌入式软件,专业应用于触摸屏的二级界面开发,具有单片机协议,mod ...