floyd骚操作——传递闭包
传递闭包的含义指通过传递性推导出尽量多的元素之间的关系,而传递闭包一般都是采用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++。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<int, ll> pil;; 19 typedef pair<int, int> pii; 20 typedef unsigned long long ull; 21 22 #define lson i<<1 23 #define rson i<<1|1 24 #define bug printf("*********\n"); 25 #define FIN freopen("D://code//in.txt", "r", stdin); 26 #define debug(x) cout<<"["<<x<<"]" <<endl; 27 #define IO ios::sync_with_stdio(false),cin.tie(0); 28 29 const double eps = 1e-8; 30 const int mod = 10007; 31 const int maxn = 4500 + 7; 32 const double pi = acos(-1); 33 const int inf = 0x3f3f3f3f; 34 const ll INF = 0x3f3f3f3f3f3f3f; 35 36 int n, m, u, v; 37 int relationship[107][107]; 38 39 int main() { 40 //FIN; 41 scanf("%d%d", &n, &m); 42 memset(relationship, 0, sizeof(relationship)); 43 for(int i = 1; i <= m; i++) { 44 scanf("%d%d", &u, &v); 45 relationship[u][v] = 1; 46 } 47 for(int k = 1; k <= n; k++) { 48 for(int i = 1; i <= n; i++) { 49 for(int j = 1; j <= n; j++) { 50 if(relationship[i][k] && relationship[k][j]) { 51 relationship[i][j] = 1; 52 } 53 } 54 } 55 } 56 int ans = 0, j; 57 for(int i = 1; i <= n; i++) { 58 for(j = 1; j <= n; j++) { 59 if(i == j) continue; 60 if(relationship[i][j] == 0 && relationship[j][i] == 0) { 61 break; 62 } 63 } 64 if(j > n) ans++; 65 } 66 printf("%d\n", ans); 67 return 0; 68 }
View Code
Problem 2(POJ1094)
题目链接:http://poj.org/problem?id=1094
题目:
题意:给你n个大写字母,m对大小关系,根据他给的关系推测是否有大小矛盾的情况。如果有矛盾的就输出是在第几组关系时矛盾;如果不矛盾,判断只需要前t对组关系就能推测出他们从小到大的排序;如果没有以上两种情况就输入无法确定。
思路:对于每输入一对关系就跑一次floyd判断一遍,如果能推测出他们的关系,那么就跑一边拓扑排序求出他们从小打到的排序情况;如果有矛盾的关系就直接输出是在第几组关系时矛盾;如果没有以上情况就输出无法确定。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<int, ll> pil;; 19 typedef pair<int, int> pii; 20 typedef unsigned long long ull; 21 22 #define lson i<<1 23 #define rson i<<1|1 24 #define bug printf("*********\n"); 25 #define FIN freopen("D://code//in.txt", "r", stdin); 26 #define debug(x) cout<<"["<<x<<"]" <<endl; 27 #define IO ios::sync_with_stdio(false),cin.tie(0); 28 29 const double eps = 1e-8; 30 const int mod = 10007; 31 const int maxn = 4500 + 7; 32 const double pi = acos(-1); 33 const int inf = 0x3f3f3f3f; 34 const ll INF = 0x3f3f3f3f3f3f3f; 35 36 int n, m, t, flag; 37 char s[1007][5]; 38 int d[30][30], in[30], num[30]; 39 vector<int> G[30]; 40 41 bool floyd() { 42 for(int k = 1; k <= n; k++) { 43 for(int i = 1; i <= n; i++) { 44 for(int j = 1; j <= n; j++) { 45 if(d[i][k] && d[k][j]) { 46 d[i][j] = 1; 47 } 48 } 49 } 50 } 51 for(int i = 1; i <= n; i++) { 52 for(int j = 1; j <= n; j++) { 53 if(i == j) continue; 54 if((d[i][j] && d[j][i]) || (d[i][j] == 0 && d[j][i] == 0)) { 55 return false; 56 } 57 } 58 } 59 return true; 60 } 61 62 void topsort(int m) { 63 t = 0; 64 for(int i = 1; i <= n; i++) { 65 G[i].clear(); 66 } 67 memset(in, 0, sizeof(in)); 68 for(int i = 1; i <= m; i++) { 69 int x = s[i][0] - 'A' + 1, y = s[i][2] - 'A' + 1; 70 G[x].push_back(y); 71 in[y]++; 72 } 73 queue<int> q; 74 for(int i = 1; i <= n; i++) { 75 if(in[i] == 0) { 76 q.push(i); 77 } 78 } 79 while(!q.empty()) { 80 int x = q.front(); q.pop(); 81 num[t++] = x; 82 for(int i = 0; i < G[x].size(); i++) { 83 int v = G[x][i]; 84 in[v]--; 85 if(in[v] == 0) { 86 q.push(v); 87 } 88 } 89 } 90 } 91 92 int main() { 93 //FIN; 94 while(~scanf("%d%d", &n, &m)) { 95 if(n == 0 && m == 0) break; 96 memset(d, 0, sizeof(d)); 97 for(int i = 1; i <= m; i++) { 98 scanf("%s", s[i]); 99 } 100 flag = 0; 101 for(int i = 1; i <= m; i++) { 102 int x = s[i][0] - 'A' + 1, y = s[i][2] - 'A' + 1; 103 d[x][y] = 1; 104 if(floyd()) { 105 printf("Sorted sequence determined after %d relations: ", i); 106 topsort(i); 107 for(int i = 0; i < t; i++) { 108 printf("%c", num[i] - 1 + 'A'); 109 } 110 printf(".\n"); 111 flag = 1; 112 } else { 113 for(int j = 1; j <= n; j++) { 114 for(int k = 1; k <= n; k++) { 115 if(j == k) continue; 116 if((d[j][k] && d[k][j])) { 117 printf("Inconsistency found after %d relations.\n", i); 118 flag = 1; 119 break; 120 } 121 } 122 if(flag) break; 123 } 124 } 125 if(flag) break; 126 } 127 if(!flag) printf("Sorted sequence cannot be determined.\n"); 128 } 129 return 0; 130 }
View Code
转载于:https://www.cnblogs.com/Dillonh/p/9388247.html
floyd骚操作——传递闭包相关推荐
- 五分钟没有操作自动退出_这又是什么骚操作??5只蚂蚁战略配售基金拟增设B类份额,自动赎回退出!!...
他来了,他来了,这又是什么骚操作??昨天,五只创新未来18个月封闭运作混合型证券投资基金发布联合声明,会为这个战略配售基金安排一个月的退出选择期. 5只创新未来18个月封闭运作混合型证券投资基金发布联 ...
- GitHub 骚操作,个人页还能这么玩?
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 之前写过一篇 GitHub 骚操作的文章 GitHub 竟 ...
- 杀疯了!通过游戏“元宇宙”,Deepmind让AI学会玩各种没玩过的游戏,骚操作不断...
来源:大数据文摘本文约1800字,建议阅读7分钟 面对任务一看就会的AI,离我们心里的通用人工智能还有多远呢? 对于AI来说,完成一个单一任务或许相对简单,但是涉及到合作和博弈时,AI往往显得有些愚蠢 ...
- K项目的一些心得之全球模板里的几个骚操作
K项目的一些心得之全球模板里的几个骚操作 1,数据迁移阶段,物料主数据分类视图里,batch class的代码跟物料号相同. 这意味着如果需要迁移的物料有1万个,导入程序会自动创建1万023类型的分类 ...
- git idea 可视化_那些你应该知道的,但是你一定不知道的 Git 骚操作
Hello 大家好,作为团队中的主程阿粉经常参与很多核心功能的开发,而且很多时候一个需求没做好中间又插入新的紧急的需求或者 bug 修复,每次遇到这种情况,如果两个地方代码不冲突的话还好,可以直接在本 ...
- 笔记合并_.NET Core开发实战(第23课:静态文件中间件:前后端分离开发合并部署骚操作)学习笔记(上)...
23 | 静态文件中间件:前后端分离开发合并部署骚操作 我们先来看一下静态文件中间件有哪些能力 1.支持指定相对路径 2.支持目录的浏览 3.支持设置默认文档 4.支持多目录映射 源码链接: http ...
- RabbitMQ 的这些骚操作你知道吗?
RabbitMQ的Java客户端统一使用com.rabbitmq.client作为顶级包名.其中,最核心的类主要有:ConnectionFactory.Connection.Channel.Consu ...
- 80%开发者都不知道的以太坊骚操作:「事件」和「日志」还可以这么玩!
80%开发者都不知道的以太坊骚操作:「事件」和「日志」还可以这么玩! 2018年05月02日 00:00:00 阅读数:366 作者 | 蔡一 志顶科技技术总监 4月6日,Daniel Larime ...
- Java 8 - Stream流骚操作解读2_归约操作
文章目录 Pre 什么是归约操作 元素求和 reduce reduce如何运行的 最大值和最小值 Pre Java 8 - Stream流骚操作解读见到过的终端操作都是返回一个 boolean ( a ...
最新文章
- 根据listObject中的某个字段排序
- Linux虚拟内存与线性地址翻译
- 【python图像处理】图像的增强(ImageEnhance类详解)
- unity 阳光插件_网络广告,阳光创信保驾护航
- 石头剪刀布程序流程图_“剪刀,石头,布”心理学
- seata分布式事务回滚机制是如何实现的
- redis的IM的聊天工具
- BZOJ-1055 玩具取名
- 2核4G阿里云服务器被黑客抓鸡??然鹅一个操作就搞定
- Visual Leak Detector(vld)无法显示内存泄露行号
- 写给学生看的系统分析与验证笔记(一)——形式化基础
- html中用于排版标题标签是,web端测试也需要懂-HTML排版标签
- 使用Python把BT种子转化为磁力链接
- [南阳OJ-No.33]蛇形填数|在n*n方陈里填入1,2,...,n*n,要求填成蛇形。
- pycharm 文件名颜色所代表的含义
- 鞭炮游戏 甲、乙,丙三人同时开始放第一个鞭炮
- 台式计算机耳机有杂音怎么办,小编教你解决电脑耳机有噪音和杂音怎么办几个方法!...
- GaussDB - 浅析华为高斯GaussDB
- 18种抗癌果蔬排行榜
- 元祖python_Python ---元祖
热门文章
- 神经网络输入数据预处理,神经网络自然语言处理
- 梅科尔工作室——KNN临近算法
- Bugku:加密 ok
- UVa340 Master-Mind Hints 猜数字游戏的提示 题解
- 塞尔达正在维护服务器,我的世界1.8服务器塞尔达
- mysql start sever_MySQL安装最后一步start server总是错号 ?
- Vue Element-ui Table表格排序
- 神经网络 最小二乘法,神经网络的数学方法
- JavaScript 绘制柱状图
- OneNet: End-to-End One-Stage Object Detection by Classification Cost