codeforces500B
New Year Permutation
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 < bkall 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.
Examples
- 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
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.
sol:十分显然的是a可以与b换,b可以与c换,a就可以和c换,于是把可以换的用并查集缩成一个个连通块,在每个联通块中做到最优,那么就是答案了
- #include <bits/stdc++.h>
- using namespace std;
- typedef int ll;
- inline ll read()
- {
- ll s=;
- bool f=;
- char ch=' ';
- while(!isdigit(ch))
- {
- f|=(ch=='-'); ch=getchar();
- }
- while(isdigit(ch))
- {
- s=(s<<)+(s<<)+(ch^); ch=getchar();
- }
- return (f)?(-s):(s);
- }
- #define R(x) x=read()
- inline void write(ll x)
- {
- if(x<)
- {
- putchar('-'); x=-x;
- }
- if(x<)
- {
- putchar(x+''); return;
- }
- write(x/);
- putchar((x%)+'');
- return;
- }
- #define W(x) write(x),putchar(' ')
- #define Wl(x) write(x),putchar('\n')
- const int N=;
- int n,a[N],Pos[N];
- bool Bo[N][N];
- int Father[N];
- inline int Get_Father(int x)
- {
- return (Father[x]==x)?(x):(Father[x]=Get_Father(Father[x]));
- }
- struct Record
- {
- int Shuz,Weiz;
- };
- inline bool cmp_Shuz(Record p,Record q)
- {
- return p.Shuz<q.Shuz;
- }
- inline bool cmp_Weiz(Record p,Record q)
- {
- return p.Weiz<q.Weiz;
- }
- vector<Record>Liantong[N];
- vector<int>Xulie[N];
- int main()
- {
- int i,j;
- R(n);
- for(i=;i<=n;i++)
- {
- R(a[i]); Father[i]=i;
- }
- for(i=;i<=n;i++)
- {
- for(j=;j<=n;j++)
- {
- char ch=' ';
- while(!isdigit(ch)) ch=getchar();
- if(ch=='')
- {
- Father[Get_Father(i)]=Get_Father(j);
- }
- }
- }
- for(i=;i<=n;i++)
- {
- Liantong[Get_Father(i)].push_back((Record){a[i],i});
- }
- for(i=;i<=n;i++) if(Liantong[i].size())
- {
- sort(Liantong[i].begin(),Liantong[i].end(),cmp_Shuz);
- for(j=;j<Liantong[i].size();j++) Xulie[i].push_back(Liantong[i][j].Shuz);
- }
- for(i=;i<=n;i++) if(Liantong[i].size())
- {
- sort(Liantong[i].begin(),Liantong[i].end(),cmp_Weiz);
- for(j=;j<Liantong[i].size();j++) a[Liantong[i][j].Weiz]=Xulie[i][j];
- }
- for(i=;i<n;i++) W(a[i]); Wl(a[n]);
- return ;
- }
- /*
- input
- 7
- 5 2 4 3 6 7 1
- 0001001
- 0000000
- 0000010
- 1000001
- 0000000
- 0010000
- 1001000
- output
- 1 2 4 3 6 7 5
- input
- 5
- 4 2 1 5 3
- 00100
- 00011
- 10010
- 01101
- 01010
- output
- 1 2 3 4 5
- */
codeforces500B的更多相关文章
随机推荐
- Android 由 android:launchMode="singleInstance“引发的界面无法返回的情况
问题描述:现有A.B.C三个Activity.现在A跳转到B再由B跳转到C,然后依次返回.正常情况是C先返回B然后再返回的A.但现在的情况是C直接跳过B直接返回到A了. 解决办法:认真排查了A.B.C ...
- .NET和F#周报第35周-.NET 8月重大更新
来看看8月份最后一个周F#和.NET最新相关信息. https://www.yuque.com/rock/fsharp-weekly/35 这次我们多聊聊.NET相关的东西, 看看.NET的健康生态. ...
- [Python]Python 函数调用小例子
函数定义: In [78]: def printme(str): ....: print str ....: return ....: 调用: In [79]: printme('This is Ji ...
- 校内模拟赛 SovietPower Play With Amstar
SovietPower Play With Amstar 题意: 一棵二叉树,每次询问一条路径上的路径和,初始每个点有一个权值1,询问后权值变为0.$n \leq 10^7,m\leq10^6$ 分析 ...
- ubuntu在指定的文件夹下创建python3和python2的虚拟环境
1.用pip安装virtualenv sudo apt-get install python-virtualenv 2.创建python2和python3虚拟环境 2.1 创建python2的虚拟环境 ...
- 【转】Git版本控制软件从入门到精通学习手册
GIT 学习手册简介 本站为 Git 学习参考手册.目的是为学习与记忆 Git 使用中最重要.最普遍的命令提供快速翻阅. 这些命令以你可能需要的操作类型划分,并且将提供日常使用中需要的一些常用的命令以 ...
- NB-IOT_BC95_B5常用AT指令集
.AT+<cmd>=? 测试命令,用于向模块询问支持的设置项目. .AT+<cmd>? 读取命令,用于让模块上报某个命令代表的设置项当前的值. .AT+<cmd>= ...
- VS2013软件的安装和单元测试
VS2013是什么? 微软在Builder 2013开发者大会上发布了Visual Studio 2013预览版,并且发布其程序组件库.NET 4.5.1的预览版.该软件已于北京时间2013年11月1 ...
- 【CV】ICCV2015_Unsupervised Learning of Spatiotemporally Coherent Metrics
Unsupervised Learning of Spatiotemporally Coherent Metrics Note here: it's a learning note on the to ...
- Linux内核及分析 第二周 操作系统是如何工作的?
计算机是如何工作的? 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函 ...