New Year Permutation(Floyd+并查集)
Description
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.
Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.
As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.
Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.
Input
The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.
The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.
Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.
Output
In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.
Sample Input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
1 2 4 3 6 7 5
5
4 2 1 5 3
00100
00011
10010
01101
01010
1 2 3 4 5
思路:
用floyd先将左右潜在的可以连通的边在邻接矩阵中标记出来,然后像冒泡一样的给他们排序一下就可以了
要注意两点:
一是刚开始开数组的时候,要从0开始开,不然用scanf输入会很麻烦
二是floyd的三层for循环,注意k的顺序要在最前面 这题也是对并查集数据结构的一种很好的理解,不过忽略了并查集分组的性质,而只关注了并查集的连通性
即通过邻接矩阵得到扩展边与边的连通关系
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; int n;
int p[];
char f[][]; int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i < n;i++)
scanf("%d",&p[i]);
for(int i = ;i < n;i++)
{
scanf("%s",f[i]);
for(int j = ;j < n;j++)
if(f[i][j] == '')
f[i][j] = '';
}
for(int k = ;k < n;k++)
for(int i = ;i < n;i++)
for(int j = ;j < n;j++)
if(f[i][k]=='' && f[k][j]=='')
f[i][j] = ''; for(int i = ;i < n;i++)
for(int j = i+;j < n;j++)
if(f[i][j]=='' && p[j] < p[i])
swap(p[i],p[j]); for(int i = ;i < n-;i++)
printf("%d ",p[i]);
printf("%d\n",p[n-]);
}
return ;
}
New Year Permutation(Floyd+并查集)的更多相关文章
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- CF 500 B. New Year Permutation 并查集
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】
度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...
- 求最小环 —— 并查集 与 Floyd
对于一个图,如何求出其中的最小环(不包括一元环)? 很显然,对于一个无向图,每一条边都是一个二元环:对于有向图,可以考虑从每一个点出发,用DFS求出它到自己的距离,如果遍历了$N$个点仍未便利到自己, ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
随机推荐
- Trie和Ternary Search Tree介绍
Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...
- 高效JQuery编码
缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); ...
- Swift Core Data 图片存储与读取Demo
实体的模型定义: 实体的class定义: @objc(ImageEntity) class ImageEntity: NSManagedObject { @NSManaged var imageDat ...
- TinyXml 快速入门(三)
在<TinyXml 快速入门(二)>介绍使用tinyxml库获取xml文件声明,查询指定节点.删除指定节点的做法.在本文中继续介绍修改指定节点和增加节点的做法. 修改节点其实和查询指定节点 ...
- [转] 浅谈 C++ 中的 new/delete 和 new[]/delete[]
转:http://www.cnblogs.com/hazir/p/new_and_delete.html 在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以 ...
- Trie 字典树
字典树是哈希树的变种, 它采用公用前缀的方式来提高效率, 刚开始以为公用前缀, 空间会节省, 后来想想, 空间也不是节省, 因为每一个都有26个指针(这里假设都是小写字母的话), 不过他的时间复杂度是 ...
- ASP.NET-FineUI开发实践-9(二)
其实我也不会,老实教人学怕误人子弟,但是抱着毁人不倦的精神还是糊弄糊弄个别小白吧,最起码能加点原创. 下面以表单为例,打开官方项目,版本为FineUI_4.1.1,打开form_compare页,右键 ...
- 关于禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项
这个问题是我新装好sql2008r2以后,我把服务器上的数据库还原到本地,取代码里跟踪测试的时候,出现的这个问题. 然后我在网上找了之后在sql里直接新建查询执行如下语句: exec sp_confi ...
- Jquery插件-Html5图片上传并裁剪
/** * 图片裁剪 * @author yanglizhe * 2015/11/16 */ (function($){ /** * Drag */ var Drag={obj:null,init:f ...
- (转)PHP 的 __FILE__ 常量
今天碰到了PHP的常量__FILE__的问题了. 在网上查了一下.总结了以下规律. dirname(__FILE___) 函数返回的是脚本所在在的路径. 比如文件 b.php 包含如下内容: < ...