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 ...
随机推荐
- 标C编程笔记day06 动态分配内存、函数指针、可变长度參数
动态分配内存:头文件 stdlib.h malloc:分配内存 calloc:分配内存,并清零 realloc:调整已分配的内存块大小 演示样例: in ...
- C++ TinyXml操作(含源码下载)
前言 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译,使用TinyXML进行C++ XML解析,使用简单,容易上手.这个解析库的模型通过解析X ...
- 10 Questions To Make Programming Interviews Less Expensive--reference
Conducting Interview is not cheap and costs both time and money to a company. It take a lot of time ...
- MySQL(13):Select-order by
1. 按照字段值进行排序: 语法: order by 字段 升序|降序(asc|desc) 允许多字段排序,指的是,先按照第一个字段排序,如果说,不能区分,才使用第二个字段,以此类推. ...
- poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...
- Android Studio错误
晚上一直在折腾android studio这个东西,弄的蛋疼.. 之前是有用的,然后今天闲的没事干,更新了下,反正弄出了一大堆的错误.. 错误:failed to find Build Tools r ...
- MVC验证码的编写
主要是相互学习一下mvc,希望各位大神指导 /// <summary> /// 生成随机数字 /// </summary> /// <returns>随机数字< ...
- 遍历页面上所有TextBox,并赋值为String.Empty
//不含母板页 foreach (System.Web.UI.Control txtobj in this.Page.Controls) { if (txtobj.GetType().Na ...
- iOS开发之录音
录音 除了上面说的,在AVFoundation框架中还要一个AVAudioRecorder类专门处理录音操作,它同样支持多种音频格式.与AVAudioPlayer类似,你完全可以将它看成是一个录音机控 ...
- Jq/Js收集
判断checkbox选中的个数1.$('#del').click(function(){ var length = $("input[name='checkItem']:checked&qu ...