题意:
有0~n-1个点按顺序围成一圈..现在在某些两点间加边..这条边要么在圆内..要么在圆外..问能否保证这些线段都不相交
题解:
每个线段有两种状态..里面or外面..又有些线段的某些状态是不能共存的..抽象出2-sat模型..每条线段作为一个组..线段的两种状态作为这个组的两个点..以线段相交的情况来做点之间的有向边..然后用tarjan跑强联通分量..判断每个线段的两种状态是否在一个强联通里..若在..说明一定有线段相交..
Program:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#define ll long long
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 1005
using namespace std;
struct node
{
int x,y;
}line[MAXN];
int n,m,DfsIndex,tpnum,tp[MAXN],dfn[MAXN],low[MAXN];
bool instack[MAXN];
vector<int> T[MAXN];
stack<int> mystack;
bool ok(int a,int b) //判断两直线是否相交
{
int cnt=0;
if (line[a].x>=line[b].x && line[a].x<=line[b].y) cnt++;
if (line[a].y>=line[b].x && line[a].y<=line[b].y) cnt++;
if (cnt==1) return false;
return true;
}
void tarjan(int x)
{
int i,y,m=T[x].size();
dfn[x]=low[x]=++DfsIndex;
instack[x]=true;
mystack.push(x);
for (i=0;i<m;i++)
{
y=T[x][i];
if (!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
}else
if (instack[y])
low[x]=min(low[x],dfn[y]);
}
if (low[x]==dfn[x])
{
tpnum++;
do
{
x=mystack.top();
mystack.pop();
tp[x]=tpnum;
instack[x]=false;
}while (low[x]!=dfn[x]);
}
return;
}
bool judge()
{
int i;
for (i=0;i<m;i++)
if (tp[i<<1]==tp[(i<<1)|1]) return false;
return true;
}
int main()
{
int i,j;
while (~scanf("%d%d",&n,&m))
{
for (i=0;i<m;i++)
{
int x,y,t;
scanf("%d%d",&x,&y);
if (x>y) t=x,x=y,y=t;
line[i].x=x,line[i].y=y;
}
for (i=0;i<(m<<1);i++) T[i].clear();
for (i=0;i<m;i++)
for (j=i+1;j<m;j++)
if (!ok(i,j))
{
T[i<<1].push_back((j<<1)|1);
T[j<<1].push_back((i<<1)|1);
T[(i<<1)|1].push_back(j<<1);
T[(j<<1)|1].push_back(i<<1);
}
memset(dfn,0,sizeof(dfn));
memset(instack,false,sizeof(instack));
while (!mystack.empty()) mystack.pop();
DfsIndex=tpnum=0;
for (i=0;i<(m<<1);i++)
if (!dfn[i]) tarjan(i);
if (judge()) printf("panda is telling the truth...\n");
else printf("the evil panda is lying again\n");
}
return 0;
}