传递闭包的含义指通过传递性推导出尽量多的元素之间的关系,而传递闭包一般都是采用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. 团队作业7——第二次项目冲刺(Beta版本)-第二篇

    1.工作分工: 团队成员 分工 郭达22120 项目整合,后台代码 刘德培44060 数据库模块 石浩洋22061 前台界面优化 曾繁钦22056 前台界面优化.测试 孙斌22030 后台代码 2.燃 ...

  2. 【week2】结对编程-四则运算 及感想

    首先我要说一下,我得作业我尽力了,但是能力有限,还需练习. 四则运算,改进代码流程: 1.手动输入算式(属于中缀表达式) 2.将中缀表达式转化成后缀表达式 生成out数组 3.一个操作数栈,一个运算符 ...

  3. 【Redis】- 总结精讲

    本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...

  4. phpcms V9如何判断用户是否登录以及登陆后的标签写法问题

    首先要获取userid {php$userid=param::get_cookie('_userid');}​ 然后再判断是否为空 {if $userid}...这里写已经登录之后的代码...{els ...

  5. jquery计算器(改良版)

    代码: <!Doctype html> <html> <meta charset="UTF-8"> <title>计算器</t ...

  6. [OS] 操作系统基本类型

    ·批处理系统:(用户脱机使用.成批处理.多道程序运行) 批处理系统,又名批处理操作系统.批处理是指用户将一批作业提交给操作系统后就不再干预,由操作系统控制它们自动运行.这种采用批量处理作业技术的操作系 ...

  7. <hx>标签 字体自动加粗 自动换行

    <hx>标签 字体自动加粗 自动换行

  8. CF549H:Degenerate Matrix ——题解

    https://vjudge.net/problem/CodeForces-549H ———————————————————————— 题目大意:给一个矩阵,每个数可以加任意的数使得该矩阵为退化矩阵( ...

  9. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...

  10. BZOJ1599 find the mincost route 【floyd】

    题目链接 BZOJ1599 题解 最小环模板?周末了养生一下[逃] 解释一下原理 \(floyd\)算法每一轮求出以\([1,k]\)为中介点的最短路 我们对于一个环,考虑环上编号最大的点,在\(k\ ...