HDU5971

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
vector<int> e[1010];

int color[1010];
int tst[1010];
int vis[1010];
int ans;
int num[1010];
int fa[1010];
int Find(int x)
{
    int r=x;
    while (fa[r]!=r)
        r=fa[r];
    int i=x,j;
    while(i!=r)
    {
        j=fa[i];
        fa[i]=r;
        i=j;
    }
    return r;
}
void Union(int i,int j)
{
    i=Find(i);
    j=Find(j);
    if(i!=j)
    {
        fa[i]=j;
        num[j]+=num[i];
    }
}
void dfs(int u,int co)
{
    if (ans==0) return ;
    vis[u]=1;
    color[u]=co;
    int nxtco=3-co;
    for (int i=0;i<e[u].size();i++)
    {
        if (color[e[u][i]]&&color[e[u][i]]!=nxtco) ans=0;
        if (!vis[e[u][i]])
        {
            dfs(e[u][i],nxtco);
        }
    }
}
int main()
{
    int n,m,good,bad;
    while (scanf ("%d%d%d%d",&n,&m,&good,&bad)!=EOF)
    {
        ans=1;
        for (int i=1; i<=n; i++)
        {
            fa[i]=tst[i]=i;
            color[i]=vis[i]=0;
            num[i]=1;
            e[i].clear();
        }
        int t1,t2;
        while (m--)
        {
            scanf ("%d%d",&t1,&t2);
            e[t1].push_back(t2);
            e[t2].push_back(t1);
            Union(t1,t2);
        }
        while (good--)
        {
            scanf ("%d",&t1);
            tst[Find(t1)]=t1;
            color[t1]=1;
        }
        while (bad--)
        {
            scanf ("%d",&t1);
            tst[Find(t1)]=t1;
            color[t1]=2;
        }
        for (int i=1;i<=n;i++)
        {
            if (num[Find(i)]==1&&color[i]==0)
            {
                ans=0;
                break;
            }
        }
        for (int i=1;i<=n;i++)
        {
            if (vis[i]==0)
            {
                int thico=1;
                if (color[tst[Find(i)]]!=0)
                    thico=color[tst[Find(i)]];

                dfs(tst[Find(i)],thico);
                if (ans==0) break;
            }
        }
        if (!ans)printf ("NO\n");
        else printf ("YES\n");
    }
    return 0;
}

 

posted on 2017-11-09 13:57  very_czy  阅读(267)  评论(0编辑  收藏  举报

导航