题目链接:http://codeforces.com/problemset/problem/500/B

题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn。紧接着是一个 n * n 的矩阵A,当且仅当 Aij = 1 时,pi 与 pj 可以交换数值。现在问如何交换数值,使得最后得到的排列字典序最小。

  比赛的时候不会做,看了Tutorial 1 的解法,觉得别人做得太巧妙了,出题者也出得很好 ^_^

  可以看这个:http://codeforces.com/blog/entry/15488

它是利用了并查集来做的。如果遇到 Aij = 1 的话,就将点 i 和 点 j 连一条边。最终会得到一些互不相交的集合。以第一组 test 来说吧~~~

  

  

  这就表示位置 1、4、7 的数是可以交换的,位置 2、5 要保持原封不动,位置 3、6 的数可以交换。由于每个集合都有一个祖先,即第一层的那个数,依次为 7、2、5、6,然后就把每个集合的数放到以该集合祖先为首的 vector 数组里面,并把每个集合内的数从小到大排序。最后遍历位置 1 ~ n,根据每个位置的数所属的集合(以哪个祖先为首),依次输出。

  只能说,解题思路真是太巧妙兼神奇!竟然可以转化成图!继续干爸爹吧 ^_^

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ;
int p[maxn], cnt[maxn];
int group[maxn];
char s[maxn];
vector<int> vi[maxn]; int find(int x)
{
if (x == group[x])
return x;
return group[x] = find(group[x]);
} void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
group[fx] = fy;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n;
while (scanf("%d", &n) != EOF)
{
vi[n].clear();
for (int i = ; i <= n; i++) {
scanf("%d", &p[i]);
group[i] = i;
}
for (int i = ; i <= n; i++) {
scanf("%s", s+);
for (int j = ; j <= n; j++) {
if (s[j] == '') {
merge(i, j);
}
}
}
for (int i = ; i <= n; i++) {
vi[find(i)].push_back(p[i]); // 千万不要写成 vi[group[i]].push_back(p[i]);
}
for (int i = ; i <= n; i++) {
sort(vi[i].begin(), vi[i].end());
}
memset(cnt, , sizeof(cnt));
for (int i = ; i <= n; i++) {
int g = group[i];
printf("%d%c", vi[g][cnt[g]++], i == n ? '\n' : ' ');
}
}
return ;
}

  注意,代码中的 51 行的部分,如果写成 vi[group[i]].push_back(p[i]) 是错的!以第二个 test 为例,group[1] = 3,find[1] = 5。find[i] 才会找到最终的祖先!!!

codeforces 500B.New Year Permutation 解题报告的更多相关文章

  1. codeforces B. Levko and Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...

  2. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  3. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  4. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  5. codeforces 483C.Diverse Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/C 题目意思:给出 n 和 k,要求输出一个含有 n 个数的排列 p1, p2, ...,pn,使得 ...

  6. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

  7. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  8. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. PHP如何实现页面静态化

    1.file_put_contents()函数 2.fwrite()函数 3.使用PHP内置缓存机制实现页面静态化-output_buffering

  2. 清北国庆day1 (脑)残

    (留坑) /* 不知道为什要找的循环节TM这么长 */ #include<cstdio> #include<cstdlib> #include<cstring> u ...

  3. git/gitLab

    gitlab安装:http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml http://www.cnblogs.com/wi ...

  4. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  5. hadoop 之 kafka 安装与 flume -> kafka 整合

    62-kafka 安装 : flume 整合 kafka 一.kafka 安装 1.下载 http://kafka.apache.org/downloads.html 2. 解压 tar -zxvf ...

  6. Swift2.1 语法指南——协议

    原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm ...

  7. redis-key2

    package com.ztest.redis; import java.util.List; import redis.clients.jedis.Jedis; import com.sun.ist ...

  8. servlet和http请求

    1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...

  9. git上传github上

    1.git init --初始化git   (选择文件夹) 2.git add README  --添加项目(项目的文件夹) 3.git commit -m "SSM(360)" ...

  10. sqlite采用的ORM包

    关注了两个库的使用方式,一个是Dapper,一个是Simple.Data.考虑一个可选的是ORMLite, Dapper和simple.data都比较好用,动态对象的使用妙不可言,区别在于 //Dap ...