zoukankan      html  css  js  c++  java
  • 2-SAT poj3207将边看做点

    Ikki's Story IV - Panda's Trick
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 10238   Accepted: 3762

    Description

    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...

    将边看成点,然后拆点,一个代表圆内边(以点代边),一个代表园外边.
    由约束条件搞出哪些点在所建图中必定要相连,然后跑tajan得到联通块‘
    并查集也是可以的

    平面上,一个圆,圆的边上按顺时针放着n个点。现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接。给你的信息中,每个点最多只会连接的一条边。问能不能连接这m条边,使这些边都不相交。

    2-sat中一条有向边的涵义是选了起点就必须选终点

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1005;
    const int M=2000005;
    const int INF=1000000000;
    int n,m,tot,head[N];
    int dfn[N],low[N],index,instack[N],scc;
    int top,st[N],fa[N];
    int x[N],y[N];
    struct edge{
       int v,nxt;
    }e[M];
    void init(){
       tot=index=scc=top=0;
       memset(dfn,0,sizeof(dfn));
       memset(instack,0,sizeof(instack));
       memset(head,-1,sizeof(head));
    }
    void insert(int x,int y){
       e[tot].v=y;
       e[tot].nxt=head[x];
       head[x]=tot++;
    }
    void Tarjan(int u){
       instack[u]=1;
       st[top++]=u;
       dfn[u]=low[u]=++index;
       for(int i=head[u];~i;i=e[i].nxt){
        int v=e[i].v;
        if(!dfn[v]) {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v]) low[u]=min(low[u],dfn[v]);
       }
       if(dfn[u]==low[u]){
        ++scc;int t;
        do{
            t=st[--top];
            instack[t]=0;
            fa[t]=scc;
        }while(t!=u);
       }
    }
    void build(){
       for(int i=1;i<=m;++i){
        scanf("%d%d",&x[i],&y[i]);
        ++x[i],++y[i];
        if(x[i]>y[i]) swap(x[i],y[i]);
       }
       for(int i=1;i<=m;++i) for(int j=i+1;j<=m;++j)
       if((x[i]<=x[j]&&y[i]>=x[j]&&y[i]<=y[j])||(x[i]>=x[j]&&x[i]<=y[j]&&y[i]>=y[j])){
        insert(i,j+m);
        insert(j,i+m);
        insert(i+m,j);
        insert(j+m,i);
       }
       n=2*m;
    }
    bool check(){
       for(int i=1;i<=m;++i) if(fa[i]==fa[i+m]) return false;
       return true;
    }
    void solve(){
       for(int i=1;i<=n;++i) if(!dfn[i]) Tarjan(i);
       if(check()) puts("panda is telling the truth...");
       else puts("the evil panda is lying again");
    }
    int main(){
        scanf("%d%d",&n,&m);
        init();
        build();
        solve();
    }
  • 相关阅读:
    spring security使用数据库管理用户权限
    ubuntu安装配置jdk tomcat mysql ...
    64位虚拟机安装64位ubuntu出现问题
    maven pom文件结构详解
    Maven3下的java web项目
    数据库分页和使用jstl标签替换分页的jsp代码
    servlet生命周期
    图片校验码的生成
    HttpSessionListener和HttpSessionBindingListener监听session的销毁
    perl 处理特殊字符
  • 原文地址:https://www.cnblogs.com/mfys/p/7295068.html
Copyright © 2011-2022 走看看