Pagini recente » Cod sursa (job #2793991) | Istoria paginii agm-2018/runda1 | Cod sursa (job #1823517) | Cod sursa (job #3296134) | Cod sursa (job #1700777)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cctype>
#define pb push_back
#define mp make_pair
#define MAXN 100001
#define DIM 8192
using namespace std;
class Reader
{
public:
Reader(const char* filename)
{
f.open(filename);
pos = 0;
f.read(buffer,DIM);
}
inline Reader& operator >>(int& x)
{
x=0;
sgn=1;
while((buffer[pos]<'0'||buffer[pos]>'9')&&buffer[pos]!='-')
if(++pos==DIM)
f.read(buffer,DIM),pos=0;
if(buffer[pos]=='-')sgn=-1,pos++;
while(buffer[pos]>='0'&&buffer[pos]<='9')
{
x=x*10+buffer[pos]-'0';
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
x*=sgn;
return (*this);
}
~Reader()
{
f.close();
}
private:
ifstream f;
int pos,aux,sgn;
char buffer[DIM];
};
Reader f("dfs.in");
ofstream g("dfs.out");
int n,m,t[MAXN],h[MAXN],i,a,b,nr,A,B;
int tata(int nod)
{
int nodd=nod,aux;
while(t[nod]!=nod)nod=t[nod];
while(t[nodd]!=nod)aux=t[nodd],t[nodd]=nod,nodd=aux;
return nod;
}
int main()
{
ios_base::sync_with_stdio(false);
f>>n>>m;
for(i=1;i<=n;i++)t[i]=i;
while(m--)
{
f>>A>>B;
a=tata(A);
b=tata(B);
if(a!=b)
{
nr++;
if(h[a]>h[b])t[b]=a;
else t[a]=b;
if(h[a]==h[b])h[a]++;
}
}
g<<n-nr;
return 0;
}