stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cstdio>

#include <vector>

#include <cstring>

#include <cstdlib>



void numbering(int id, int *g, int num, std::vector<int> *a){

    int tmp;

    for(int i=0;i<a[id].size();i++){

        tmp = a[id][i];

        if(g[tmp])continue;

        g[tmp] = num;

        numbering(tmp, g, num, a);

    }

}



void grouping(std::vector<int> *a, int *g, int n){

    int num = 1;

    for(int i=0;i<n;i++){

        if(g[i])continue;

        g[i] = num;

        numbering(i,g,num,a);

        num++;

    }

}



int main(){

    int i, n, m, q, s, t;

    int *g;

    scanf("%d %d",&n,&m);

    g = (int *)malloc(sizeof(int)*n);

    memset(g, 0, sizeof(int)*n);

    std::vector<int> a[n];

    for(i=0;i<m;i++){

        scanf("%d %d",&s,&t);

        a[s].push_back(t);

        a[t].push_back(s);

    }

    scanf("%d",&q);

    grouping(a,g,n);

    for(i=0;i<q;i++){

        scanf("%d %d",&s,&t);

        if(g[s]==g[t])printf("yes\n");

        else printf("no\n");

    }

    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 4.825893009492512x):
#include <cstdio>

#include <cstdlib>



typedef struct{

    int height;

    int parent;

}node;



node* nd;



int top(int key){

    if(nd[key].parent == -1)return key;

    else return top(nd[key].parent);

}



void join(int s, int t){

    int a = top(s);

    int b = top(t);

    if(a==b)return;

    if(nd[a].height<nd[b].height){

        nd[a].parent = b;

    }else{

        nd[b].parent = a;

        if(nd[a].height==nd[b].height)nd[a].height++;

    }

    return;

}



bool isFriend(int s, int t){

    if(top(s)==top(t))return true;

    else return false;

}



int main(){

    int i, n, m, s, t, q;

    scanf("%d %d",&n,&m);



    nd = (node *)malloc(sizeof(node)*n);



    for(i=0;i<n;i++){

        nd[i].height = 1;

        nd[i].parent = -1;

    }



    for(i=0;i<m;i++){

        scanf("%d %d",&s,&t);

        join(s,t);

    }



    scanf("%d",&q);

    for(i=0;i<q;i++){

        scanf("%d %d",&s,&t);

        if(isFriend(s,t))printf("yes\n");

        else printf("no\n");

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <map>

#include <vector>

using namespace std;



int main(){

	int n,a,b;

	bool flag;

	string number;

	map<string,int> info;

	vector<string> seq;

	

	while(cin >> n && n){

		info.clear();

		seq.clear();

		while(n--){

			cin >> number >> a >> b;

			if(!info[number])seq.push_back(number);

			while(b-- && info[number]<1000000){

				info[number] += a;

			}

		}

		flag = true;

		for(int i=0;i<seq.size();i++){

			if(info[seq[i]]>=1000000){

				flag = false;

				cout << seq[i] << endl;

			}

		}

		if(flag)cout << "NA" << endl;

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.1666468366352485x):
#include <iostream>

#include <map>

#include <vector>

using namespace std;



int main(){

	int n,a,b;

	bool flag;

	int number;

	map<int,int> info;

	vector<int> seq;

	

	while(cin >> n && n){

		info.clear();

		seq.clear();

		while(n--){

			cin >> number >> a >> b;

			if(!info[number])seq.push_back(number);

			while(b-- && info[number]<1000000){

				info[number] += a;

			}

		}

		flag = true;

		for(int i=0;i<seq.size();i++){

			if(info[seq[i]]>=1000000){

				flag = false;

				cout << seq[i] << endl;

			}

		}

		if(flag)cout << "NA" << endl;

	}

}
// Slower C++ solution:
#include <iostream>

#include <map>

#include <vector>

using namespace std;



int main(){

	int n,a,b;

	bool flag;

	string number;

	map<string,int> info;

	vector<string> seq;

	

	while(cin >> n && n){

		info.clear();

		seq.clear();

		while(n--){

			cin >> number >> a >> b;

			if(!info[number])seq.push_back(number);

			while(b-- && info[number]<1000000){

				info[number] += a;

			}

		}

		flag = true;

		for(int i=0;i<seq.size();i++){

			if(info[seq[i]]>=1000000){

				flag = false;

				cout << seq[i] << endl;

			}

		}

		if(flag)cout << "NA" << endl;

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.20263276155154x):
#include <iostream>

#include <map>

#include <vector>

using namespace std;



int main(){

	long long n,a,b;

	bool flag;

	int number;

	map<int,long long> info;

	vector<int> seq;

	

	while(cin >> n && n){

		info.clear();

		seq.clear();

		while(n--){

			cin >> number >> a >> b;

			if(!info[number])seq.push_back(number);

			info[number] += a*b;

		}

		flag = true;

		for(int i=0;i<seq.size();i++){

			if(info[seq[i]]>=1000000){

				flag = false;

				cout << seq[i] << endl;

			}

		}

		if(flag)cout << "NA" << endl;

	}

}
// Slower C++ solution:
#include <iostream>

#include <string>

using namespace std;

typedef struct{

	string id;

	long long sales;

} staff;



int main(void)

{

	long long n;

	while(cin>>n){

		if(!n) break;

		staff staff1[4001];

		int pt = 0;

		for(int i=0; i< n; i++){

			string id;

			cin >> id;

			long long price;

			int bnum;

			cin >> price >> bnum;

			int m=-1;

			for(int j=0;j < i; j++){

				if(id==staff1[j].id){

					pt++;

					m=j;

					break;

				}

			}

			if(m==-1){

				staff1[i-pt].id = id;

				staff1[i-pt].sales = price * bnum;

			}else{

				staff1[m].sales += price * bnum;

			}

		}

		int count = 0;

		for(int i=0; i < n-pt; i++){

			if(staff1[i].sales >= 1000000){

				count++;

				cout << staff1[i].id << endl;

			}

		}

		if(!count) cout << "NA" << endl;

	}

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.166067636859379x):
#include<iostream>

#include<map>

#include<vector>

#include<string>



using namespace std;

#define mp make_pair

int main(){

  int n;

  string a;

  long long b,c;

  map<string,long long> data;

  vector<string> id;

  while(true){

    id.clear();

    data.clear();

    cin>>n;

    if(!n)

      break;

    for(int i=0;i<n;i++){

      cin>>a>>b>>c;

      c*=b;

      if(data.find(a)==data.end()){

	data.insert(mp(a,c));

	id.push_back(a);

      }

      else{

	data[a]+=c;

      }

    }

    bool empty = true;

    for(int i=0;i<id.size();i++){

      if(data[id[i]]>=1000000){

	cout<<id[i]<<endl;

	empty = false;

      }

    }

    if (empty)

      cout<<"NA"<<endl;



  }

  return 0;

}
// Slower C++ solution:
#include<iostream>

#include<iomanip>

#include<cmath>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<set>

#include<map>

#include<queue>

#include<stack>

#include<vector>

#include<cctype>

#define szxb 200010

using namespace std;

int n;

long long a[szxb];

int main()

{

	memset(a,0,sizeof(a));

	scanf("%d",&n);

	int t;

	long long total=0;

	for(register int i=1;i<=n;i++)

	{

		scanf("%d",&t);

		total+=t,a[i]=(a[i-1]+t);

	}

	long long minn=1e17;

	for(register int i=2;i<=n;i++)

	{

		minn=(min(minn,abs(total-(a[i-1]*2))));

	}

	printf("%lld",minn);

	putchar(10);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 13.582102439806484x):
#include<cstdio>

#include<cstdlib>

int n;

long long int sum,min,x;

int a[200000];

int main()

{

	scanf("%d",&n);

	for(int i=0;i<n;i++)

	  scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	  sum+=a[i];

	min=labs(sum-2*a[0]);

	for(int i=0;i<n-1;i++)

	{

		x+=a[i];

		if(labs(sum-2*x)<min)

		  min=abs(sum-2*x);

	}

	printf("%d\n",min);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<iomanip>

#include<cmath>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<set>

#include<map>

#include<queue>

#include<stack>

#include<vector>

#include<cctype>

#define szxb 200010

using namespace std;

int n;

long long a[szxb];

int main()

{

	memset(a,0,sizeof(a));

	scanf("%d",&n);

	int t;

	long long total=0;

	for(register int i=1;i<=n;i++)

	{

		scanf("%d",&t);

		total+=t,a[i]=(a[i-1]+t);

	}

	long long minn=1e17;

	for(register int i=2;i<=n;i++)

	{

		minn=(min(minn,abs(total-(a[i-1]*2))));

	}

	printf("%lld",minn);

	putchar(10);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.592060708027512x):
#include<bits/stdc++.h>

using namespace std;

long long a[200010];

long long b[200010];



int main(){

	int c;

	cin>>c;

	long long s=0;

	for(int i=0;i<c;i++){

		cin>>a[i];

		if(i==0)

			b[i]=a[i];

		else

			b[i]=b[i-1]+a[i];

		s=s+a[i];

	}

	long long m;

	if(b[0]>s-b[0])

		m=b[0]+b[0]-s;

	else

		m=s-b[0]-b[0];

	for(int i=1;i<(c-1);i++){

		if(m>abs(s-b[i]-b[i]))

		m=abs(s-b[i]-b[i]);

	}

	cout<<m<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<iomanip>

#include<cmath>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<set>

#include<map>

#include<queue>

#include<stack>

#include<vector>

#include<cctype>

#define szxb 200010

using namespace std;

int n;

long long a[szxb];

int main()

{

	memset(a,0,sizeof(a));

	scanf("%d",&n);

	int t;

	long long total=0;

	for(register int i=1;i<=n;i++)

	{

		scanf("%d",&t);

		total+=t,a[i]=(a[i-1]+t);

	}

	long long minn=1e17;

	for(register int i=2;i<=n;i++)

	{

		minn=(min(minn,abs(total-(a[i-1]*2))));

	}

	printf("%lld",minn);

	putchar(10);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.5680660061022436x):
#include <bits/stdc++.h>

 

using namespace std;

 

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	int n;

	cin >> n;

	int a[n];

	long long sum = 0;

	for(int i = 0; i < n; i++) cin >> a[i], sum += a[i];

	long long ans = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;

	for(int i = 0; i < n - 1; i++) sum -= 2 * a[i], ans = min(ans, llabs(sum));

	cout << ans;

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.53218890552342x):
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.5512183155500967x):
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 20000000000;//pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.5269591977410246x):
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N;//,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 2 * 10000000000;//pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        //cin >> a;

        cin >> sum[i];

        sum[i] += sum[i - 1];// + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.526864160851617x):
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;

void amin(long long  &x,long long  y)

{

    if ( y < x ) {

        x   =   y;

    }

}



int main()

{

    int N;//,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 2 * 10000000000;//pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        //cin >> a;

        cin >> sum[i];

        sum[i] += sum[i - 1];// + a;

    }

    

    for ( int i = 1; i < N; i++) {

        //min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

        amin(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.5269591977410246x):
#include <iostream>

//#include <string>

#include <algorithm>

#include <cmath>

//#include <vector>

//#include <limits>



using namespace std;

/*void amin(long long  &x,long long  y)

{

    if ( y < x ) {

        x   =   y;

    }

}*/



int main()

{

    int N;//,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 2 * 10000000000;//pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        //cin >> a;

        cin >> sum[i];

        sum[i] += sum[i - 1];// + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

        //amin(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <algorithm>

#include <cmath>

#include <vector>

#include <limits>



using namespace std;



int main()

{

    int N,a;

    long long sum[200001] = {};

    long long min_num = 2 * pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        cin >> a;

        sum[i] = sum[i - 1] + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - sum[i] - sum[i]));

    }

    cout << min_num << '\n';



}

// Faster C++ solution for the same problem (measured speedup: 2.495519092873311x):
#include <iostream>

//#include <string>

#include <algorithm>

#include <cmath>

//#include <vector>

//#include <limits>



using namespace std;

/*void amin(long long  &x,long long  y)

{

    if ( y < x ) {

        x   =   y;

    }

}*/



int main()

{

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);



    int N;//,a;

    long long sum[200001];

    sum[0] = 0;

    long long min_num = 2 * 10000000000;//pow(10, 9.0);



    cin >> N;



    for (int i = 1; i <= N; i++) {

        //cin >> a;

        cin >> sum[i];

        sum[i] += sum[i - 1];// + a;

    }

    

    for ( int i = 1; i < N; i++) {

        min_num = min(min_num, abs(sum[N] - (2 * sum[i])));

        //amin(min_num, abs(sum[N] - (2 * sum[i])));

    }

    cout << min_num << '\n';



}
// Slower C++ solution:
#include<bits/stdc++.h>

#include<iostream>

using namespace std;

long long int a[500000];



int main()

{

    long long int i,n,j,k,l=1000000000000,sm=0;

    scanf("%lld",&n);

    for(i=1; i<=n; i++)

    {

        scanf("%lld",&k);

        a[i]=a[i-1]+k;

        sm+=k;

    }

    if(n==2)

    {

        l=abs(sm-2*a[1]);

    }

    else

    {

        for(i=1; i<n; i++)

        {

            l=min(l,abs(sm-2*a[i]));

        }

    }

    printf("%lld",l);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.241217999743294x):
#pragma GCC optimize(3)

#pragma GCC optimize("Ofast")

#pragma GCC optimize("inline")

#pragma GCC optimize("-fgcse")

#pragma GCC optimize("-fgcse-lm")

#pragma GCC optimize("-fipa-sra")

#pragma GCC optimize("-ftree-pre")

#pragma GCC optimize("-ftree-vrp")

#pragma GCC optimize("-fpeephole2")

#pragma GCC optimize("-ffast-math")

#pragma GCC optimize("-fsched-spec")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize("-falign-jumps")

#pragma GCC optimize("-falign-loops")

#pragma GCC optimize("-falign-labels")

#pragma GCC optimize("-fdevirtualize")

#pragma GCC optimize("-fcaller-saves")

#pragma GCC optimize("-fcrossjumping")

#pragma GCC optimize("-fthread-jumps")

#pragma GCC optimize("-funroll-loops")

#pragma GCC optimize("-fwhole-program")

#pragma GCC optimize("-freorder-blocks")

#pragma GCC optimize("-fschedule-insns")

#pragma GCC optimize("inline-functions")

#pragma GCC optimize("-ftree-tail-merge")

#pragma GCC optimize("-fschedule-insns2")

#pragma GCC optimize("-fstrict-aliasing")

#pragma GCC optimize("-fstrict-overflow")

#pragma GCC optimize("-falign-functions")

#pragma GCC optimize("-fcse-skip-blocks")

#pragma GCC optimize("-fcse-follow-jumps")

#pragma GCC optimize("-fsched-interblock")

#pragma GCC optimize("-fpartial-inlining")

#pragma GCC optimize("no-stack-protector")

#pragma GCC optimize("-freorder-functions")

#pragma GCC optimize("-findirect-inlining")

#pragma GCC optimize("-fhoist-adjacent-loads")

#pragma GCC optimize("-frerun-cse-after-loop")

#pragma GCC optimize("inline-small-functions")

#pragma GCC optimize("-finline-small-functions")

#pragma GCC optimize("-ftree-switch-conversion")

#pragma GCC optimize("-foptimize-sibling-calls")

#pragma GCC optimize("-fexpensive-optimizations")

#pragma GCC optimize("-funsafe-loop-optimizations")

#pragma GCC optimize("inline-functions-called-once")

#pragma GCC optimize("-fdelete-null-pointer-checks")

#include <cstdio>

#define ll long long

#define abs(a) ((a)>0?(a):(-a))

#define min(a,b) ((a)<(b)?(a):(b))

using namespace std;

ll n,a[210000],tot,ans=922372036854775805;

inline int read()

{

    int x=0,f=1;char ch=getchar();

    for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;

    for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';

    return x*f;

}

int main()

{

	n=read();

	for(register ll i=1; i<=n; ++i) a[i]=read(),tot+=a[i];	 

	for(register ll i=1; i<=n-1; ++i)

		tot-=2*a[i],ans=min(ans,(ll)abs(tot));

	printf("%lld\n",ans);	

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



vector<int> adj[100005];

vector<bool> vis(100005, false);



void dfs(int i){

	if(!vis[i]) {

		vis[i] = true;

		for(auto v : adj[i])

			dfs(v);

	}

}



void solve() {

	int n, m;

	cin >> n >> m;

	for(int i=0; i<m; i++) {

		int x, y;

		cin >> x >> y;

		--x;

		--y;

		adj[x].push_back(y);

		adj[y].push_back(x);

	}

	int comp = 0;

	for (int i=0; i<n; i++) {

		if(!vis[i]) {

			comp++;

			dfs(i);

		}

	}

	cout << comp-1;

}



int main() {



	solve();	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.460047376180677x):
#include <bits/stdc++.h>

 

typedef long long ll;

 

using namespace std;

 

vector<int> par;

 

int find(int n){

	if(par[n]==n)return n;

	else return par[n]=find(par[n]);

}

 

void uni(int a,int b){

	a=find(a);

	b=find(b);

	if(a!=b)par[b]=a;

}

 

int main(){

	//freopen("input.in","r",stdin);

	//freopen("output.in","w",stdout);

	ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int i,j,n,m,a,b;

    cin>>n>>m;

    par=vector<int>(n+1);

    for(i=1;i<=n;i++)par[i]=i;

    while(m--){

    	cin>>a>>b;

    	uni(a,b);

    }

    set<int> s;

    for(i=1;i<=n;i++)s.insert(find(i));

    cout<<s.size()-1;

 

 

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ii = pair<int, int>;

using iii = pair<int, ii>;

#define vt vector

#define pq priority_queue

#define pb push_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second

const int MOD = 1e9 + 7;

const int INF = 2e9;

const int N = 1e5 + 5;



int n, m, a, b, ans;

bool vis[N];

vt<int> v[N];



void dfs(int u) {

    if(vis[u]) return;

    vis[u] = true;

    for(int x : v[u]) {

        dfs(x);

    }

}



void solve() {

    int T = 1;

    // cin >> T;

    for(int tc = 1; tc <= T; tc++) {

        cin >> n >> m;

        while(m--) {

            cin >> a >> b;

            v[a].pb(b);

            v[b].pb(a);

        }

        for(int i = 1; i <= n; i++) {

            if(!vis[i]) {

                dfs(i);

                ans++;

            }

        }

        cout << ans - 1 << "\n";

    }

}



int main() {

 

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    

    solve();

    

}

// Faster C++ solution for the same problem (measured speedup: 3.514315417502783x):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ii = pair<int, int>;

using iii = pair<ii, int>;

#define vt vector

#define pq priority_queue

#define pb push_back

#define mp make_pair

#define fi first

#define se second

const int MOD = 1e9 + 7;

const int INF = 2e9;

const int N = 1e5 + 5;



int n, m, a, b, ans;

int par[N];

bool vis[N];



void init() {

    for(int i = 1; i <= n; i++) par[i] = i;

}



int root(int x) {

    return par[x] == x ? x : par[x] = root(par[x]);

}



void join(int x, int y) {

    par[root(x)] = root(y);

}



void solve() {

    int T = 1;

    // cin >> T;

    for(int tc = 1; tc <= T; tc++) {

        cin >> n >> m;

        init();

        while(m--) {

            cin >> a >> b;

            join(a, b);

        }

        for(int i = 1; i <= n; i++) {

            if(!vis[root(i)]) {

                vis[root(i)] = true;

                ans++;

            }

        }

        cout << ans - 1 << "\n";

    }

}



int main() {

 

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    

    solve();

    

}