poj 3207(2-SAT)

poj 3207
1、注意加边
2、判两条线段是否重叠的方法

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define db double
#define ms(i,j) memset(i,j)
#define INF 100000000
#define llINF 1LL<<60
using namespace std;

const int MAXM = 500 + 5;

struct TwoSAT
{
    int mark[MAXM*2];
    int S[MAXM*2];
    vector<int> G[MAXM*2];
    int n, c;
    void init(int n)
    {
        this->n = n;
        for (int i=0;i<=2*n;i++) 
        {
            G[i].clear();
            mark[i] = false;
        }
    } 
    void addE(int x, int xv, int y, int yv)
    {
        x = x * 2 + xv;
        y = y * 2 + yv;
        G[x^1].push_back(y);
        G[y^1].push_back(x); 
    }
    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;
    }
    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;
    }
}ts;
struct linkz
{
    int a, b;
}lk[MAXM];

int n, m;

int init()
{
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d", &lk[i].a, &lk[i].b);
        if (lk[i].a>lk[i].b) 
        {
            int t = lk[i].a; 
            lk[i].a = lk[i].b, lk[i].b = t;
        }
    }
    ts.init(n);
    for (int i=1;i<=m;i++)
    for (int j=i+1;j<=m;j++)
    {
        int &x1 = lk[i].a, &y1 = lk[i].b, &x2 = lk[j].a, &y2 = lk[j].b;
        if ( (x1<=x2 && y1<=y2 && x2<=y1) || (x1>=x2 && y1>=y2 && y2>=x1) )
        {
            ts.addE(i-1, 0, j-1, 0);
            ts.addE(i-1, 1, j-1, 1);

        }
    }
}
int solve()
{
    if (ts.solve())
    {
        printf("panda is telling the truth...\n");
    } else printf("the evil panda is lying again\n");
}
int main()
{
    while (scanf("%d%d", &n, &m)==2)
    {
        init();
        solve();
    }
    return 0;
}
------ 本文结束 ------