Skip to content

Create TR4 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions TR4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<bits/stdc++.h>
#define max 100001
using namespace std;

int q[max];
int size[max];
int root (int node);
int root(int x)
{
while(x!=q[x])
{
q[x]=q[q[x]];
x=q[x];
}
return x;
}

void connect(int u,int v)
{
int rootu=root(u);
int rootv=root(v);
if(rootu==rootv)
return;
if(size[rootu]<size[rootv])
{
q[rootu]=rootv;
size[rootv]+=size[rootu];
}
else
{
q[rootv]=rootu;
size[rootu]+=size[rootv];
}
}
int main()
{
int n,m,u,v;
set<int> s;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
q[i]=i;
size[i]=1;
}
for(int i=0;i<m;i++)
{
cin>>u>>v;
connect(u,v);
}
for(int i=1;i<=n;i++)
{
if(s.find(root(i))==s.end())
{
s.insert(root(i));
}
}
cout<<s.size()<<endl;

}