POJ 3683 Priest John's Busiest Day (2-SAT+輸出可行解),poj3683

來源:互聯網
上載者:User

POJ 3683 Priest John's Busiest Day (2-SAT+輸出可行解),poj3683

題目地址: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 __int64const 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\n",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\n",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\n",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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.