-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceTheNumbers2.cpp
More file actions
40 lines (39 loc) · 1007 Bytes
/
ReplaceTheNumbers2.cpp
File metadata and controls
40 lines (39 loc) · 1007 Bytes
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
#include<bits/stdc++.h>
#define fer(i,a,b) for(re i = a; i<b; ++i)
#define re register int
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;
int rep[500001];
int queries[500001][3];
int ans[500001];
int main(){
int q;
cin >> q;
//Initialise the array
for (int i = 0; i < 500001; i++){
rep[i] = i;
}
for (int i = 0; i < q; i++){
cin >> queries[i][0] >> queries[i][1];
if (queries[i][0] == 2) cin >> queries[i][2];
}
int a = 0;
//if corresponding to 1 in the hashmap there comes a different value update
//else print it in the ans
for (int i = q-1; i >= 0; i--){
if (queries[i][0] == 1){
ans[a++] = rep[queries[i][1]];
}
//For 2 we just need to replace x and y
else{
rep[queries[i][1]] = rep[queries[i][2]];
}
}
//Reverse as we are going from top to bottom
while (a--){
cout << ans[a] << " ";
}
cout << endl;
}