-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDSU.cpp
More file actions
46 lines (46 loc) · 964 Bytes
/
DSU.cpp
File metadata and controls
46 lines (46 loc) · 964 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
41
42
43
44
45
46
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=n-1;i>=0;i--)
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
int siz[10005];
int parent[10005];
int find(int a){
while(a!=parent[a])a=parent[a];
return a;
}
void join(int a,int b){
a=find(a);
b=find(b);
if(siz[a]<siz[b]) swap(a,b);
siz[a]+=siz[b];
parent[b]=a;
}
int main(){
io
int n,m,x,y,l;
cin>>n>>m;
for(int i=1;i<=n;i++){
parent[i]=i;
siz[i]=1;
}
for(int i=0;i<m;i++){
cin>>x>>y;
join(x,y);
}
cin>>l;
int arr[l+1];
for(int i=0;i<l;i++){
cin>>arr[i];
arr[i] = find(arr[i]);
}
//displays parent of input nodes
for(int i=0;i<;i++) cout<<arr[i];
}