-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ide (21).cpp
109 lines (94 loc) · 2.2 KB
/
Ide (21).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
99
100
101
102
103
104
105
106
107
108
109
/// ASSIGNMENT 3 QUESTION 1
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N= 10e5;
int parent[N];
int si[100];
int x=1;
void make_set(int v) { //making set
parent[v] = v;
si[v] = x;
}
int find_set(int v) { // finding a particular value in the set
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) { // taking usnion of the sets by rank
a = find_set(a);
b = find_set(b);
if (a != b) {
if (si[a] < si[b])
swap(a, b);
parent[b] = a;
si[a] = si[a]+ si[b];
}
}
int main() {
char c;
int x,n,m,prev,cur,ans,ans1,x1,flag=0;
// your code goes here
cin>>m>>n;
for(int i=0;i<n;i++)
{
/*cin>>c;
cout<<c<<" ";*/
cin>>c;
//cout<<c<< " ";
if(c=='u')
{
/*cin>>c;
cout<<c<< " ";*/cin>>c;
//cout<<c<< " ";
cin>>c;
//cout<<c<< " ";
cin>>c;
//cout<<c<< " ";
cin>>c;
//cout<<c<< " ";
cin>>cur;
//cout<<cur<<"*";
make_set(cur);
cin>>x;
// cout<<x<<"*";
make_set(x);
if(flag==1)
union_sets(prev, cur);
flag=1;
prev=cur;
}
if(c=='g')
{
cin>>c>>c;
cin>>x;
cin>>x1;
//ans1= find_set(x1);
if(x!=0&&x1!=0)
{if ( find_set(x1)==0 || find_set(x)==0) // if the value is not found it gives the ans zero
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;}
//cases when if the value to be found is itself zero
else if(x==0&&x1!=0)
{if ( find_set(x1)!=0 && find_set(x)==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else if(x1==0&&x!=0)
{if ( find_set(x)!=0 && find_set(x1)==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else if(x==0&&x1==0)
{if ( find_set(x1)==0 && find_set(x)==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
}
return 0;
}