Skip to content

Instantly share code, notes, and snippets.

@sofhiasouza
Last active June 25, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofhiasouza/b3253b3d815246857757808cee912c5a to your computer and use it in GitHub Desktop.
Save sofhiasouza/b3253b3d815246857757808cee912c5a to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int n, k, comp[maxn];
int busca(int x)
{
if(comp[x] == x) return x;
return comp[x] = busca(comp[x]);
}
void join(int a, int b)
{
if(a > b) swap(a, b);
comp[busca(b)] = busca(a);
}
bool ok(int a, int b)
{
if(busca(a) == busca(b)) return true;
return false;
}
int main()
{
cin >> n >> k;
for(int i = 1 ; i <= n ; i++) comp[i] = i;
for(int i = 0 ; i < k ; i++)
{
char x;
int a, b;
cin >> x >> a >> b;
if(x == 'C')
{
if(!ok(a, b)) join(a, b);
}
else
{
if(!ok(a, b)) cout << "NAO\n";
else cout << "SIM\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment