POJ3683 Priest John's Busiest Day
原题链接
\(2-SAT\)模板题。
对于有时间重叠的婚礼转换成\(2-SAT\)的命题形式连边,用\(tarjan\)找强连通分量并判断,确定方案即可。
然而一道模板题,我因为数组开小了调了一晚上。。。
#include<cstdio>
using namespace std;
const int N = 2010;
const int M = 1e6 + 10;
struct dd {
int x[2], y[2], z, S[2];
};
dd a[N >> 1];
int fi[N], di[M << 2], ne[M << 2], dfn[N], low[N], st[N], bl[N], l, ti, tp, SCC;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
void tarjan(int x)
{
int i, y;
dfn[x] = low[x] = ++ti;
st[++tp] = x;
v[x] = 1;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (!dfn[y])
{
tarjan(y);
low[x] = minn(low[x], low[y]);
}
else
if (v[y])
low[x] = minn(low[x], dfn[y]);
}
if (!(dfn[x] ^ low[x]))
{
++SCC;
do
{
y = st[tp--];
bl[y] = SCC;
v[y] = 0;
} while (x ^ y);
}
}
void pr(int p, int i)
{
printf("%02d:%02d ", a[i].x[p], a[i].y[p]);
a[i].y[p] += a[i].z;
a[i].x[p] += a[i].y[p] / 60;
a[i].y[p] %= 60;
printf("%02d:%02d\n", a[i].x[p], a[i].y[p]);
}
int main()
{
int i, n, j, x, y, z;
n = re();
for (i = 1; i <= n; i++)
{
a[i].x[0] = re();
a[i].y[0] = re();
a[i].x[1] = re();
a[i].y[1] = re();
a[i].z = re();
a[i].S[0] = 60 * a[i].x[0] + a[i].y[0];
a[i].S[1] = 60 * a[i].x[1] + a[i].y[1] - a[i].z;
while (a[i].y[1] < a[i].z)
{
a[i].x[1]--;
a[i].y[1] += 60;
}
a[i].y[1] -= a[i].z;
}
for (i = 1; i < n; i++)
for (j = i + 1; j <= n; j++)
for (x = 0; x < 2; x++)
for (y = 0; y < 2; y++)
if (!(a[i].S[x] + a[i].z <= a[j].S[y] || a[i].S[x] >= a[j].S[y] + a[j].z))
{
add(i + x * n, j + (y ^ 1) * n);
add(j + y * n, i + (x ^ 1) * n);
}
for (i = 1; i <= (n << 1); i++)
if (!dfn[i])
tarjan(i);
for (i = 1; i <= n; i++)
if (!(bl[i] ^ bl[i + n]))
{
printf("NO");
return 0;
}
printf("YES\n");
for (i = 1; i <= n; i++)
{
if (bl[i] > bl[i + n])
pr(1, i);
else
pr(0, i);
}
return 0;
}
posted on 2018-09-12 21:13 Iowa_Battleship 阅读(110) 评论(0) 编辑 收藏 举报