zoukankan      html  css  js  c++  java
  • POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)

    题目地址:POJ 3683

    第一次做须要输出可行解的题目。

    。大体思路是先用强连通来推断是否有可行解,然后用逆序建图。用拓扑排序来进行染色。然后输出可行解。

    详细思路见传送门

    由于推断的时候少写了一个等号。。检查了好长时间。。sad。。。

    代码例如以下:

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <queue>
    #include <map>
    #include <set>
    #include <algorithm>
    
    using namespace std;
    #define LL __int64
    const int INF=0x3f3f3f3f;
    int head[2100], cnt, index, top, ans;
    int dfn[2100], low[2100], belong[2100], instack[2100], stak[2100];
    struct N
    {
        int l, r;
    } time[2100];
    struct node
    {
        int u, v, next;
    } edge[10000000];
    void add(int u, int v)
    {
        edge[cnt].v=v;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    }
    bool pan(N x, N y)
    {
        if((x.l<y.l&&x.r<=y.l)||(x.r>y.r&&x.l>=y.r))
            return 1;
        return 0;
    }
    void tarjan(int u)
    {
        dfn[u]=low[u]=++index;
        instack[u]=1;
        stak[++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(instack[v])
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(dfn[u]==low[u])
        {
            ans++;
            while(1)
            {
                int v=stak[top--];
                instack[v]=0;
                belong[v]=ans;
                if(u==v) break;
            }
        }
    }
    int head1[2100], cnt1, ct[2100], in[2100], color[2100];
    struct node1
    {
        int u, v, next;
    } edge1[10000000];
    void add1(int u, int v)
    {
        edge1[cnt1].v=v;
        edge1[cnt1].next=head1[u];
        head1[u]=cnt1++;
    }
    void topo()
    {
        queue<int>q;
        int i;
        for(i=1; i<=ans;i++)
        {
            if(in[i]==0) q.push(i);
        }
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            if(!color[u])
            {
                color[u]=1;
                color[ct[u]]=-1;
            }
            for(i=head1[u];i!=-1;i=edge1[i].next)
            {
                int v=edge[i].v;
                in[v]--;
                if(!in[v]) q.push(v);
            }
        }
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=top=ans=index=0;
        memset(dfn,0,sizeof(dfn));
        memset(instack,0,sizeof(instack));
        memset(head1,-1,sizeof(head1));
        memset(in,0,sizeof(in));
        memset(color,0,sizeof(color));
        cnt1=0;
    }
    int main()
    {
        int n, i, j, a1, b1, a2, b2, c;
        scanf("%d",&n);
        init();
        for(i=0; i<n; i++)
        {
            scanf("%d:%d%d:%d%d",&a1,&b1,&a2,&b2,&c);
            time[i<<1].l=a1*60+b1;
            time[i<<1].r=a1*60+b1+c;
            time[i<<1|1].l=a2*60+b2-c;
            time[i<<1|1].r=a2*60+b2;
        }
        for(i=0; i<n<<1; i++)
        {
            for(j=0; j<i; j++)
            {
                if(!pan(time[i],time[j]))
                {
                    add(i,j^1);
                    add(j,i^1);
                    //printf("%d %d
    ",i,j);
                }
            }
        }
        for(i=0; i<n<<1; i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        int flag=0;
        for(i=0; i<n; i++)
        {
            if(belong[i<<1]==belong[i<<1|1])
            {
                flag=1;
                break;
            }
            ct[belong[i<<1]]=belong[i<<1|1];
            ct[belong[i<<1|1]]=belong[i<<1];
        }
        if(flag)
            puts("NO");
        else
        {
            puts("YES");
            for(i=0;i<n<<1;i++)
            {
                for(j=head1[i];j!=-1;j=edge1[j].next)
                {
                    int v=edge1[j].v;
                    if(belong[i]!=belong[v])
                    {
                        add1(belong[v],belong[i]);
                        in[belong[i]]++;
                    }
                }
            }
            topo();
            for(i=0;i<n;i++)
            {
                if(color[belong[i<<1]]==1)
                {
                    printf("%02d:%02d %02d:%02d
    ",time[i<<1].l/60,time[i<<1].l%60,time[i<<1].r/60,time[i<<1].r%60);
                }
                else
                {
                    printf("%02d:%02d %02d:%02d
    ",time[i<<1|1].l/60,time[i<<1|1].l%60,time[i<<1|1].r/60,time[i<<1|1].r%60);
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    CNN comprehension
    Gradient Descent
    Various Optimization Algorithms For Training Neural Network
    gerrit workflow
    jenkins job配置脚本化
    Jenkins pipeline jobs隐式传参
    make words counter for image with the help of paddlehub model
    make words counter for image with the help of paddlehub model
    git push and gerrit code review
    image similarity
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7070246.html
Copyright © 2011-2022 走看看