link to question is Harmo and tools first join group ACM-OI if u face any difficulities in accessing problem.
My Code:[submission:13129913]
#include<bits/stdc++.h>
#define ll long long int
#define inp(x) scanf("%lld",&x)
using namespace std;
struct node{
ll d;
node * next;
};
struct hdnode{
node * next;
};
struct ds{
hdnode * head;
node * tail;
};
ds *a;
static ll szt[100009];
inline void pre(ll n){
a = new ds [100009];
for(ll i=1;i<=n;++i){
a[i].head = new hdnode;
a[i].tail = new node;
(a[i].tail)->d = i;
(a[i].tail)->next = NULL;
(a[i].head)->next = a[i].tail;
}
return ;
}
inline void finds(ll n){
for(ll i=1;i<=n;++i){
if(a[i].head!=NULL){
node *temp = new node;
temp = (a[i].head)->next;
while(temp!=NULL){
szt[temp->d] = i;
temp = temp->next;
}
}
}
return ;
}
inline void merges(ll s,ll t){
if(a[s].head == NULL){
return ;
}
(a[t].tail)->next = (a[s].head)->next;
a[s].head = NULL;
a[t].tail = a[s].tail;
//a[s].tail = NULL;
return ;
}
int main(){
ll n,q,s,t;
inp(n);inp(q);
pre(n);
while(q--){
inp(s);inp(t);
merges(s,t);
/* snippet for checking
for(ll i=1;i<=n;++i){
cout<<i<<" : ";
if(a[i].head!=NULL){
node *temp = new node;
temp = (a[i].head)->next;
while(temp!=NULL){
cout<<temp->d<<" ";
temp = temp->next;
}
}
cout<<endl;
}*/
}
finds(n);
for(ll i=1;i<=n;++i){
printf("%lld ",szt[i]);
}
return 0;
}