Algorithm
Problem link- https://www.spoj.com/problems/IITKWPCI/
IITKWPCI - Find Lexicographically Smallest Permutation
You are given n numbers a1, a2, ... an. You have to permute the numbers in such a way that resulting permutation should be lexicographically smallest . But there is a problem, you can not swap every pair of numbers. You can only swap the position i and j if they are good position. You will be given m pairs of i and j's which will denote good positions.
So complying to restrictions posed here, find the lexicographically smallest permutation of a1, a2, , , an.
Definition: (a1, a2, ... an) is lexicographically smaller than (b1, b2, ... bn) if first index i where ai and bi differs, ai < bi satisfies.
eg. (1, 2, 3, 4) is smaller than (2, 1, 3, 4)
Input
T : number of test cases (T <= 10)
Next Line will contain n and m. (1 <= n <= 10^3 and 0 <= m <= min (n * (n - 1) / 2, 10^5).
Next Line will contains a1, a2, ... an. (a[i] >= 1 && a[i] <=10^6)
For next m lines, each line will contain i, j separated by space which will denote that you can swap ai and aj.
Output
For each test case, output n numbers representing the permutation of a1, a2, ... an according to problem statement.
Example
Input: 2
3 1
3 2 1
2 3
4 2
2 4 3 1
1 3
3 4
Output: 3 1 2
1 4 2 3
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007LL
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI 3.14159265358979323846
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
const int MAXN = 1e6+5;
int n, m;
int parent[MAXN];
int size[MAXN];
deque<int> roots[MAXN];
void init(){
for(int i = 1;i <= 1000000; i++){
parent[i] = i;
size[i] = 1;
roots[i].clear();
}
}
int root(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
bool find(int a, int b){
return root(a) == root(b);
}
void union1(int a, int b){
int root_a = root(a);
int root_b = root(b);
if(root_a == root_b)
return;
if(size[root_a] < size[root_b]){
parent[root_a] = root_b;
size[root_b] += size[root_a];
}else{
parent[root_b] = root_a;
size[root_a] += size[root_b];
}
}
int main(){
io;
int t;
cin >> t;
while(t--){
cin >> n >> m;
init();
int arr[n];
for(int i = 0;i < n; i++)
cin >> arr[i];
while(m--){
int i, j;
cin >> i >> j;
i--;j--;
union1(arr[i], arr[j]);
}
for(int i = 0;i < n; i++){
int root_i = root(arr[i]);
roots[root_i].push_back(arr[i]);
}
for(int i = 1;i <= 1000000; i++)
sort(roots[i].begin(), roots[i].end());
for(int i = 0;i < n; i++){
int root_i = root(arr[i]);
cout << roots[root_i].front() << " ";
roots[root_i].pop_front();
}
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
3 1
3 2 1
2 3
4 2
2 4 3 1
1 3
3 4
Output
1 4 2 3
Demonstration
SPOJ Solution-Find Lexicographically Smallest Permutation-Solution in C, C++, Java, Python