POJ - 3207 Ikki’s Story IV - Panda’s Trick
Time limit 1000 ms
Memory limit 131072 kB
liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.
liympanda has a magic circle and he puts it on a plane, there are n points on its boundary in circular border: 0, 1, 2, …, n − 1. Evil panda claims that he is connecting m pairs of points. To connect two points, liympanda either places the link entirely inside the circle or entirely outside the circle. Now liympanda tells Ikki no two links touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether this is possible…
Despaired at the minesweeping game just played, Ikki is totally at a loss, so he decides to write a program to help him.
Input
The input contains exactly one test case.
In the test case there will be a line consisting of of two integers: n and m (n ≤ 1,000, m ≤ 500). The following m lines each contain two integers ai and bi, which denote the endpoints of the ith wire. Every point will have at most one link.
Output
Output a line, either “panda is telling the truth…” or “the evil panda is lying again”.
题目大意
平面上有一个圆
圆的边界上顺时针分布着n个点
有m条线链接任意两个点,这些线可以在圆外或圆内,但不相交
保证每个点至多只连一条线,问是否合法
题目分析
对每条线用两个点分别表示在圆内(i)还是在圆外(i+m)
将m条线枚举两两组合
设当前枚举到的两条线为i和j
若他们同在圆内时会相交(可以证明此时如果同在圆外也会相交)
那么连边
a
d
d
(
i
,
j
+
m
)
,
a
d
d
(
i
+
m
,
j
)
,
a
d
d
(
j
,
i
+
m
)
,
a
d
d
(
j
+
m
,
i
)
add(i,j+m),add(i+m,j),add(j,i+m),add(j+m,i)
add(i,j+m),add(i+m,j),add(j,i+m),add(j+m,i)
表示如果其中一条在圆内(外),另一条必须在圆外(内)
之后2-SAT判断即可
#include<iostream>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const int maxn=2010;
int n,m,test;
struct node{int v,nxt;}E[2000010];
int head[maxn],tot;
struct node2{int x,y;}rem[maxn];
int low[maxn],dfn[maxn],cnt;
int col[maxn],ins[maxn],colnum;
int st[maxn],top;
void add(int u,int v)
{
E[++tot].nxt=head[u];
E[tot].v=v;
head[u]=tot;
}
void tarjan(int u)
{
low[u]=dfn[u]=++cnt;
st[++top]=u; ins[u]=1;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]);}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++colnum;
do{
col[st[top]]=colnum;
ins[st[top]]=0;
}
while(st[top--]!=u);
}
}
int judge(int a,int b,int c,int d)
{
if(a<c&&c<b&&b<d)return 1;
if(c<a&&a<d&&d<b)return 1;
return 0;
}
int main()
{
n=read();m=read();
for(int i=1;i<=m;++i)
{
int x=read()+1,y=read()+1;
if(x>y) swap(x,y);
rem[i].x=x; rem[i].y=y;
for(int j=1;j<i;++j)
if(judge(x,y,rem[j].x,rem[j].y))
add(i,j+m),add(i+m,j),add(j,i+m),add(j+m,i);
}
for(int i=1;i<=m<<1;++i)
if(!dfn[i])tarjan(i);
for(int i=1;i<=m;++i)
if(col[i]==col[i+m]){test=1;break;}
if(test)printf("the evil panda is lying again");
else printf("panda is telling the truth...");
return 0;
}
BZOJ1997 [HNOI2010]Planar
Time Limit: 10 Sec
Memory Limit: 64 MB
Description
Input
题目分析
和上题几乎一样,就是饶了点弯子告诉你圆上点的顺时针顺序
以及还得知道 平面图一定满足
m
≤
3
n
−
6
m\leq 3n-6
m≤3n−6这个结论,不然数据范围确实大的可怕了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long lt;
int read()
{
int x=0,f=1;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return x*f;
}
const int maxn=50010;
int T,n,m;
struct edge{int u,v;}rem[maxn];
struct node{int v,nxt;}E[maxn<<3];
int head[maxn],tot;
int dfn[maxn],low[maxn],col[maxn],colnum;
int st[maxn],ins[maxn],top,cnt;
int pos[maxn];
void add(int u,int v)
{
E[++tot].nxt=head[u];
E[tot].v=v;
head[u]=tot;
}
int check(int x,int y)
{
int a=rem[x].u,b=rem[x].v;
int c=rem[y].u,d=rem[y].v;
if(a<c&&c<b&&b<d) return 1;
if(c<a&&a<d&&d<b) return 1;
return 0;
}
void tarjan(int u)
{
dfn[u]=low[u]=++cnt;
st[++top]=u; ins[u]=1;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]);}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
colnum++;
do{
col[st[top]]=colnum;
ins[st[top]]=0;
}while(st[top--]!=u);
}
}
void init()
{
cnt=tot=0;
memset(head,0,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(col,0,sizeof(col));
}
int main()
{
T=read();
while(T--)
{
n=read();m=read(); init();
for(int i=1;i<=m;++i)
rem[i].u=read(),rem[i].v=read();
for(int i=1;i<=n;++i) pos[read()]=i;
if(m>3*n-6){ printf("NO\n"); continue;}
for(int i=1;i<=m;++i)
{
rem[i].u=pos[rem[i].u]; rem[i].v=pos[rem[i].v];
if(rem[i].u>rem[i].v) swap(rem[i].u,rem[i].v);
for(int j=1;j<i;++j)
if(check(i,j))
add(i,j+m),add(j,i+m),add(i+m,j),add(j+m,i);
}
for(int i=1;i<=m<<1;++i)
if(!dfn[i]) tarjan(i);
int judge=0;
for(int i=1;i<=m;++i)
if(col[i]==col[i+m]){ judge=1; break;}
if(judge) printf("NO\n");
else printf("YES\n");
}
return 0;
}