TwoSAT example
[Html]
// File Name: uva1146.cpp
// Author: bo_jwolf
// Created Time: Thursday, May 09,201 3 PM12: 10: 09 HKT
# Include <vector>
# Include <list>
# Include <map>
# Include <set>
# Include <deque>
# Include <stack>
# Include <queue>
# Include <bitset>
# Include <algorithm>
# Include <functional>
# Include <numeric>
# Include <utility>
# Include <sstream>
# Include <iostream>
# Include <iomanip>
# Include <cstdio>
# Include <cmath>
# Include <cstdlib>
# Include <cstring>
# Include <ctime>
// TwoSAT solver;
Using namespace std;
Const int maxn = 2005;
// Nt n, T [maxn] [2];
/* Twosat */
Struct TwoSAT
{
Int n;
Vector <int> G [maxn * 2];
Bool mark [maxn * 2];
Int S [maxn * 2], c;
Bool dfs (int x)
{
If (mark [x ^ 1])
Return false;
If (mark [x])
Return true;
Mark [x] = true;
S [c ++] = x;
For (int I = 0; I <G [x]. size (); I ++)
If (! Dfs (G [x] [I])
Return false;
Return true;
}
Void init (int n)
{
This-> n = n;
For (int I = 0; I <n * 2; I ++)
G [I]. clear ();
Memset (mark, 0, sizeof (mark ));
}
Void add_clause (int x, int xval, int y, int yval)
{
X = x * 2 + xval;
Y = y * 2 + yval;
G [x ^ 1]. push_back (y );
G [y ^ 1]. push_back (x );
}
Bool solve ()
{
For (int I = 0; I <n * 2; 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;
}
};
TwoSAT solver;
Int n, T [maxn] [2];
Bool test (int diff)
{
Solver. init (n );
For (int I = 0; I <n; I ++)
For (int a = 0; a <2; a ++)
For (int j = I + 1; j <n; j ++)
For (int B = 0; B <2; B ++)
If (abs (T [I] [a]-T [j] [B]) <diff)
Solver. add_clause (I, a ^ 1, j, B ^ 1 );
Return solver. solve ();
}
Int main ()
{
While (scanf ("% d", & n) = 1 & n)
{
Int L = 0, R = 0;
For (int I = 0; I <n; I ++)
For (int a = 0; a <2; a ++)
{
Scanf ("% d", & T [I] [a]);
R = max (R, T [I] [a]);
}
While (L <R)
{
Int M = L + (R-L + 1)/2;
If (test (M ))
L = M;
Else
R = M-1;
}
Printf ("% d \ n", L );
}
Return 0;
}
// File Name: uva1146.cpp
// Author: bo_jwolf
// Created Time: Thursday, May 09,201 3 PM12: 10: 09 HKT
# Include <vector>
# Include <list>
# Include <map>
# Include <set>
# Include <deque>
# Include <stack>
# Include <queue>
# Include <bitset>
# Include <algorithm>
# Include <functional>
# Include <numeric>
# Include <utility>
# Include <sstream>
# Include <iostream>
# Include <iomanip>
# Include <cstdio>
# Include <cmath>
# Include <cstdlib>
# Include <cstring>
# Include <ctime>
// TwoSAT solver;
Using namespace std;
Const int maxn = 2005;
// Nt n, T [maxn] [2];
/* Twosat */
Struct TwoSAT
{
Int n;
Vector <int> G [maxn * 2];
Bool mark [maxn * 2];
Int S [maxn * 2], c;
Bool dfs (int x)
{
If (mark [x ^ 1])
Return false;
If (mark [x])
Return true;
Mark [x] = true;
S [c ++] = x;
For (int I = 0; I <G [x]. size (); I ++)
If (! Dfs (G [x] [I])
Return false;
Return true;
}
Void init (int n)
{
This-> n = n;
For (int I = 0; I <n * 2; I ++)
G [I]. clear ();
Memset (mark, 0, sizeof (mark ));
}
Void add_clause (int x, int xval, int y, int yval)
{
X = x * 2 + xval;
Y = y * 2 + yval;
G [x ^ 1]. push_back (y );
G [y ^ 1]. push_back (x );
}
Bool solve ()
{
For (int I = 0; I <n * 2; 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;
}
};
TwoSAT solver;
Int n, T [maxn] [2];
Bool test (int diff)
{
Solver. init (n );
For (int I = 0; I <n; I ++)
For (int a = 0; a <2; a ++)
For (int j = I + 1; j <n; j ++)
For (int B = 0; B <2; B ++)
If (abs (T [I] [a]-T [j] [B]) <diff)
Solver. add_clause (I, a ^ 1, j, B ^ 1 );
Return solver. solve ();
}
Int main ()
{
While (scanf ("% d", & n) = 1 & n)
{
Int L = 0, R = 0;
For (int I = 0; I <n; I ++)
For (int a = 0; a <2; a ++)
{
Scanf ("% d", & T [I] [a]);
R = max (R, T [I] [a]);
}
While (L <R)
{
Int M = L + (R-L + 1)/2;
If (test (M ))
L = M;
Else
R = M-1;
}
Printf ("% d \ n", L );
}
Return 0;
}