-
Notifications
You must be signed in to change notification settings - Fork 1
/
IITKWPCI.cpp
98 lines (81 loc) · 1.23 KB
/
IITKWPCI.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
vector<vector<int> > graph(1001);
int traverse[1001];
int n;
void resetgraph(){
for (int i = 0; i < 1001; i++)
{
traverse[i]=0;
vector<int> x;
graph[i]=x;
}
}
void DFS(int t,vector<int> &a)
{
if(traverse[t]==1)
return;
a.push_back(t);
traverse[t]=1;
for (int i = 0; i < graph[t].size(); i++)
{
if(traverse[graph[t][i]]==0){
DFS(graph[t][i],a);
}
}
}
void sortme(int *a,vector<int> &x){
vector<int> b(x.size()),c(x.size());
for (int i = 0; i < x.size(); i++)
{
b[i]=a[x[i]];
c[i]=x[i];
}
sort(b.begin(),b.end());
sort(c.begin(),c.end());
for (int i = 0; i < b.size(); i++)
a[c[i]]=b[i];
{
}
}
void sortg(int* a)
{
for (int i = 0; i < n; i++)
{
if(traverse[i]!=1){
vector<int> x;
DFS(i,x);
sortme(a,x);
}
}
}
int main(){
int t,m,t1,t2;
cin>>t;
while(t--)
{
resetgraph();
cin>>n>>m;
int a[n];
for (int i = 0; i < n; i++)
{
cin>>a[i];
}
for (int i = 0; i < m; i++)
{
cin>>t1>>t2;
graph[t1-1].push_back(t2-1);
graph[t2-1].push_back(t1-1);
}
sortg(a);
for (int i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}