题目链接

分析:一些边把各个节点连接成了一颗颗树。因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置。

我用了极为暴力的方法,先dfs每棵树,再用了优先队列。我估计最大复杂度约在O(Nlog(N)),理论上应该跑不过。因为再cf上做题,看见5s时限,强行上了。很侥幸,在4秒的时候过了= =。

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 1e6 + 5;
std::vector<int> G[maxn];
int N, M;
int p[maxn];
bool vis[maxn];
struct Node {
int val;
bool operator <(const Node &rhs) const {
return val > rhs.val;
}
};
priority_queue<Node> q;
priority_queue<int> ans;
void dfs(int u, int fa) {
if (vis[u]) return;
vis[u] = true;
ans.push(p[u]);
q.push((Node){u});
for (int i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
}
}
int main(int argc, char const *argv[]) {
cin>>N>>M;
mem(vis, false);
for (int i = 1; i <= N; i ++) scanf("%d", p + i);
for (int i = 0; i < M; i ++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].pb(v);
G[v].pb(u);
}
for (int i = 1; i <= N; i ++) {
if (!vis[i]) dfs(i, -1);
while (!ans.empty()) {
int tmp = ans.top(); ans.pop();
Node x = q.top(); q.pop();
int id = x.val;
p[id] = tmp;
}
}
for (int i = 1; i <= N; i ++) cout<<p[i]<<" ";
return 0;
}

正解没有那么暴力啊。正解是通过并查集来区分树。因为并查集的原因,遍历点的时候一定是从小到大的,省去了对位置的排序,复杂度又降了一个常数。这里只需要对各个树中的值排序就行了。复杂度比我的方法小了太多。用vector就可以了。

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 1e6 + 5;
int par[maxn], p[maxn];
std::vector<int> pos[maxn];
std::vector<int> num[maxn];
int N, M;
void init() {
for (int i = 1; i <= N; i ++) par[i] = i;
}
int findpar(int x) {
return par[x] = (par[x] == x ? x : findpar(par[x]));
}
void unite(int x, int y) {
x = findpar(x), y = findpar(y);
if (x == y) return;
par[x] = y;
}
int main(int argc, char const *argv[]) {
cin>>N>>M;
init();
for (int i = 1; i <= N; i ++) scanf("%d", p + i);
for (int i = 0; i < M; i ++) {
int u, v;
scanf("%d%d", &u, &v);
unite(u, v);
}
for (int i = 1; i <= N; i ++) {
int x = findpar(i);
pos[x].pb(i);
num[x].pb(-p[i]);
}
for (int i = 1; i <= N; i ++) {
sort(num[i].begin(), num[i].end());
for (int j = 0; j < pos[i].size(); j ++) {
int id = pos[i][j];
p[id] = -num[i][j];
}
}
for (int i = 1; i <= N; i ++) cout<<p[i]<<" ";
return 0;
}

Educational Codeforces Round 14 D. Swaps in Permutation的更多相关文章

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

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

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

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

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

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

  4. Educational Codeforces Round 14

    A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...

  5. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  6. Educational Codeforces Round 14 C. Exponential notation 数字转科学计数法

    C. Exponential notation 题目连接: http://www.codeforces.com/contest/691/problem/C Description You are gi ...

  7. Educational Codeforces Round 14 B. s-palindrome 水题

    B. s-palindrome 题目连接: http://www.codeforces.com/contest/691/problem/B Description Let's call a strin ...

  8. Educational Codeforces Round 14 A. Fashion in Berland 水题

    A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...

  9. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

随机推荐

  1. 20145320 《Java程序设计》第10周学习总结

    20145320 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 计算机网络概述 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 按照计算机网络的定义 ...

  2. php操作Mysql 以及封装常用的函数 用外连接连接3个表的案例

    <?php header("content-type;text/html;charset=utf-8"); //数据库连接define('DB_HOST','localhos ...

  3. 【Selenium】4.创建你的第一个Selenium IDE脚本

    http://newtours.demoaut.com/ 这个网站将会用来作为我们测试的网址. 通过录制来创建一个脚本 让我们来用最普遍的方法——录制来创建一个脚本.然后,我们将会用回放的功能来执行录 ...

  4. cocos2d-x使用AssetsManager类实现资源的在线更新

    从2.1.2版本开始,2dx在libExtensions下添加了一个AssetsManager类用于资源的在线更新和简单的版本管理,同时添加了AssetsManagerTest项目示范了AssetsM ...

  5. 利用Java进行MySql数据库的导入和导出

    利用Java来进行Mysql数据库的导入和导出的总体思想是通过Java来调用命令窗口执行相应的命令. MySql导出数据库的命令如下: mysqldump -uusername -ppassword  ...

  6. innerHTML

    对于innerHTML 属性,几乎所有的元素都有innerHTML属性,它是一个字符串,用来设置或获取位于对象起始和结束标签内的HTML.(获取HTML当前标签的起始和结束里面的内容) 下面的例子返回 ...

  7. 学习Uml开始

    Um的全称是 Unified Modeling Language, 统一建模语言,uml可以帮助我们做软件需求和软件设计的工作, 1.1UML的定义 UML是一种通用的可视化建模语言,是一种标准化的用 ...

  8. ECMAScript 6教程 (三) Class和Module(类和模块)

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文连接,博客地址为 http://www.cnblogs.com/jasonnode/ .该系列课程是 ...

  9. ubuntu 如何 su 到 root(作为 root 用户操作)

    ubuntu 安装后,root用户默认被锁定,不允许登录,也不允许"su"到 root.对于桌面用户来说,这样安全性更高一些,但对于服务器可以设置成"允许 su 到roo ...

  10. SSI-Server Side Inclued

    SSI是指将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本.图形或应用程序信息包含到网页中. IIS.Apache等主流web服务器都支持,cassini不支持.它并不经过asp ...