Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define fast_read ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #define SIZE 100001
- using namespace std;
- ll n,m,q,x,y,par[SIZE],sze[SIZE];
- char ty;
- void init() {
- for (int i = 1;i<=n;i++) {
- sze[i] = 1;
- par[i] = i;
- }
- }
- ll f_root(ll a) {
- while (par[a] != a) {
- par[a] = par[par[a]];
- a = par[a];
- }
- return a;
- }
- void _union(ll a,ll b) {
- ll roota = f_root(a);
- ll rootb = f_root(b);
- if (sze[roota] + sze[rootb] > m || roota == rootb)return;
- if (sze[roota] < sze[rootb]) {
- par[roota] = par[rootb];
- sze[rootb] += sze[roota];
- }
- else {
- par[rootb] = par[roota];
- sze[roota] += sze[rootb];
- }
- }
- string check(ll a,ll b) {
- ll roota = f_root(a);
- ll rootb = f_root(b);
- if (roota == rootb)return "Yes";
- return "No";
- }
- int main()
- {
- cin>>n>>m>>q;
- init();
- while (q--) {
- cin>>ty;
- if (ty == 'A') {
- cin>>x>>y;
- _union(x,y);
- /*cout<<endl;
- for (int i = 1;i<=n;i++)
- cout<<par[i]<<" ";
- cout<<endl;
- for (int i = 1;i<=n;i++)
- cout<<sze[i]<<" ";
- cout<<endl<<endl;*/
- }
- else if (ty == 'E') {
- cin>>x>>y;
- cout<<check(x,y)<<endl;
- }
- else {
- cin>>x;
- cout<<sze[f_root(x)]<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment