poj3660(Cow Contest)解题报告
Solution:
传递闭包
//if a beats b and b beats c , then a beats c
//to cow i, if all the result of content(n-1) has been known,
//then the rank can be determined
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define maxn 1000 //if a beats b and b beats c , then a beats c int main()
{
long n,m,i,j,k,a,b,ans,total=;
bool vis[maxn][maxn];
scanf("%ld%ld",&n,&m);
for (i=;i<=n;i++)
for (j=;j<=n;j++)
vis[i][j]=false;
for (i=;i<=m;i++)
{
scanf("%ld%ld",&a,&b);
vis[a][b]=true;
}
for (i=;i<=n;i++)
for (j=;j<=n;j++)
for (k=;k<=n;k++)
vis[j][k]=vis[j][k] || (vis[j][i] && vis[i][k]);
//to cow i, if all the result of content(n-1) has been known,
//then the rank can be determined
for (i=;i<=n;i++)
{
ans=;
for (j=;j<=n;j++)
if (vis[i][j] || vis[j][i])
ans++;
if (ans==n-)
total++;
}
printf("%ld\n",total);
return ;
}
传递闭包标程+解释:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define maxn 1000 //n个点,点编号为1~n,m条边
//传递闭包:判断图中任意两点是否可达
//时间复杂度:n*n*n int main()
{
long n,m,i,j,k,a,b;
bool vis[maxn][maxn];
scanf("%ld%ld",&n,&m);
for (i=;i<=n;i++)
for (j=;j<=n;j++)
vis[i][j]=false;
for (i=;i<=m;i++)
{
scanf("%ld%ld",&a,&b);
vis[a][b]=true;
}
//如果是无向图,a能到达b意味着b也能到达a,所以可以求a->b(a<b),i<j<k
//如果没有if:操作n*n*n
//如果有if:判断n*n+n*(n-1)*n次,操作n*(n-1)*(n-2)
//所以直接不用if
//从j点到k点:从j到k经过的点的编号小于等于i(i=1,2,…,n)。当然也可以从j直达到k。
for (i=;i<=n;i++)
for (j=;j<=n;j++)
//if (i!=j)
for (k=;k<=n;k++)
//if (i!=j && i!=k)
vis[j][k]=vis[j][k] || (vis[j][i] && vis[i][k]);
//if (vis[j][i] && vis[i][k])
//vis[j][k]=true;
for (i=;i<=n;i++)
{
printf("%ld : ",i);
for (j=;j<=n;j++)
if (vis[i][j])
printf("%ld ",j);
printf("\n");
}
return ;
}
/*
5 5
1 2
2 3
1 3
4 5
5 3
*/
其实传递闭包跟floyd很像,原理都是一样的。
poj3660(Cow Contest)解题报告的更多相关文章
- POJ3660——Cow Contest(Floyd+传递闭包)
Cow Contest DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...
- POJ-3660.Cow Contest(有向图的传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17797 Accepted: 9893 De ...
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- POJ3660 Cow Contest —— Floyd 传递闭包
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ-3660 Cow Contest( 最短路 )
题目链接:http://poj.org/problem?id=3660 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, ar ...
- POJ3660 Cow Contest floyd传递闭包
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
- POJ3660 Cow Contest【最短路-floyd】
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we ...
- poj3660 Cow Contest(Floyd-Warshall方法求有向图的传递闭包)
poj3660 题意: 有n头牛, 给你m对关系(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少牛的排名. 分析: 在这呢先说一下关系闭包: 关系闭包有三种: 自反闭包(r), 对 ...
随机推荐
- Linux运维笔记-日常操作命令总结(3)
文本操作:sed sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作. sed命令行格式为: sed [-nefri] ‘c ...
- springboot整合curator实现分布式锁
理论篇: Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处 ...
- NSCache的简单使用
简介 1)NSCache 是苹果官方提供的缓存类,用法与 NSMutableDictionary 的用法很相似,在 AFNetworking 和 SDWebImage 中,使用它来管理缓存. 2)NS ...
- Buy the Ticket HDU 1133
传送门 [http://acm.hdu.edu.cn/showproblem.php?pid=1133] 题目描述和分析 代码 #include<iostream> #include< ...
- article元素以及section
<p>发表日期:<time pubdate="pubdate">2015/10/30</time></p> article元素有自己 ...
- Python学习笔记(三)——条件语句、循环语句
注:需注意代码的缩进格式 注:需注意代码的缩进格式 注:需注意代码的缩进格式 Python 与其他语言最大的区别就是,Python 的代码块不使用大括号 {} 来控制类,函数以及其他逻辑判断.pyth ...
- ModSecurity is an open source, cross-platform web application firewall (WAF) module.
http://www.modsecurity.org/ ModSecurity is an open source, cross-platform web application firewall ( ...
- JS判断浏览器种类
function myBrowser() { var userAgent = navigator.userAgent; //取得浏览器的userAgent ...
- vue的使用1
Vue.$set(object, key, value); <!-- Alt + C --> <input @keyup.alt.="clear"> < ...
- Docker(二十)-Docker容器CPU、memory资源限制
背景 在使用 docker 运行容器时,默认的情况下,docker没有对容器进行硬件资源的限制,当一台主机上运行几百个容器,这些容器虽然互相隔离,但是底层却使用着相同的 CPU.内存和磁盘资源.如果不 ...