题目链接:

  http://codeforces.com/problemset/problem/691/D

题目大意:

  给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交换X Y位置上的数字,求可以得到的最大字典序的数列。

题目思路:

  【搜索】【并查集】

  这题可以用搜索或者并查集写,都能过。

  把位置分成若干块,每一块里面的位置都是可以被这一块里另一个位置经过若干次调换的(类似强连通,位置可达)。

  然后把每一块位置里的 位置按从小到大排序,位置上的值按从大到小排序,依次填入位置(最大的放最前)。

  每个点都会被放且仅放一次。

并查集:

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int fa[N],next[N],last[N],c[N],a[N],b[N],d[N];
bool mark[N];
int zhao(int aa)
{
if(fa[aa]== || fa[aa]==aa)return aa;
return fa[aa]=zhao(fa[aa]);
}
bool cmp1(int aa,int bb)
{
return aa>bb;
}
bool cmp2(int aa,int bb)
{
return aa<bb;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,fx,fy;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(last,);mem(mark,);mem(fa,);
scanf("%d",&m);
for(i=;i<=n;i++)
scanf("%d",c+i);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
fx=zhao(x);
fy=zhao(y);
fa[fy]=fx;
}
for(i=;i<=n;i++)
{
fa[i]=zhao(i);
next[i]=last[fa[i]],last[fa[i]]=i;
}
for(i=;i<=n;i++)
{
if(mark[c[i]])continue;
for(k=,j=last[fa[i]];j;j=next[j])
{
a[++k]=c[j];
b[k]=j;
}
sort(a+,a++k,cmp1);
sort(b+,b++k,cmp2);
for(j=;j<=k;j++)
d[b[j]]=a[j],mark[a[j]]=;
}
for(i=;i<=n;i++)
printf("%d ",d[i]);
puts("");
}
return ;
}
/*
// //
*/

搜索(学长写的):

 #include<stdio.h>
#include<algorithm>
#define MAXN 1000002
using namespace std; int a[MAXN],first[MAXN],to[*MAXN],next[*MAXN];
int stack_a[MAXN],stack_loc[MAXN],ans[MAXN];
bool vis[MAXN];
int top; void dfs(int u)
{
int e,v; vis[u]=true;
stack_a[top]=a[u];
stack_loc[top++]=u;
e=first[u];
while(e)
{
v=to[e];
if(!vis[v])dfs(v);
e=next[e];
}
} int main()
{
int n,m,i,j,u,v; scanf("%d%d",&n,&m);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
next[i]=first[u];
next[i+m]=first[v];
first[u]=i;
first[v]=i+m;
to[i]=v;
to[i+m]=u;
}
for(i=;i<=n;i++)
{
if(!vis[i])
{
top=;
dfs(i);
sort(stack_a,stack_a+top);
sort(stack_loc,stack_loc+top);
for(j=;j<top;j++)ans[stack_loc[j]]=stack_a[top-j-];
}
}
for(i=;i<=n;i++)
{
printf("%d",ans[i]);
printf("%c",i<n?' ':'\n');
} return ;
}

【搜索】【并查集】Codeforces 691D Swaps in Permutation的更多相关文章

  1. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  2. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  3. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  4. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  5. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  6. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  8. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

随机推荐

  1. 第五篇:python基础之循环结构以及列表

    python基础之循环结构以及列表   python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...

  2. HTML - 键盘事件

    Keyboard 事件 onkeydown: 在用户按下按键时触发. onkeypress: 在用户敲击按钮时触发. onkeyup: 当用户释放按键时触发. 示例 <!DOCTYPE html ...

  3. Sql2008中使用DataTable作为存储过程的参数

    使用DataTable作为存储过程的参数   最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...

  4. Java开发工程师必会做试题

    一.单选题     (共19道题,每题5分) 1.下面有关java的一些细节问题,描述错误的是? A.构造方法不需要同步化 B.一个子类不可以覆盖掉父类的同步方法 C.定义在接口中的方法默认是publ ...

  5. spring集成 log4j + slf4j

    以maven web项目为例, 首先.在pom文件引入相关依赖,如下(spring官网文档有介绍): <dependencies> <!-- spring 相关 --> < ...

  6. Ubuntu系统配置日志/var/log/message

    ubuntu系统默认不生成/var/log/messages文件,有时候想查看相关日志就很不方便,于是我们可以设置使系统生成此文件. 1.先安装 apt-get install rsyslog2.用v ...

  7. WPF里面的常用笔刷

    程序运行效果 <Window x:Class="This_brush.MainWindow" xmlns="http://schemas.microsoft.com ...

  8. Chess---->简单命令框象棋(人VS人)

    简单粗暴,直接先上代码: ChessBoard.h:  1 #ifndef CHESBOARD_H  2 #include<iostream>  3 #include<string& ...

  9. centos 7 samba相关命令

    1.安装相关包 yum install samba samba-client samba-common 2.启动smb的命令 systemctl enable smb.service systemct ...

  10. CentOS 6.5 IP 设置

    DEVICE=eth0TYPE=EthernetUUID=7d6d54e0-054d-472b-8cc1-080f16ef36c1ONBOOT=yesNM_CONTROLLED=yesBOOTPROT ...