http://poj.org/problem?id=3207
题意:
一个圆盘的边沿上有n个点, 下标从0开始, 有m条线连接2m个互不相同的点, 线可以在圆盘内部,也可以在圆盘外部, 要求任意两条线不能相交. 给出m条线(内外随意), 问是否满足每条线都不相交.
思路:
可以将第i条线看成一对顶点,编号分别为2*i和2*i+1.那么如果线段i与j相交,就在2*i与2*j+1以及2*i+1与2*j之间连一条双向边。然后就转化到2-sat上判断是否存在可行解了。
//#pragma comment(linker,"/STACK:327680000,327680000") #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long #define inf 0x7f7f7f7f #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("din.txt", "r", stdin) #define Write() freopen("dout.txt", "w", stdout); #define N 1007 #define M 1007 using namespace std; struct side { int s,e; }sid[M]; int n,m; struct node { int v; int next; }g[M*M]; int head[M],ct; int dfn[M],low[M]; int belong[M],stk[M]; bool isn[M]; int idx,cnt,top; bool isok(int i,int j) { //判断相交,建图是关键 if ((sid[i].s < sid[j].s && sid[i].e > sid[j].s && sid[i].e < sid[j].e) || (sid[i].s > sid[j].s && sid[i].s < sid[j].e && sid[i].e > sid[j].e)) return true; else return false; } void add(int u,int v) { g[ct].v = v; g[ct].next = head[u]; head[u] = ct++; g[ct].v = u; g[ct].next = head[v]; head[v] = ct++; } void tarjan(int u) { int i,j; dfn[u] = low[u] = ++idx; stk[++top] = u; isn[u] = true; for (i = head[u]; i != - 1; i = g[i].next) { int v = g[i].v; if (dfn[v] == -1) { tarjan(v); low[u] = min(low[u],low[v]); } else if (isn[v]) { low[u] = min(low[u],dfn[v]); } } if (dfn[u] == low[u]) { cnt++; do { j = stk[top--]; belong[j] = cnt; isn[j] = false; }while (j != u); } } void solve() { int i; for (i = 0; i < 2*m; ++i) { dfn[i] = low[i] = -1; isn[i] = false; belong[i] = 0; } idx = cnt = top = 0; for (i = 0; i < 2*m; ++i) { if (dfn[i] == -1) tarjan(i); } bool flag = false; for (i = 0; i < m; ++i) { if (belong[2*i] == belong[2*i + 1]) { flag = true; break; } } if (flag) printf("the evil panda is lying again\n"); else printf("panda is telling the truth...\n"); } int main() { // Read(); int i,j; while (~scanf("%d%d",&n,&m)) { for (i = 0; i < m; ++i) { scanf("%d%d",&sid[i].s,&sid[i].e); if (sid[i].s > sid[i].e) swap(sid[i].s,sid[i].e); } CL(head,-1); ct = 0; for (i = 0; i < m; ++i) { for (j = i + 1; j < m; ++j) { if (isok(i,j)) { add(2*i,2*j + 1); add(2*j,2*i + 1); } } } solve(); } return 0; }