poj 3683 Priest John's Busiest Day(2—sat)

Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9545 Accepted: 3256 Special Judge

Description

John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This yearN couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from timeSi to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. Thei-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either fromSi to Si + Di, or fromTi - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.

Note that John can not be present at two weddings simultaneously.

Input

The first line contains a integer N ( 1 ≤ N ≤ 1000).
The next N lines contain the Si, Ti andDi. Si and Ti are in the format ofhh:mm.

Output

The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony. If it is "YES", output anotherN lines describing the staring time and finishing time of all the ceremonies.

Sample Input

2
08:00 09:00 30
08:15 09:00 20

Sample Output

YES
08:00 08:30
08:40 09:00


题意:在一个小村庄,杰森作为婚礼祝福者,在某一天,有n场婚礼,杰森要给所有婚礼祝福,时间在婚礼的开始或结束之前。问杰森是否能参加所有婚礼,若可以则输出参加的时间。


思路:很明显的2—sat的题目,一场婚礼可以有两个参加的时间段,婚礼之间可能会发生冲突,解决这种问题通常使用2-sat。

注意点:1.数组要开的足够大,本人由于没开足够大的数组wa了半个小时。
        2.注意运算符之间的优先级比如i!=(j^1),注意,如果不打括号是会发生错误的。

个人关于2-sat的一点小理解:
   1.由于这是做的第一道2sat,建图的时候总感觉不太对。当两场婚礼的时间段有所冲突的时候,就在冲突与不冲突之间连一条边,这样的连边正确么???
        从理解上来上,冲突之后的确只能这样选择,但是如果考虑到连的边也是冲突的话,那么这相应点就会在一个强连通分量,在最终判断的时候终究是NO,所以建图没有问题。
   2.建边的时候是addedge(i,j^1);addedge(j,i^1),由于是有向图,建边方向很重要,为何要这样建???
        这是我个人的小猜测:由于i和j是冲突的时间段,所以i和j是不能形成互联的状态的。这样的连边方式就是为了杜绝这个现象。
   



 代码:
//#include<bits/stdc++h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int N = 2010;
const int M = N*N;
struct node
{
    int l,r,id;
    node (int _l=0,int _r=0,int _id=0)
    {
        l=_l;
        r=_r;
        id=_id;
    }
} A[N*2];
struct Edge
{
    int from, to, next;
} edge[M], edge2[M];
int head[N];
int cntE, cntE2;
void addedge(int u, int v)
{
    edge[cntE].from = u;
    edge[cntE].to = v;
    edge[cntE].next = head[u];
    head[u] = cntE++;
}
void addedge2(int u, int v)
{
    edge2[cntE2].from = u;
    edge2[cntE2].to = v;
    edge2[cntE2].next = head[u];
    head[u] = cntE2++;
}

int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt;

void tarjan(int u)
{
    dfn[u] = low[u] = ++idx;
    in[u] = true;
    stk[++top] = u;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
        else if (in[v]) low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u])
    {
        ++cnt;
        while (1)
        {
            int v = stk[top--];
            kind[v] = cnt;
            in[v] = false;
            if (v == u) break;
        }
    }
}

int opp[N], ind[N], col[N]; // 相对的点 入度 染色 col[]=1选择

bool topsort(int n) // 序号从0开始
{
    for (int i = 0; i < 2*n; i += 2)
    {
        int k1 = kind[i];
        int k2 = kind[i^1]; // 相对的两个点
        if (k1 == k2) return false;
        opp[k1] = k2;
        opp[k2] = k1;
    }
    memset(head, -1, sizeof head);
    int u, v;
    for (int i = 0; i < cntE; ++i)
    {
        u = edge[i].from, v = edge[i].to;
        if (kind[u] != kind[v])   // 反向建图
        {
            addedge2(kind[v], kind[u]);
            ind[kind[u]]++;
        }
    }
    queue<int> q;
    for (int i = 1; i <= cnt; ++i) if (!ind[i]) q.push(i);
    while (q.size())
    {
        u = q.front();
        q.pop();
        if (!col[u]) col[u] = 1, col[ opp[u] ] = -1;
        for (int i = head[u]; i != -1; i = edge2[i].next)
            if (--ind[edge2[i].to] == 0) q.push(edge2[i].to);
    }
    return true;
}

void init()
{
    cntE = cntE2 = 0;
    memset(head, -1, sizeof head);
    memset(dfn, 0, sizeof dfn);
    memset(in, false, sizeof in);
    idx = top = cnt = 0;
    memset(ind, 0, sizeof ind);
    memset(col, 0, sizeof col);
}
int judge(int i,int j)
{
    if(A[i].l<=A[j].l&&A[j].l<A[i].r)return 1;
    if(A[j].l<=A[i].l&&A[i].l<A[j].r)return 1;
    return 0;
}
int main()
{
    int n;
    scanf("%d",&n);
    int a,b,c,d,e,flag=0;
    init();
    for(int i=0; i<n; i++)
    {
        scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&e);
        int l=a*60+b,r=c*60+d;
        A[i*2]= {l,l+e,i};
        A[i*2+1]= {r-e,r,i};
    }
    for(int i=0; i<2*n; i++)
        for(int j=i+1; j<2*n; j++)
        {
            if(i!=(j^1)&&judge(i,j))
            {
                addedge(i,j^1);
                addedge(j,i^1);
            }
        }
    for(int i=0; i<2*n; i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=0;i<2*n;i+=2)
    if(kind[i]==kind[i+1])
    {
        printf("NO\n");
        return 0;
    }/*
    printf("YES\n");
    for(int i=0;i<2*n;i+=2)
    if(kind[i]<kind[i+1])
    {
        printf("%02d:%02d %02d:%02d\n",A[i].l/60,A[i].l%60,A[i].r/60,A[i].r%60);
    }
    else
        printf("%02d:%02d %02d:%02d\n",A[i+1].l/60,A[i+1].l%60,A[i+1].r/60,A[i+1].r%60);
    */if(topsort(n))
    {
        printf("YES\n");
        for(int i=0; i<2*n; i++)
            if(col[kind[i]]==1)
            {
                printf("%02d:%02d %02d:%02d\n",A[i].l/60,A[i].l%60,A[i].r/60,A[i].r%60);
            }
    }
    else
        printf("NO\n");
    return 0;
}

 
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值