B. New Year Permutation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ ni ≠ 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 ≤ nAi, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ nAi, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Examples
input
75 2 4 3 6 7 10001001000000000000101000001000000000100001001000
output
1 2 4 3 6 7 5
input
54 2 1 5 30010000011100100110101010
output
1 2 3 4 5
Note

In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).

In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).

A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

题意:给你一个数字n,然后底下n个数字,然后再给你n排01串,01串的意思是如果第i行第j列的数字是1,那么上面的数字串里的第i个数字和第j个数字就可以交换位置,题目保证m[i][j] = m[j][i],问你按照这么操作下来(如果m[i][j] = 1,那么A[i]与A[j]可交换,也可不交换),能得到的数字串最小的字典序是多少

思路:dfs

#include <iostream>
using namespace std;

int m[400][400];
int g[400];
bool vis[400];
int x;
int t,it;
bool flag[400];
int w[400];
//dfs的目的是得到x可以的最小值
void dfs( int a ){
	if( !vis[g[a]] ){//如果说g[a]没有遍历过,那么取小的值
		x = min( x,g[a] );
	}
	w[a] = it;//然后第a个数字,就用过了,防止下面出现死循环
	for( int i = 0; i < t; i++ ){
		if( m[a][i] == 1 and w[i] != it ){//搜索可能出现的最小值
			dfs(i);
		}
	}
}

int main(){
	cin >> t;
	for( int i = 0; i < t; i++ ){
		scanf("%d",&g[i]);
	}
	for( int i = 0; i < t; i++ ){
		for( int j = 0; j < t; j++ ){
			scanf("%1d",&m[i][j]);
		}
	}
	for( int i = 0; i < t; i++ ){
		it++;//记录是第几个数字
		x = t;//先令x的序号为最大
		dfs(i);
		vis[x] = true;
		printf("%d ",x);
	}
}

好多大佬用的是floyd算法,仔细研究了一下dalao的代码,受益匪浅啊~

#include <iostream>
using namespace std;
const int maxn = 400;
bool vis[maxn];
int A[maxn],m[maxn][maxn];
int a;
int pos[maxn];

int main(){
	cin >> a;
	for( int i = 1; i <= a; i++ ){
		cin >> A[i];
		pos[A[i]] = i;//离散化,这不操作可以即可以根据数字找到位置,也可以根据位置找到数字
	}
	for( int i = 1; i <= a; i++ ){
		for( int j = 1; j <= a; j++ ){
			scanf("%1d",&m[i][j]);
		}
		m[i][i] = 1;//自己与自己交换是被允许的
	}

	for( int k = 1; k <= a; k++ ){// floyd一下
		for( int i = 1; i <= a; i++ ){
			for( int j = 1; j <= a; j++ ){
				if( m[i][k] == 1 and m[k][j] == 1 ){
					m[i][j] = 1;
				}
			}

		}
	}
	//i代表i号位置,j代表数字j
	for( int i = 1; i <= a; i++ ){//这个循环是循环数组
		for( int j = 1; j <= a; j++ ){
			if( m[i][pos[j]] == 1 and !vis[j] ){//因为pos[j]是j的位置,j又是从小到达的,这步贪心
				cout << j << " ";
				vis[j] = true;//j这个数字用过了
				pos[A[i]] = pos[j];//改变A[i]的位置为j的位置(A[i]与j位置互换)
				pos[j] = i;//j的位置变成了i
				break;
			}
		}
	}
}

codeforces 508B的更多相关文章

  1. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. 涂抹mysql笔记-InnoDB/MyISAM及其它各种存储引擎

    存储引擎:一种设计的存取和处理方式.为不同访问特点的表对象指定不同的存储引擎,可以获取更高的性能和处理数据的灵活性.通常是.so文件,可以用mysql命令加载它. 查看当前mysql数据库支持的存储引 ...

  2. JVM内部细节之一:synchronized关键字及实现细节(轻量级锁Lightweight Locking)

    在C程序代码中我们可以利用操作系统提供的互斥锁来实现同步块的互斥访问及线程的阻塞及唤醒等工作.然而在Java中除了提供Lock API外还在语法层面上提供了synchronized关键字来实现互斥同步 ...

  3. Docker系列07:Docker-compose

    1  什么是Docker-Compose Compose项目来源于之前的fig项目,使用python语言编写,与docker/swarm配合度很高. Compose 是 Docker 容器进行编排的工 ...

  4. kubernetes学习笔记之十一:kubernetes dashboard认证及分级授权

    第一章.部署dashboard 作为Kubernetes的Web用户界面,用户可以通过Dashboard在Kubernetes集群中部署容器化的应用,对应用进行问题处理和管理,并对集群本身进行管理.通 ...

  5. 为 github markdown 文件生成目录(toc)

    业务需要 在编写 github 项目时,有时候会编写各种 README.md 等 markdown 文件,但是 github 默认是没有目录的. 于是就自己写了一个小工具. markdown-toc ...

  6. django之Models和ORM

    ORM Object Relational Mapping,简称ORM,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久 ...

  7. TCP‘三次握手’和‘四次挥手’(通俗易懂)

      概述 我们都知道 TCP 是 可靠的数据传输协议,UDP是不可靠传输,那么TCP它是怎么保证可靠传输的呢?那我们就不得不提 TCP 的三次握手和四次挥手. 三次握手 下图为三次握手的流程图 下面通 ...

  8. zabbix4.0下zabbix-agentd安装

    转:http://www.safecdn.cn/monitor/2018/12/zabbix4-0-zabbix-agentd-install/316.html 一 安装源和Zabbix的依赖包: 1 ...

  9. Nginx 工作原理

    Nginx 工作原理 Nginx由内核和模块组成. Nginx本身做的工作实际很少,当它接到一个HTTP请求时,它仅仅是通过查找配置文件将此次请求映射到一个location block,而此locat ...

  10. java处理url中的特殊字符%等

    java处理url中的特殊字符(如&,%...) URL(Uniform Resoure Locator,统一资源定位器)是Internet中对资源进行统一定位和管理的标志.一个完整的URL包 ...