• 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();
    }
  • 相关阅读:
    第二部分 高数_9 优化
    第二部分 高数_8 泰勒公式、麦克劳林公式和线性化
    第二部分 高数_7 二元符合函数的求导法则
    第二部分 高数_6 高阶偏导数
    第二部分 高数_5 多元函数的导数
    第二部分 高数_4 函数的积分
    第二部分 高数_3 函数的微分
    第二部分 高数_2 导数
    第二部分 高数_1 极限
    第一部分 现代_4 特征值和特征向量
  • 原文地址:https://www.cnblogs.com/mfys/p/7295068.html
Copyright © 2020-2023  润新知