1272. Non-Yekaterinburg Subway

http://acm.timus.ru/problem.aspx?space=1&num=1272

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<vector>



using namespace std;

const int INF=0x3f3f3f3f;

const int N=10005;

int f[N];

int findx(int x)

{

    if(f[x]!=x)

    f[x]=findx(f[x]);

    return f[x];

}

int main()

{

    int n,k,m;

    while(scanf("%d %d %d",&n,&k,&m)!=EOF)

    {

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

        f[i]=i;

        while(k--)

        {

            int l,r;

            scanf("%d %d",&l,&r);

            f[findx(l)]=findx(r);

        }

        int ans=0;

        while(m--)

        {

            int l,r;

            scanf("%d %d",&l,&r);

            if(findx(l)!=findx(r))

            {++ans;f[findx(l)]=r;}

        }

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

    }

    return 0;

}

 

你可能感兴趣的:(in)