You are not logged in. Please login at www.codechef.com to post your questions!

×

Union Find On Codeforces

Can you help me with this problem here is my solution getting wa / I have used union find method `

#include<bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
int i,j,k=0,d,n=0,m,a,b,c,q,l,r;
cin>>n;
vector<int> x[1001],y[1001];
int arr[n+1];
for(i=0;i<=n;i++)
    arr[i]=i;
for(i=1;i<=n;i++){
    cin>>a>>b;
    if(x[a].size())
        arr[i]=x[a][0];
    else if(y[b].size())
        arr[i]=y[b][0];
    else {
        x[a].emplace_back(i);
        y[b].emplace_back(i);
    }

}
    for(i=1;i<=n;i++)
        if(arr[i]==i)
            k++;
cout<<k-1<<"\n";

return 0;
}

ps. I don't know why codechef has so pathetic code sharing . use this linnk as an alternative for my solution code .

asked 11 Dec '18, 21:29

harshraj22's gravatar image

3★harshraj22
12
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,197
×655
×121

question asked: 11 Dec '18, 21:29

question was seen: 65 times

last updated: 11 Dec '18, 21:29