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 ...
随机推荐
- SAX PULL解析实例
XML三种解析方式: SAX解析:基于事件驱动,事件机制基于回调函数的,得到节点和节点之间内容时也会回调事件 PULL解析:相同基于事件驱动,仅仅只是回调时是常量 DOM解析:是先把XML文件装入内存 ...
- [AngularJS] ngMessageFormat
ngMessageFormat can be installed via npm using the following command: $ npm install angular-message- ...
- 对不起,说句粗话——这个太屌了,windows1.0安装程序(附下载)
今天逛一个软件论坛发现的,仅仅有几百K.遥想当今我刚接触windows的版本号是3.1,当时记得非常清楚哦,进入windows要从dos命令行进入.如今一转眼,变成进入伪dos是执行栏里敲cmd了.唉 ...
- mybatis 自动生成xml文件配置
http://blog.csdn.net/techbirds_bao/article/details/9233599/
- Gamit的安装
--2017-1-6修正 我这里用的是Gamit10.5,系统是Ubunt 14. 1:获取root权限(仅针对装好Ubunt后没有修改过root密码的用户,如果修改过密码,但是忘掉了,则请进入单用户 ...
- Dev GridControl 按条件合并相同单元格
Dev 默认的合并方式,只要(垂直方向)相邻两个单元格的值相同都会进行合并,这种方式并不是最优的,所以需要在进行合并的过程中进行判断. 方式如下: 1:先设置需要合并的列为允许合并 OptionsVi ...
- Android开发中在一个Activity中关闭另一个Activity
比如有ActivityA, ActivityB,在ActivityB中关闭ActivityA 解决方案: 1. 在 ActivityA 里面设置一个静态的变量instance,初始化为this在 Ac ...
- .net中Web.config文件的基本原理及相关设置
11.7 使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化 ...
- C#基础学习第二天(.net菜鸟的成长之路-零基础到精通)
1.加号的使用 在我们c#当中,如果想要两个字符串相连接,那么我们可以使用+号连接. 加号两边如果有一边是字符串,那么此时字符串起到了一个连接的作用. 如果加号两遍都是数字,那么加号起到一个相加 ...
- delphi2010 开发及调试WebService 实例
使用delphi已经10多年了,一直搞桌面程序开发,对Webservice一直很陌生,近来因工作需要,学习delphi开发WebService,担心遗忘,作此笔记. 特别感谢 中塑在线技术总监 大犇 ...