poj 3275 Ranking the Cows
来源:互联网 发布:自助友情链接源码 编辑:程序博客网 时间:2024/06/17 14:07
这个题目其实挺巧妙的。
最坏情况下要再加几次询问才能确定每头牛的rank。
首先我们考虑最坏情况下要多少条有向边才能确定一个图的顶点的排序。答案是n*(n-1)/2.
这个简单说明一下,首先找一个点向所有其他的点连接一条边,这样仅仅确定了一个顶点是最高等级,去掉该点。然后重复该过程。到确定所有顶点排位的时候就用了上述的边数。
那么现在就很容易了。依次确定每个顶点可以访问到的顶点数目,再累加起来得到sum。这个其实就是现在已经有的边数。再用上述边数减去sum就是答案。
#include <iostream>
#include <cstdio>#include <cstring>
using namespace std;
const int maxn=1e3+9,maxm=1e4+9;
int n,m;
int head[maxn],lon;
struct
{
int to,next;
}e[maxm];
void edgemake(int from,int to)
{
e[++lon].to=to;
e[lon].next=head[from];
head[from]=lon;
}
void edgeini()
{
memset(head,-1,sizeof(head));
lon=-1;
}
int flag[maxn];
int dfs(int t)
{
flag[t]=1;
int ret=1;
for(int k=head[t];k!=-1;k=e[k].next)
{
int u=e[k].to;
if(!flag[u])
ret+=dfs(u);
}
return(ret);
}
int main()
{
scanf("%d %d",&n,&m);
edgeini();
for(int i=1,from,to;i<=m;i++)
{
scanf("%d %d",&from,&to);
edgemake(from,to);
}
int ans=0;
for(int i=1;i<=n;i++)
{
memset(flag,0,sizeof(flag));
ans+=dfs(i)-1;
}
printf("%d\n",n*(n-1)/2-ans);
return 0;
}
- POJ 3275 Ranking The Cows
- poj 3275 Ranking the Cows
- Poj 3275 Ranking the Cows
- poj 3275 Ranking The Cows
- poj 3275 Ranking the Cows 图+BFS
- poj 3275 Ranking the Cows floyd算法
- poj 3275 Ranking the Cows(floyd …
- poj 3275 Ranking the Cows 传递闭包+邻接表优化
- POJ 3275 Ranking the Cows floyd求传递闭包 || bfs
- PKU 3275 Ranking the Cows 最短路 floyd
- bzoj1703[Usaco2007 Mar]Ranking the Cows 奶牛排名
- 【bzoj1703】[Usaco2007 Mar]Ranking the Cows 奶牛排名
- Treats for the Cows POJ
- BZOJ 1703 [Usaco2007 Mar]Ranking the Cows 奶牛排名 bitset优化
- 传递闭包——洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows
- BZOJ 1703 Ranking the Cows 奶牛排名 (传递闭包)
- BZOJ 1703 [Usaco2007 Mar]Ranking the Cows 奶牛排名 Floyd+bitset
- POJ 2387 Til the Cows Come Home
- HDOJ 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(完全背包)
- c/c++中const用法总结
- Memory Notification: Library Cache Object loaded into SGA
- 线程关闭socket的方法
- Sleep和wait的区别
- poj 3275 Ranking the Cows
- GDB的使用方法(推荐)
- linux之限制IP登录SSH服务
- 关于OpenStack的一些看法
- 心情随笔
- oracle中关于ANL,SOME,ALL解析
- IOS第六天——Obj-C的内存管理初步认识
- 用Maven管理Android的pom.xml配置
- 前端 CSS 规范大全