poj3683 2-sat Priest John's Busiest Day

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 year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si 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. The i-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 from Si to Si + Di, or from Ti - Di toTi). 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 SiTi and DiSi and Ti are in the format of hh: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 another N 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

Source

 

这个2-sat做的一路蒙蔽

判断个人觉得很简单,

输出解就要命了

有一个小镇上只有一个牧师。这个小镇上有一个传说,

在九月一日结婚的人会受到爱神的保佑,但是要牧师举办一个仪式。

这个仪式要么在婚礼刚刚开始的时候举行,要么举行完婚礼正好结束。 
现在已知有n场婚礼,告诉你每一场的开始和结束时间,

以及举行仪式所需要的时间。问牧师能否参加所有的婚礼,

如果能则输出一种方案。

 

这题输出解的方法

构建包含2n个点的有向图,如果有a+b则在a和!b   b和!a间连接一条边。

如果a和!a在一个强连通分量中,则无解。要求解集,

只需要将原图缩点后反向建图,然后染色,

具体染色方法是将遇到的第一个没有颜色的点染成红色,与它矛盾的点染成蓝色,

如此循环,所有的红色的点的集合就是解集。

多看点书还是有好处的 , 

这是大佬讲的,理解不了就记忆吧  ,也许这就是弱鸡吧

求大佬给出证明

 

这题的建图非常简单就没必要讲了

 

难受啊!!!!

 

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <vector>
  6 
  7 using namespace std;
  8 const int maxn = 4e5 + 10;
  9 struct w {
 10     int s, e;
 11     void disp() {
 12         printf("%02d:%02d %02d:%02d\n", s / 60, s % 60, e / 60, e % 60);
 13     }
 14 } wed[maxn];
 15 struct node {
 16     int u, v, next;
 17 } edge[maxn];
 18 int dfn[maxn], s[maxn], instack[maxn];
 19 int head[maxn], low[maxn], belong[maxn];
 20 int tot, flag, cnt, top, n, m;
 21 void add(int u, int v) {
 22     edge[tot].u = u;
 23     edge[tot].v = v;
 24     edge[tot].next = head[u];
 25     head[u] = tot++;
 26 }
 27 void init() {
 28     tot = flag = top = cnt = 0;
 29     memset(dfn, 0, sizeof(dfn));
 30     memset(head, -1, sizeof(head));
 31     memset(instack, 0, sizeof(head));
 32     memset(s, 0, sizeof(s));
 33 }
 34 void tarjan(int v) {
 35     low[v] = dfn[v] = ++flag;
 36     instack[v] = 1;
 37     s[top++] = v;
 38     for (int i = head[v] ; ~i ; i = edge[i].next ) {
 39         int j = edge[i].v;
 40         if (!dfn[j]) {
 41             tarjan(j);
 42             low[v] = min(low[v], low[j]);
 43         } else if (instack[j]) low[v] = min(low[v], dfn[j]);
 44     }
 45     if (dfn[v] == low[v]) {
 46         cnt++;
 47         int t;
 48         do {
 49             t = s[--top];
 50             instack[t] = 0;
 51             belong[t] = cnt;
 52         } while(t != v);
 53     }
 54 }
 55 
 56 int check(int i, int j) {
 57     if (wed[i].s >= wed[j].e || wed[i].e <= wed[j].s ) return 0;
 58     return 1;
 59 }
 60 void build(int i, int j) {
 61     if (check(2 * i, 2 * j)) add(2 * i, 2 * j + 1);
 62     if (check(2 * i, 2 * j + 1)) add(2 * i, 2 * j);
 63     if (check(2 * i + 1, 2 * j)) add(2 * i + 1, 2 * j + 1);
 64     if (check(2 * i + 1, 2 * j + 1)) add(2 * i + 1, 2 * j);
 65 }
 66 int in[maxn];
 67 queue<int>q;
 68 vector<int>tu[maxn];
 69 vector<int>ha[maxn];
 70 int color[maxn];
 71 void maketu() {
 72     int v;
 73     for (int u = 0 ; u < 2 * n ; u++) {
 74         for (int i = head[u] ; ~i ; i = edge[i].next) {
 75             v = edge[i].v;
 76             if (belong[u] != belong[v]) {
 77                 tu[belong[v]].push_back(belong[u]);
 78                 in[belong[u]]++;
 79             }
 80         }
 81     }
 82 }
 83 
 84 void topsort()  {
 85     for (int i = 1 ; i <= cnt ; i++)
 86         if (!in[i]) q.push(i);
 87     int u, v;
 88     while(!q.empty()) {
 89         u = q.front();
 90         q.pop();
 91         if (!color[u]) {
 92             color[u] = 1;
 93             for (int i = 0 ; i < ha[u].size() ; i++)
 94                 color[ha[u][i]] = 2;
 95         }
 96         for (int i = 0 ; i < tu[u].size() ; i++) {
 97             v = tu[u][i];
 98             in[v]--;
 99             if (!in[v]) q.push(v);
100         }
101     }
102 }
103 void solve() {
104     for (int i = 0 ; i < n ; i++) {
105         if (belong[i << 1] == belong[i << 1 | 1]) {
106             printf("NO\n");
107             return ;
108         } else {
109             ha[belong[i << 1]].push_back(belong[i << 1 | 1]);
110             ha[belong[i << 1 | 1]].push_back(belong[i << 1]);
111         }
112     }
113     printf("YES\n");
114     maketu();
115     topsort();
116     for (int i = 0 ; i < n ; i++) {
117         if (color[belong[i << 1]] == 1) wed[i << 1].disp();
118         else wed[i << 1 | 1].disp();
119     }
120 }
121 
122 int main() {
123   //  freopen("DATA.txt", "r", stdin);
124     scanf("%d", &n);
125     init();
126     int x, y, x1, y1, d;
127     for (int i = 0 ; i < n ; i++) {
128         scanf("%d:%d %d:%d %d", &x, &y, &x1, &y1, &d);
129         wed[i << 1].s = x * 60 + y;
130         wed[i << 1].e = wed[i << 1].s + d;
131         wed[i << 1 | 1].e = x1 * 60 + y1;
132         wed[i << 1 | 1].s = wed[i << 1 | 1].e-d;
133     }
134     for (int i = 0 ; i < n ; i++)
135         for (int j = 0 ; j < n ; j++)
136             if (i != j)  build(i, j);
137     for (int i = 0 ; i < 2 * n ; i++)
138         if (!dfn[i]) tarjan(i);
139     solve();
140     return 0;
141 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9143566.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值