Replace the Numbers (并查集 | 倒叙模拟)

E. Replace the Numbers

[Link](Problem - E - Codeforces)

题意

给你一个空的序列,有两种操作 1   x : 从 序 列 最 后 加 入 x 1\ x:从序列最后加入x 1 x:x 2   x   y : 将 当 前 序 列 里 所 有 的 x 变 成 y 2\ x \ y: 将当前序列里所有的x变成y 2 x y:xy。给你 q q q个操作,输出操作后的序列。

思路

并查集维护

如果每次从头到尾遍历修改复杂度太高。因为是对于一个相同数的操作,也就是对于集合的操作,我们可以用并查集来维护(选取集合代表元素),记录每个位置是什么数,每个数在哪个位置,即可。

对于一个新加的数,如果前面出现过就直接将其合并到前面,否则就初始化这个数在的位置。

对于要修改的 x x x y y y,如果 y y y出现过直接将 x x x的代表元素合并到 y y y上,将 x x x置为不存在,如果 y y y没出现过那就将 y y y置为 x x x的位置,将 x x x位置的数改为 y y y,置 x x x不存在即可。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 5e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
	e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
int f[N], vis[N], num[N];
int find(int x) {
	return x == f[x] ? x : f[x] = find(f[x]);
}
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int T; cin >> T;
	for (int i = 1; i <= T; i ++) {
		int op; cin >> op;
		if (op == 1) {
			int x; cin >> x;
			a[++n] = i, f[i] = i, vis[i] = x;
			if (num[x]) 				f[i] = num[x];			
			else num[x] = i;
		}
		else {
			int x, y; cin >> x >> y;
			if (x != y && num[x]) {
				if (num[y]) {
				f[num[x]] = num[y];
				num[x] = 0;
			}
			else {
				num[y] = num[x];
				vis[num[x]] = y;
				num[x] = 0;
			}
			}
		}
	}
	for (int i = 1; i <= n; i ++) cout << vis[find(a[i])] << ' ';
	cout << endl;
	return 0;
}

倒叙性质模拟

因为每个数可能会变多次,所以我们倒着来模拟, f [ y ] : y 变 成 什 么 f[y]:y变成什么 f[y]:y,对于插入操作如果 f [ x ] ! = 0 f[x]!=0 f[x]!=0我们直接插入 f [ x ] f[x] f[x]即可,对于改变操作,如果 f [ y ] = = 0 f[y]==0 f[y]==0代表后面 y y y不会改变, f [ x ] = y f[x]=y f[x]=y即可,如果 f [ y ] ! = 0 f[y]!=0 f[y]!=0代表后面 f [ y ] f[y] f[y]变了 f [ x ] = f [ y ] f[x]=f[y] f[x]=f[y]即可(当前操作被后面影响了),也有点路径压缩的感觉,最后正序输出即可。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 5e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
	e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N][3];
vector<int> ve;
int f[N];
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i][0];
		if (a[i][0] == 1) cin >> a[i][1];
		else cin >> a[i][1] >> a[i][2];
	}

	for (int i = n; i; i --) {
		if (a[i][0] == 1) {
			if (!f[a[i][1]]) ve.push_back(a[i][1]);
			else ve.push_back(f[a[i][1]]);
		}
		else {
			if (!f[a[i][2]]) f[a[i][1]] = a[i][2];
			else 		     f[a[i][1]] = f[a[i][2]];
		}
	} 
	
	reverse(ve.begin(), ve.end());
	for (auto x : ve) cout << x << ' ';
	cout << endl;
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值