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 ...
随机推荐
- ios AFNetworking 有用篇
在寻常开发中,af是个非常好用的东西.非常喜欢.可是网上的af找了好多都不太全面,不有用.所以我今天做了一个demo.有上传下载的. 比較有用.希望大家可以用到. 去我github下载demo git ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
- 简单dp ---HDU3485 Count 101
题目大意:让求长度为n的0 和 1 构成的串中不包含101子串的个数有多少. 这个题当时想了好久,以为是一个规律题,一直在推规律,最后还是wa了,上网一看原来是dp问题, 不过确实递推式挺巧妙的. 递 ...
- (转)C#读取文件路径
//获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssembly ( ) ...
- DataGrid 使用模型列后实现点击列名称排序
DataGrid 使用模型列后实现点击列名称排序 代码如下: <DataGridTemplateColumn Header="型 号" SortMemberPath=&quo ...
- JavaScript基础(一)
JavaScript一.什么是JavaScript?脚本描述语言,网页交互特效,说白了,就是实现HTML实现不了的效果.(JavaScript是一种基于对象.事件驱动的简单脚本语言,嵌入在HTML文档 ...
- apache 2.4 You don't have permission to access / on this server
用的2.4版本,以前版本解决: 马上打开apache的配置文件httpd.conf,逐行检查.在大约快一半的地方有以下这段代码: <Directory /> Options Foll ...
- SQL SERVER 查询特定的前几条数据
1. 使用MS SQL Server 2008: 2. 数据库内容如下: insert into xuexi1 values('张三0', '数学', 98 ) insert into xuexi1 ...
- CAGradientLayer实现色差动画
效果图: 代码部分: RPGradientAnimationView.h #import <UIKit/UIKit.h> typedef enum : NSUInteger { RPGra ...
- dom方法读取xml(不常用)
Book.java package com.xml.demo; public class Book { private int id; private String name; private Flo ...