1. 程式人生 > >16大連區域賽 現場賽 A— Wrestling Match【2-sat】

16大連區域賽 現場賽 A— Wrestling Match【2-sat】

Wrestling Match

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 711    Accepted Submission(s): 184  

Problem Description

Nowadays, at least one wrestling match is held every year in our country. There are a lot of people in the game is "good player”, the rest is "bad player”. Now, Xiao Ming is referee of the wrestling match and he has a list of the matches in his hand. At the same time, he knows some people are good players,some are bad players. He believes that every game is a battle between the good and the bad player. Now he wants to know whether all the people can be divided into "good player" and "bad player".

Input

Input contains multiple sets of data.For each set of data,there are four numbers in the first line:N (1 ≤ N≤ 1000)、M(1 ≤M ≤ 10000)、X,Y(X+Y≤N ),in order to show the number of players(numbered 1toN ),the number of matches,the number of known "good players" and the number of known "bad players".In the next M lines,Each line has two numbersa, b(a≠b) ,said there is a game between a and b .The next line has X different numbers.Each number is known as a "good player" number.The last line contains Y different numbers.Each number represents a known "bad player" number.Data guarantees there will not be a player number is a good player and also a bad player.

Output

If all the people can be divided into "good players" and "bad players”, output "YES", otherwise output "NO".

Sample Input

5 4 0 0

1 3

1 4

3 5

4 5

5 4 1 0

1 3

1 4

3 5

4 5

2

Sample Output

NO

YES

題意:

n個人,m場比賽,每一場比賽必有一個好的,一個壞的。

如果存在一個人既是好的又是壞的,那麼輸出NO【一定注意大寫】

如果比賽中沒有出現,並且也沒有提示是好的壞的,那麼輸出NO【一定注意大寫】

否則YES

分析:

這個題其實就是暴力深搜,也算是2-sat的思想,我直接套的模板,但是沒有注意輸出格式,wrong了好多發。

程式碼:

#include<bits/stdc++.h>

using namespace std;
const int maxn=10000+10;
bool vis[maxn*2];
struct TwoSAT
{
    int n;//原始圖的節點數(未翻倍)
    vector<int> G[maxn*2];//G[i]==j表示如果mark[i]=true,那麼mark[j]也要=true
    bool mark[maxn*2];//標記
    int S[maxn*2],c;//S和c用來記錄一次dfs遍歷的所有節點編號

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    //加入(x,xval)或(y,yval)條件
    //xval=0表示假,yval=1表示真
    void add_clause(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }

    //從x執行dfs遍歷,途徑的所有點都標記
    //如果不能標記,那麼返回false
    bool dfs(int x)
    {
        if(mark[x^1]) return false;//這兩句的位置不能調換
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;i<G[x].size();i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }

    //判斷當前2-SAT問題是否有解
    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        if(!mark[i] && !mark[i+1])
        {
            c=0;
            if(!dfs(i))
            {
                while(c>0) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}fun;

int main()
{
    int i,j,x,y,aa,bb,m,n;
    while(~scanf("%d%d%d%d",&n,&m,&x,&y))
    {
        fun.init(n);
        memset(vis,0,sizeof(vis));
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&aa,&bb);
            aa--;
            bb--;
            fun.add_clause(aa,1,bb,1);
            fun.add_clause(aa,0,bb,0);
            vis[aa]=1;
            vis[bb]=1;

        }
        for(i=0;i<x;i++)
        {
            scanf("%d",&aa);
            aa--;
            vis[aa]=1;
            fun.add_clause(aa,1,aa,1);
        }
        for(i=0;i<y;i++)
        {
            scanf("%d",&aa);
            aa--;
            vis[aa]=1;
            fun.add_clause(aa,0,aa,0);
        }
        bool flag;
        flag=-1;

        for(i=0;i<n;i++)
        {
            if(vis[i]==0)
            {
                flag=0;
                break;
            }
        }
        if(flag==0)
        {
            printf("NO\n");
            continue;
        }
        flag=fun.solve();
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");

    }

}