poj 3660Cow Contest(传递闭包)
来源:互联网 发布:软件开发精品课程 编辑:程序博客网 时间:2024/06/05 03:07
题目给出了m对的相对关系,求有多少个排名是确定的。
使用floyed求一下传递闭包。如果这个点和其余的关系都是确定的,那么这个点的排名就是确定的。
传递闭包:
所谓传递性,可以这样理解:对于一个节点j,如果i能到k,k能到j,那么i就能到j。求传递闭包,就是把图中所有满足这样传递性的节点都弄出来,计算完成后,我们也就知道任意两个节点之间是否相连。
传递闭包的计算过程一般可以用Warshell算法描述:
FOR(k, 1, n){
FOR(i, 1, n){
}
}
}
其中a数组为布尔数组,用来描述两个节点是否相连,可以看做一个无权图的邻接矩阵。可以看到,算法过程跟Floyd很相似,三重循环,枚举每个中间节点。只不过传递闭包只需要求出两个节点是否相连,而不用求其间的最短路径长。
<strong><span style="font-size:18px;">/*********************************************** * Author: fisty * Created Time: 2015/2/16 20:50:01 * File Name : four_H.cpp *floyed算法,传递闭包。如果一个点和其余点的关系都是确定的,则这个的排名是确定的 *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define MAX_N 110int G[MAX_N][MAX_N];int main() { //freopen("in.cpp", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); int n, m; Memset(G, 0); cin >> n >> m; for(int i = 1;i <= m; i++){ int u, v; cin >> u >> v; G[u][v] = 1; } for(int k = 1;k <= n; k++){ for(int j = 1;j <= n; j++){ for(int i = 1;i <= n; i++){ if(G[i][k] && G[k][j]){ G[i][j] = 1; } } } } int ans = 0; int j; for(int i = 1;i <= n ; i++){ for(j = 1;j <= n; j++){ if(i == j) continue; if(!G[i][j] && !G[j][i]) break; //关系不确定 } if(j > n){ //代表i 到(1 ~ n) 这些顶点的关系都已经确定.因为内循环循环了n 次,j 才会 > n ans++; //Debug(i); } } cout << ans << endl; return 0;}</span></strong>
0 0
- POJ-3660 Cow Contest 传递闭包
- POJ 3660 Cow Contest 传递闭包
- POJ 3660 Cow Contest 传递闭包
- 传递闭包-POJ 3660 Cow Contest
- poj Cow Contest(传递闭包)
- POJ 3660 Cow Contest (Floyd 传递闭包)
- POJ 3660 Cow Contest.(Floyd,传递闭包).
- poj 3660 Cow Contest(传递闭包 Floyd)
- poj 3660Cow Contest(传递闭包)
- POJ 3660 Cow Contest(floyd传递关系闭包)
- POJ 3660 Cow Contest (Floyd求传递闭包)
- POJ-3660(Cow Contest)——传递闭包
- POJ - 3660 - Cow Contest (floyd求传递闭包)
- POJ 3660 Cow Contest(传递闭包floyed算法)
- POJ 3660 Cow Contest(传递闭包)
- POJ 3660 Cow Contest(Floyd求传递闭包)
- poj 3660 Cow Contest(传递闭包floyed算法)
- POJ 3660 Cow Contest(传递闭包)
- C++ STL 算法:逆转和旋转
- swing table的使用
- 5 返回桌面 Intent的action 和 category 属性
- Ubuntu 14.04 安装 A卡HD7750 官方闭源 显卡驱动
- NYOJ 78 圈水池( 凸包入门)
- poj 3660Cow Contest(传递闭包)
- LeetCode : Container With Most Water
- UVA-1030-Image Is Everything
- Swing JList使用
- 利用matlab计算Pearson和Spearman相关系数
- Remove Duplicates from Sorted Array
- 西安之行-兵马俑-大雁塔-古城墙
- UVa 10509 - R U Kidding Mr. Feynman?
- Zen of Dev