「CF500B」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) wherea1 = 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.
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.
In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.
1 2 3 4 5 6 7 8 9 |
7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 |
1 |
1 2 4 3 6 7 5 |
1 2 3 4 5 6 7 |
5 4 2 1 5 3 00100 00011 10010 01101 01010 |
1 |
1 2 3 4 5 |
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).

题解
用并查集维护连通性,从左往右从连通块中每次取出最小的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<vector> #include<queue> #include<algorithm> #include<map> #include<cmath> #define eps 1e-6 #define inf 1000000000 #define pa pair<int,int> #define ll long long using namespace std; ll read() { ll x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } int n; int p[305],fa[305]; int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } int main() { n=read(); for(int i=1;i<=n;i++)p[i]=read(); for(int i=1;i<=n;i++)fa[i]=i; char ch[305]; for(int i=1;i<=n;i++) { scanf("%s",ch+1); for(int j=1;j<=n;j++) if(ch[j]=='1') { int p=find(i),q=find(j); if(p!=q)fa[p]=q; } } for(int i=1;i<=n;i++) { int t=i; for(int j=i+1;j<=n;j++) if(find(j)==find(i)&&p[j]<p[t])t=j; swap(p[i],p[t]); } for(int i=1;i<=n;i++)printf("%d ",p[i]); return 0; |
分类目录
热门文章
- 11383NOIP1999拦截导弹
- 11057「BZOJ3261」最大异或和
- 10866NOIP2010导弹拦截
- 90992017 训练赛 1 by hzwer
- 8872「百度之星2017」程序设计大赛 初赛(B)
- 8023NOIP2010引水入城
- 7749「JoyOI1048」田忌赛马
- 76802017ACM萧山训练第3场(World Final 2013)
- 76472017ACM萧山训练第4场(CTUO 2015)
- 7508「CF718X」Codeforces Round #373 (Div. 1)
- 7368「BZOJ3709」[PA2014] Bohater
- 7298NOI2010 超级钢琴
- 7252「BZOJ1046」[HAOI2007] 上升序列
- 7190「BZOJ1150」[CTSC2007] 数据备份Backup
- 7071「BZOJ2811」[Apio2012] Guard
- 6745「BZOJ3105」[CQOI2013] 新Nim游戏
- 6721「BZOJ2288」「POJ Challenge」生日礼物
- 6571「BZOJ2151」种树
- 6563「BZOJ3166」[HEOI2013] Alo
- 6412「BZOJ1086」[SCOI2005] 王室联邦
- 6359NOIP2007纪念品分组
- 6336「BZOJ4027」[HEOI2015] 兔子与樱花
- 6246NOIP2012国王游戏
- 6191NOI2014随机数生成器
- 6066「BZOJ1043」[HAOI2008] 下落的圆盘
- 5989POJ训练记录2
- 5965「CF538X」Codeforces Round #300
- 59352017ACM萧山训练第5场(2016 Pacific Northwest - Division 1)
- 57912015 ACM / ICPC EC - Final
- 5707NOIP2004合并果子
- 5651「BZOJ1229」[USACO2008 Nov] toy 玩具
- 5635NOI2009诗人小G
- 55922017ACM萧山训练第2场(NWERC 2008)
- 5572「codechef」January Challenge 2015
Myfriends
近期评论
- 匿名发表在《算法竞赛编程变量命名指南》
- 匿名发表在《「czy系列赛」czy的后宫5》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》