G - Ikki‘s Story IV - Panda‘s Trick POJ - 3207(二分图染色/2-sat)

liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.

liympanda has a magic circle and he puts it on a plane, there are n points on its boundary in circular border: 0, 1, 2, …, n − 1. Evil panda claims that he is connecting m pairs of points. To connect two points, liympanda either places the link entirely inside the circle or entirely outside the circle. Now liympanda tells Ikki no two links touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether this is possible…

Despaired at the minesweeping game just played, Ikki is totally at a loss, so he decides to write a program to help him.

Input
The input contains exactly one test case.

In the test case there will be a line consisting of of two integers: n and m (n ≤ 1,000, m ≤ 500). The following m lines each contain two integers ai and bi, which denote the endpoints of the ith wire. Every point will have at most one link.

Output
Output a line, either “panda is telling the truth…” or “the evil panda is lying again”.

Sample Input
4 2
0 1
3 2
Sample Output
panda is telling the truth…
当两条边出现这种情况的时候,这个时候两条边就必须一条在外面,一条放在圆的里面。在这里插入图片描述
我们把每一条边都看出一个点,如果两边相互矛盾,就对这两个点连线,很明显,相邻的两个点不可能染同一种颜色,就直接二分图染色。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool check(int x,int y,int z,int t){
    if(z>x&&z<y&&t>y)return true;
    if(t>x&&t<y&&z<x)return true;
    return false;
}
const int N=5e2+10;
int n,m;
vector<int>v[N];
int col[N];
struct e
{
    int a,b;
}endge[N];
bool dfs(int x)
{
    for(int i=0;i<int(v[x].size());i++){
        int nw=v[x][i];
        if(col[nw]==-1){
            col[nw]=col[x]^1;
            dfs(nw);
        }
        else{
            if(col[nw]==col[x])return false;
        }
    }
    return  true;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    int a,b;
    for(int i=1;i<=m;i++){
        cin>>a>>b;
        endge[i].a=min(a,b);
        endge[i].b=max(a,b);
    }
    for(int i=1;i<m;i++){
        for(int j=i+1;j<=m;j++){
            if(check(endge[i].a,endge[i].b,endge[j].a,endge[j].b)){
                v[i].push_back(j);
                v[j].push_back(i);
            }
        }
    }
    memset(col,-1,sizeof(col));
    for(int i=1;i<=m;i++){
        if(col[i]==-1){
            col[i]=0;
            if(!dfs(i)){
                cout<<"the evil panda is lying again"<<endl;return 0;
            }
        }
    }
    cout<<"panda is telling the truth..."<<endl;
    return 0;
}

半年前学的2-sat了,复习下模板!

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool check(int x,int y,int z,int t){
    if(z>x&&z<y&&t>y)return true;
    if(t>x&&t<y&&z<x)return true;
    return false;
}
const int N=1e3+10;
int n,m,p;
int stk[N];
vector<int>v[N];
bool col[N];
struct e
{
    int a,b;
}endge[N];
bool dfs(int x)
{
    if(col[x^1])return false;
    if(col[x])return true;
    col[x]=true;
    stk[++p]=x;
    for(int i=0;i<int(v[x].size());i++){
       if(!dfs(v[x][i])){
            return false;
       }
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    int a,b;
    for(int i=1;i<=m;i++){
        cin>>a>>b;
        endge[i].a=min(a,b);
        endge[i].b=max(a,b);
    }
    for(int i=1;i<m;i++){
        for(int j=i+1;j<=m;j++){
            if(check(endge[i].a,endge[i].b,endge[j].a,endge[j].b)){
                v[2*i-2].push_back(2*j-1);
                v[2*j-1].push_back(2*i-2);
                v[2*i-1].push_back(2*j-2);
                v[2*j-2].push_back(2*i-1);
            }
        }
    }
    memset(col,false,sizeof(col));
    for(int i=0;i<2*m;i+=2){
        if(!col[i]&&!col[i+1]){
            if(!dfs(i)){
                while(p){
                    col[stk[p]]=false;
                    p--;
                }
                if(!dfs(i^1));
                cout<<"the evil panda is lying again"<<endl;return 0;
            }
        }
    }
    cout<<"panda is telling the truth..."<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值