如题= =自用模板

分为三种 1:O(n*m)求字典序最小 2.O(m)求可行性(常与二分结合) 3.O(m)求任意一组解
A AND B = 1 !A->A !B->B A AND B = 0 A->!B B->!A A OR B = 1 !A->B !B->A A OR B = 0 A->!A B->!B A XOR B = 1 A->!B !B->A !A->B B->!A A XOR B = 0 A->B B->A !A->!B !B->!A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//O(n*m)字典序最小
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
TwoSat ts = new TwoSat();
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
ts.init(n);
for (int i = 1; i <= m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
a--; //下标一定要从0开始 否则改solve里的下标
b--;
ts.add_diredge(a, b ^ 1);
ts.add_diredge(b, a ^ 1);
}
if (!ts.solve()) {
pw.println("NIE");
} else {
for (int i = 0; i < (2*n); i++) { //遍历求解
if (ts.mark[i])
pw.println(i + 1);
}
}
pw.flush();
}
}
}

class TwoSat {
//O(n*m)
Edge[] edge;
int[] s;
int c;
int n;
boolean[] mark;
//mark[i<<1]数组等于1,表示点i被选择
//mark[i<<1|1]数组等于1,表示点i没有被选择
int[] head;
int maxn = 40005; //注意开对数的两倍
int maxm = 200005;
int cnt;

TwoSat() {
edge = new Edge[maxm];
for (int i = 0; i < maxm; i++)
edge[i] = new Edge();
head = new int[maxn];
s = new int[maxn];
mark = new boolean[maxn];
}

void init(int n_) { //0~n*2
this.n = n_;
cnt = 0;
Arrays.fill(head, -1);
Arrays.fill(mark, false);
}

void add_diredge(int u, int v) {
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}

boolean dfs(int x) {
//用来判断当前的强连通分量当中会不会出现矛盾
//如果需要被选的不能被选那么矛盾
if (mark[x ^ 1])
return false;
//如果需要被选的已经被选,那么当前联通分量一定
//不会出现矛盾
if (mark[x])
return true;
//如果当前点需要被选,那么选上它,并且标记
mark[x] = true;
//当前的强连通分量加上这个点
s[c++] = x;
//找到与当前点相连点,判断他们的状态
for (int i = head[x]; i != -1; i = edge[i].next) {
if (!dfs(edge[i].to))return false;
}
return true;
}

boolean solve() {
for (int i = 0; i < 2 * n; i += 2) {
if (!mark[i] && !mark[i + 1]) {
c = 0;
if (!dfs(i)) {
//如果矛盾,那么这个强连通分量里的点都不能选取
while (c > 0) mark[s[--c]] = false;
if (!dfs(i^1)) return false;
}
}
}
return true;
}
}

class Edge {
int to, next;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//先init(n) 下标0~2*n 然后solve()判断可行性
class TwoSat {
Edge[] edge;
int[] s,dfn,low,stack,belong;
int c;
int n;
boolean[] mark,instack;
//mark[i<<1]数组等于1,表示点i被选择
//mark[i<<1|1]数组等于1,表示点i没有被选择
int[] head;
int maxn = 2005; //注意是对数*2
int maxm = 4000005;
int[]to;
int[]next;
int cnt,bcount,dindex,top;

TwoSat() {
/*edge = new Edge[maxm];
for (int i = 0; i < maxm; ia`++)
edge[i] = new Edge();*/
to=new int[maxm];
next=new int[maxm];
head = new int[maxn];
s = new int[maxn];
mark = new boolean[maxn];
dfn=new int[maxn];
low=new int[maxn];
stack=new int[maxn];
belong=new int[maxn];
instack=new boolean[maxn];
}

void init(int n_) { //0~n*2
this.n = n_;
cnt = 0;
Arrays.fill(head, -1);
Arrays.fill(mark, false);
top=0;
bcount=0;
dindex=0;
Arrays.fill(dfn, 0);
}
boolean solve(){
for(int i=0;i<2*n;i++){
if(dfn[i]==0)tarjan(i);
}
for(int i=0;i<2*n;i+=2){
if(belong[i]==belong[i^1])return false;
}
return true;
}
void add_diredge(int u, int v) {
//edge[cnt].to = v;
//edge[cnt].next = head[u]; //感觉内存不够用下面的
to[cnt]=v;
next[cnt]=head[u];
head[u] = cnt++;
}

void tarjan(int u){
dfn[u]=low[u]=++dindex;
stack[++top]=u;
instack[u]=true;
// for(int i=head[u];i!=-1;i=edge[i].next){
for(int i=head[u];i!=-1;i=next[i]){
int v=to[i];
// int v=edge[i].to;
if(dfn[v]==0){
tarjan(v);
low[u]=Math.min(low[u], low[v]);
}else if(instack[v]&&dfn[v]<low[u]){
low[u]=dfn[v];
}
}
if(dfn[u]==low[u]){
bcount++;
int v;
do{
v=stack[top--];
instack[v]=false;
belong[v]=bcount;
}while(u!=v);
}
}
}

class Edge {
int to, next;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//输出任意一组解
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
TwoSat ts = new TwoSat();
while (sc.hasNext()) {
int n=sc.nextInt();
ts.init(n);
int[][]shu=new int[n][3];
for(int i=0;i<n;i++){
String s=sc.next();
String e=sc.next();
int t=sc.nextInt();
shu[i][0]=Integer.valueOf(s.substring(0, 2))*60+Integer.valueOf(s.substring(3, 5));
shu[i][1]=Integer.valueOf(e.substring(0, 2))*60+Integer.valueOf(e.substring(3, 5));
shu[i][2]=t;
}
boolean check=true;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int s1=shu[i][0];
int e1=shu[i][0]+shu[i][2];
//
int s2=shu[i][1]-shu[i][2];
int e2=shu[i][1];
//
int s3=shu[j][0];
int e3=shu[j][0]+shu[j][2];
//
int s4=shu[j][1]-shu[j][2];
int e4=shu[j][1];
if(intersect(s1,e1,s3,e3)){
ts.add_diredge(i*2, (j*2)^1);
ts.add_diredge(j*2, (i*2)^1);
}
if(intersect(s1,e1,s4,e4)){
ts.add_diredge(i*2, j*2);
ts.add_diredge((j*2)^1, (i*2)^1);
}
if(intersect(s2,e2,s3,e3)){
ts.add_diredge(j*2, i*2);
ts.add_diredge((i*2)^1, (j*2)^1);
}
if(intersect(s2,e2,s4,e4)){
ts.add_diredge((i*2)^1, (j*2));
ts.add_diredge((j*2)^1, (i*2));
}
}
}
boolean flag=ts.solve();
if(flag){
pw.println("YES");
ts.rebuild();
for(int i=0;i<2*n;i+=2){ //颜色相同的输出
int k=i/2;
if(ts.color[ts.belong[i]]==1){
pw.println(trans(shu[k][0])+" "+trans(shu[k][0]+shu[k][2]));
}else{
pw.println(trans(shu[k][1]-shu[k][2])+" "+trans(shu[k][1]));
}
}
}
else pw.println("NO");
pw.flush();
}
}
static boolean intersect(int s1,int e1,int s2,int e2){
if(Math.max(s1, s2)<Math.min(e1, e2))return true;
else return false;
}
static String trans(int k){
int h=k/60;
int m=k-h*60;
StringBuilder sb=new StringBuilder();
if(h<10)sb.append(0);
sb.append(h);
sb.append(':');
if(m<10)sb.append(0);
sb.append(m);
return sb.toString();
}
}

class TwoSat {
//O(n*m)
Edge[] edge;
Edge[] edge2;
int[] s,dfn,low,stack,belong;
int c;
int n;
boolean[] mark,instack;
//mark[i<<1]数组等于1,表示点i被选择
//mark[i<<1|1]数组等于1,表示点i没有被选择
int[] head;
int maxn = 2005; //注意是对数*2
int maxm = 2000000;
int[]start1;
int[]to1;
int[]next1;

int[]start2;
int[]to2;
int[]next2;
int cnt,bcount,dindex,top;
int[]cf;
int[]indegree;
int[]color;
int cntnew;
TwoSat() {
/*edge = new Edge[maxm];
for (int i = 0; i < maxm; i++)
edge[i] = new Edge();
edge2=new Edge[maxm];
for(int i=0;i<maxm;i++)
edge2[i]=new Edge();*/
start1=new int[maxm];
to1=new int[maxm];
next1=new int[maxm];

start2=new int[maxm];
to2=new int[maxm];
next2=new int[maxm];

head = new int[maxn];
s = new int[maxn];
mark = new boolean[maxn];
dfn=new int[maxn];
low=new int[maxn];
stack=new int[maxn];
belong=new int[maxn];
instack=new boolean[maxn];
cf=new int[maxn];
indegree=new int[maxn];
color=new int[maxn];
}

void init(int n_) { //0~n*2
this.n = n_;
cnt = 0;
Arrays.fill(head, -1);
Arrays.fill(mark, false);
top=0;
bcount=0;
dindex=0;
Arrays.fill(dfn, 0);
cntnew=0;
}
boolean solve(){
for(int i=0;i<2*n;i++){
if(dfn[i]==0)tarjan(i);
}
for(int i=0;i<2*n;i+=2){
if(belong[i]==belong[i^1])return false;
//这里,为着色做准备,cf[x]保存的是和编号为 x 的连通分量矛盾的连通分量的编号
//在赵爽的论文中有证明,这样可以保证拓扑求解的可行性
//b 和 b' 所在的连通分量是矛盾的(如果不矛盾,那么2-SAT无解)
cf[belong[i]]=belong[i^1];
cf[belong[i^1]]=belong[i];
}
return true;
}
void rebuild(){
Arrays.fill(indegree, 0);
Arrays.fill(head, -1);
cntnew=0;
for(int i=0;i<cnt;i++){
//if(belong[edge[i].start]!=belong[edge[i].to]){
//add_diredgenew(belong[edge[i].to],belong[edge[i].start]);
//indegree[belong[edge[i].start]]++;
if(belong[start1[i]]!=belong[to1[i]]){
add_diredgenew(belong[to1[i]],belong[start1[i]]);
indegree[belong[start1[i]]]++;
}
}
Queue<Integer>queue=new LinkedList<Integer>();
Arrays.fill(color, 0);
for(int i=1;i<=bcount;i++){
if(indegree[i]==0)queue.add(i);
}
while(!queue.isEmpty()){
int cur=queue.poll();
if(color[cur]==0){
color[cur]=1;
color[cf[cur]]=-1;
}
//for(int i=head[cur];i!=-1;i=edge2[i].next){
for(int i=head[cur];i!=-1;i=next2[i]){
//int u=edge2[i].to;
int u=to2[i];
if(--indegree[u]==0)queue.add(u);
}
}
}
void add_diredge(int u, int v) {
//edge[cnt].start=u;
//edge[cnt].to = v;
//edge[cnt].next = head[u]; //感觉内存不够用下面的
to1[cnt]=v;
next1[cnt]=head[u];
start1[cnt]=u;
head[u] = cnt++;
}
void add_diredgenew(int u, int v) {
//edge2[cntnew].start=u;
//edge2[cntnew].to = v;
//edge2[cntnew].next = head[u]; //感觉内存不够用下面的
start2[cntnew]=u;
to2[cntnew]=v;
next2[cntnew]=head[u];
head[u] = cntnew++;
}
void tarjan(int u){
dfn[u]=low[u]=++dindex;
stack[++top]=u;
instack[u]=true;
//for(int i=head[u];i!=-1;i=edge[i].next){
for(int i=head[u];i!=-1;i=next1[i]){
int v=to1[i];
// int v=edge[i].to;
if(dfn[v]==0){
tarjan(v);
low[u]=Math.min(low[u], low[v]);
}else if(instack[v]&&dfn[v]<low[u]){
low[u]=dfn[v];
}
}
if(dfn[u]==low[u]){
bcount++;
int v;
do{
v=stack[top--];
instack[v]=false;
belong[v]=bcount;
}while(u!=v);
}
}
}

class Edge {
int start,to,next;
}