传递闭包的含义指通过传递性推导出尽量多的元素之间的关系,而传递闭包一般都是采用floyd算法。

下面用两道题来实现传递闭包:

Problem 1(POJ3660):

题目链接:http://poj.org/problem?id=3660

题目:

题意:n头牛参加比赛,给你m对关系(譬如给你a和b,那么给的就是a必赢b,当然,b能赢c,那么a也能赢c),问能确定多少头牛的排名。

思路:首先我们用flod算法将所有的关系进行传递,只要u能胜v,那么我们就将d[u][v]设为1,最后如果两者之间有d[u][v]=1或d[v][u]且二者不能同时出现时ans++。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, u, v;
int relationship[][]; int main() {
//FIN;
scanf("%d%d", &n, &m);
memset(relationship, , sizeof(relationship));
for(int i = ; i <= m; i++) {
scanf("%d%d", &u, &v);
relationship[u][v] = ;
}
for(int k = ; k <= n; k++) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(relationship[i][k] && relationship[k][j]) {
relationship[i][j] = ;
}
}
}
}
int ans = , j;
for(int i = ; i <= n; i++) {
for(j = ; j <= n; j++) {
if(i == j) continue;
if(relationship[i][j] == && relationship[j][i] == ) {
break;
}
}
if(j > n) ans++;
}
printf("%d\n", ans);
return ;
}

Problem 2(POJ1094)

题目链接:http://poj.org/problem?id=1094

题目:

题意:给你n个大写字母,m对大小关系,根据他给的关系推测是否有大小矛盾的情况。如果有矛盾的就输出是在第几组关系时矛盾;如果不矛盾,判断只需要前t对组关系就能推测出他们从小到大的排序;如果没有以上两种情况就输入无法确定。

思路:对于每输入一对关系就跑一次floyd判断一遍,如果能推测出他们的关系,那么就跑一边拓扑排序求出他们从小打到的排序情况;如果有矛盾的关系就直接输出是在第几组关系时矛盾;如果没有以上情况就输出无法确定。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, t, flag;
char s[][];
int d[][], in[], num[];
vector<int> G[]; bool floyd() {
for(int k = ; k <= n; k++) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(d[i][k] && d[k][j]) {
d[i][j] = ;
}
}
}
}
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(i == j) continue;
if((d[i][j] && d[j][i]) || (d[i][j] == && d[j][i] == )) {
return false;
}
}
}
return true;
} void topsort(int m) {
t = ;
for(int i = ; i <= n; i++) {
G[i].clear();
}
memset(in, , sizeof(in));
for(int i = ; i <= m; i++) {
int x = s[i][] - 'A' + , y = s[i][] - 'A' + ;
G[x].push_back(y);
in[y]++;
}
queue<int> q;
for(int i = ; i <= n; i++) {
if(in[i] == ) {
q.push(i);
}
}
while(!q.empty()) {
int x = q.front(); q.pop();
num[t++] = x;
for(int i = ; i < G[x].size(); i++) {
int v = G[x][i];
in[v]--;
if(in[v] == ) {
q.push(v);
}
}
}
} int main() {
//FIN;
while(~scanf("%d%d", &n, &m)) {
if(n == && m == ) break;
memset(d, , sizeof(d));
for(int i = ; i <= m; i++) {
scanf("%s", s[i]);
}
flag = ;
for(int i = ; i <= m; i++) {
int x = s[i][] - 'A' + , y = s[i][] - 'A' + ;
d[x][y] = ;
if(floyd()) {
printf("Sorted sequence determined after %d relations: ", i);
topsort(i);
for(int i = ; i < t; i++) {
printf("%c", num[i] - + 'A');
}
printf(".\n");
flag = ;
} else {
for(int j = ; j <= n; j++) {
for(int k = ; k <= n; k++) {
if(j == k) continue;
if((d[j][k] && d[k][j])) {
printf("Inconsistency found after %d relations.\n", i);
flag = ;
break;
}
}
if(flag) break;
}
}
if(flag) break;
}
if(!flag) printf("Sorted sequence cannot be determined.\n");
}
return ;
}

floyd骚操作——传递闭包的更多相关文章

  1. Typescript骚操作,在TS里面直接插入HTML

    Typescript骚操作,在TS里面直接插入HTML,还有语法提示 先给大家看一个图 因为我不喜欢用很重的框架,主要是并非专业UI,但是偶尔会用到,还是觉得直接element组装受不了,想想能在ts ...

  2. 闪电侠 Netty 小册里的骚操作

    前言 即使这是一本小册,但基于"不提笔不读书"的理念,仍然有必要总结一下.此小册对于那些"硬杠 Netty 源码 却不曾在千万级生产环境上使用实操"的用户非常有 ...

  3. awk骚操作

    一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...

  4. 如何在命令长度受限的情况下成功get到webshell(函数参数受限突破、mysql的骚操作)

    0x01 问题提出 还记得上篇文章记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门),我们讲到了一些PHP的一些如何巧妙地绕过数字和字母受限的技巧,今天我要给大家分享的是如 ...

  5. UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)

    题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...

  6. 关于map 及 map 骚操作

    关于map这个东西   很冷门..................   但是,这个博客带你稍微了解一下map:   map用法:一般当作一个下表无穷大的数组   关于它的骚操作:map的鬼畜用法,可以 ...

  7. 通过HTTP的HEADER完成各种骚操作

    作为一名专业的切图工程师,我从来不care网页的header,最多关心Status Code是不是200.但是HEADER真的很重要啊,客户端从服务器端获取内容,首先就是通过HEADER进行各种沟通! ...

  8. 洛谷 P1045 麦森数 (快速幂+高精度+算位数骚操作)

    这道题太精彩了! 我一开始想直接一波暴力算,然后叫上去只有50分,50分超时 然后我改成万位制提高运算效率,还是只有50分 然后我丧心病狂开long long用10的10次方作为一位,也就是100亿进 ...

  9. Mac OS 上的一些骚操作

    本帖记录个人在使用 Mac 操作系统上的一些骚操作,不断更新,以飨读者. 快速移动网页到顶部或底部 用双指上下划触摸板吗?NO,我们有更骚的操作: command + ↑ 回到顶部 command + ...

随机推荐

  1. OSG学习:矩阵变换节点示例

    #include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...

  2. JS 书籍拓展内容

    一.面向对象

  3. ServiceMessage

    <?php class ServiceMessage { private $errorCode = array( '1000' => "系统错误", '1001' =& ...

  4. svmtrain输入参数介绍【转】

    -s svm类型:SVM设置类型(默认0) 0 -- C-SVC 1 --v-SVC 2 – 一类SVM 3 -- e -SVR 4 -- v-SVR -t 核函数类型:核函数设置类型(默认2) 0 ...

  5. PL/SQL在 win8.1系统下连接Oracle11g没有database处理方法(亲身实验,吐血分享)

    一.问题 这里首先说明下我的环境:win8.1(64bit)+oracle11g(64bit)+PL/SQL(32bit).状况是:net manager正常配置,测试也成功,但是用PL/SQL连接的 ...

  6. 【Asp.Net】IIS应用程序池添加ASP.NET v4.0

    可能在安装.NET Framewrok 4.0之前,IIS就已经装好了,结果在IIS的应用程序池中只有.NET 2.0的Classic .NET AppPool和DefaultAppPool.在使用v ...

  7. CSS定义input disabled样式

    disabled 属性规定应该禁用 input 元素.被禁用的 input 元素既不可用,也不可点击.可以设置 disabled 属性,直到满足某些其他的条件为止(比如选择了一个复选框等等).然后,就 ...

  8. hdu 2962 Trucking (最短路径)

    Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 51nod 1286 三段子串(树状数组+拓展kmp)

    题意: 给定一个字符串S,找到另外一个字符串T,T既是S的前缀,也是S的后缀,并且在中间某个地方也出现一次,并且这三次出现不重合.求T最长的长度. 例如:S = "abababababa&q ...

  10. 【转】C# Datatable排序与取前几行数据

    转自:http://www.cnblogs.com/linyechengwei/archive/2010/06/14/1758337.html http://blog.csdn.net/smartsm ...