HDU-3062-Party(2-SAT)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062

Problem Description

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?

Input

n: 表示有n对夫妻被邀请 (n<= 1000)

m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))

在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2

A1,A2分别表示是夫妻的编号

C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫

夫妻编号从 0 到 n -1

Output

如果存在一种情况 则输出YES

否则输出 NO

Sample Input

2
1
0 1 1 1 

Sample Output

YES

2-SAT入门题

对于(a,a‘),(b,b‘),如果a,b构成仇恨关系,那么如果选a,则必须选b‘,选b则必须选a‘,建边a->b‘,b->a‘表示必须关系,也就是说如果选了连通分量里的一个点,那么其余所有边都必须选,则本体a,a‘在一个连通分量里为非法,只需要判断就好啦。。。

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1e6+7;
struct node
{
    int v,next;
}edge[MAXN];
int head[MAXN],index;
void add_edge(int u,int v)
{
    edge[index].v=v;
    edge[index].next=head[u];
    head[u]=index++;
}
int DFN[MAXN],low[MAXN],stack_[MAXN],in_stack[MAXN],belong[MAXN];
int cir,top,temp;
void Tarjan(int u)
{
    int p;
    DFN[u]=low[u]=++temp;
    in_stack[u]=1;
    stack_[top++]=u;
    for(int i=head[u]; i+1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!DFN[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(in_stack[v])
        {
            low[u]=min(low[u],DFN[v]);
        }
    }
    if(low[u]==DFN[u])
    {
        ++cir;
        do{
            p=stack_[--top];
            in_stack[p]=0;
            belong[p]=cir;
        }while(p!=u);
    }
}
int main()
{
    int n,m;
    while(scanf("%d",&n)!=EOF)
    {
        int a,b,c,d;
        temp=cir=top=index=0;
        memset(DFN,0,sizeof(DFN));
        memset(head,-1,sizeof(head));
        memset(in_stack,0,sizeof(in_stack));
        scanf("%d",&m);
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            a=(a<<1)+c;
            b=(b<<1)+d;
            add_edge(a,b^1);
            add_edge(b,a^1);
        }
        for(int i=0; i<n<<1; ++i)
            if(!DFN[i])
            Tarjan(i);
        int flag=0;
        for(int i=0; i<n; ++i)
        {
            if(belong[i<<1]==belong[(i<<1)^1])
                flag=1;
        }
        if(flag)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}
时间: 2024-08-24 11:36:04

HDU-3062-Party(2-SAT)的相关文章

hdu 2795 Billboard(线段树)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10890    Accepted Submission(s): 4827 Problem Description At the entrance to the university, there is a huge rectangular billboard of

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 1175 连连看 (深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以消掉.还有一个要注意的地方的就是转弯.转弯的次数不超过两次,这两个棋子才可以在棋盘上消去~ 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int

简单的dp hdu 数塔(水题)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21314    Accepted Submission(s): 12808 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

HDU - 3555 Bomb (数位DP)

题意:求1-n里有多少人包含"49"的数字 思路:数位DP,分三种情况:到第i位没有49的情况,到第i位没有49且最高位是9的情况,到第i位有49的情况,将三种情况都考虑进去就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long dp[30][3], n; int arr

HDU 3308 LCIS(线段树)

Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index counting from 0)Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. Input T in the first line, indicating

HDU 3111 Sudoku(精确覆盖)

数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 #include <string> 7 #includ

HDU 3366 Passage (概率DP)

Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. There are only n passages to go out. For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability that Bill will escape from this castle saf