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++。
代码实现如下:
#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骚操作——传递闭包的更多相关文章
- Typescript骚操作,在TS里面直接插入HTML
Typescript骚操作,在TS里面直接插入HTML,还有语法提示 先给大家看一个图 因为我不喜欢用很重的框架,主要是并非专业UI,但是偶尔会用到,还是觉得直接element组装受不了,想想能在ts ...
- 闪电侠 Netty 小册里的骚操作
前言 即使这是一本小册,但基于"不提笔不读书"的理念,仍然有必要总结一下.此小册对于那些"硬杠 Netty 源码 却不曾在千万级生产环境上使用实操"的用户非常有 ...
- awk骚操作
一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...
- 如何在命令长度受限的情况下成功get到webshell(函数参数受限突破、mysql的骚操作)
0x01 问题提出 还记得上篇文章记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门),我们讲到了一些PHP的一些如何巧妙地绕过数字和字母受限的技巧,今天我要给大家分享的是如 ...
- UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)
题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...
- 关于map 及 map 骚操作
关于map这个东西 很冷门.................. 但是,这个博客带你稍微了解一下map: map用法:一般当作一个下表无穷大的数组 关于它的骚操作:map的鬼畜用法,可以 ...
- 通过HTTP的HEADER完成各种骚操作
作为一名专业的切图工程师,我从来不care网页的header,最多关心Status Code是不是200.但是HEADER真的很重要啊,客户端从服务器端获取内容,首先就是通过HEADER进行各种沟通! ...
- 洛谷 P1045 麦森数 (快速幂+高精度+算位数骚操作)
这道题太精彩了! 我一开始想直接一波暴力算,然后叫上去只有50分,50分超时 然后我改成万位制提高运算效率,还是只有50分 然后我丧心病狂开long long用10的10次方作为一位,也就是100亿进 ...
- Mac OS 上的一些骚操作
本帖记录个人在使用 Mac 操作系统上的一些骚操作,不断更新,以飨读者. 快速移动网页到顶部或底部 用双指上下划触摸板吗?NO,我们有更骚的操作: command + ↑ 回到顶部 command + ...
随机推荐
- Token安全
token相对安全加密算法 http://blog.csdn.net/q8649912/article/details/52370565 关于文章的理解 1 sessionid 这个名词应该理解为:一 ...
- 几个关于控件的优先级: UseSystemPasswordChar > PasswordChar > 控件属性设置
using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...
- Tomcat 设计模式分析
门面设计模式 门面设计模式在 Tomcat 中有多处使用,在 Request 和 Response 对象封装中.Standard Wrapper 到 ServletConfig 封装中.Applica ...
- [NOI.AC省选模拟赛3.31] 星辰大海 [半平面交]
题面 传送门 思路 懒得解释了......也是比较简单的结论 但是自己看到几何就退缩了...... 下周之内写一个计算几何的学习笔记! Code #include<iostream> #i ...
- BZOJ2286:[SDOI2011]消耗战——题解
+++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...
- BZOJ2821:作诗——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2821 问题描述 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好 ...
- AOJ. 数组训练.2016-11-17
A题 #include <stdio.h> #include <stdlib.h> #define max 1000 __int64 a[max] = {0,1,1}; int ...
- warning: React does not recognize the xxx prop on a DOM element
这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute. 通常{...this.props}和cloneElement( ...
- jquery Promise和ES6 Promise的区别
1. Deferred对象有resolve和reject方法,可以直接修改状态 jquery用Deferred实现了Promise规范,Deferred与ES6 Promise的最大区别是: Defe ...
- 51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)
单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #inclu ...