题意:
有一个有两个面的圆,给你p条边,每条边的端点都在圆上,问你能不能通过把这些边放在正面或反面,来达到这些线段都不相交的状态。
每条边当作一个点,正面和背面两种状态必取其中一种。
2-sat题。构造必选规则,然后跑tarjan缩点判断是否有矛盾,没有则一定能找到一种解。(因为tarjan跑出来一定是有向无环图(DAG))
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
struct edge{
int from,to,v,next;
}ed[1001000],a[520];
int head[1100];
int cnte;
void ae(int x,int y){
ed[++cnte].to = y;
ed[cnte].next = head[x];
head[x]=cnte;
}
int dfn[1100],low[1100],vis[1100],stak[1100],belong[1100],cntc,cnts,index;//strong connected component //cnt of stack
void dfs(int u){
dfn[u]=low[u] = ++index;
stak[cnts++]=u;
vis[u]=1;
for(int i = head[u];i!=-1;i=ed[i].next){
int v = ed[i].to;
if(!dfn[v]){
dfs(v);
low[u] = min(low[u],low[v]);
}
else if(vis[v]){
low[u] = min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
cntc++;int v;
do{
v = stak[--cnts];
vis[v] = 0;
belong[v] = cntc;
}while(v!=u);
}
}
int n,m;
void tarjan(){
//初始化
for(int i = 0;i < 2*m;i++){
if(!dfn[i]){
dfs(i);
}
}
}
int main(){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
int from,to;
for(int i = 1;i <= m;i++){
scanf("%d%d",&from,&to);
if(from > to) swap(from,to);
a[i].from = from;a[i].to=to;
}
for(int i = 1;i <= m;i++){
for(int j = i+1;j <= m;j++){
if((a[j].to>=a[i].from&&a[j].to<=a[i].to&&a[j].from<=a[i].from)
||(a[i].to>=a[j].from&&a[i].to<=a[j].to&&a[i].from<=a[j].from) ){
ae(i,j+m);ae(j,i+m);ae(i+m,j);ae(j+m,i);
}
}
}
tarjan();
int flag = 1;
for(int i = 1;i <= m;i++){
if(belong[i] == belong[i+m]){
flag = 0;break;
}
}
if(flag == 1)
printf("panda is telling the truth...\n");
else
printf("the evil panda is lying again\n");
return 0;
}