Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7065 | Accepted: 2623 |
Description
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
”.
Sample Input
4 2 0 1 3 2
Sample Output
panda is telling the truth...
题意:有一个圆,圆上有n个点,现在要在一些点之间画条线,对于每个连线a b,线可以画在圆内或圆外,要求每两条线都不能相交,给出m个连线,问能否画出这m条线。
思路:2-SAT问题,将每条线拆成是两个点i、i+m,若连线i与连线j相交,则加边add(i,j+m),add(j+m,i),add(i+m,j),add(j,i+m).
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <map> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long using namespace std; const int maxn=1005; const int INF=1000000000; struct node { int v,next; }edge[maxn*maxn]; int head[maxn],scc[maxn],stack[maxn]; int low[maxn],dfn[maxn],s[maxn],e[maxn]; bool ins[maxn]; int n,m,num,cnt,top,snum; void init() { memset(head,-1,sizeof(head)); num=0; } void add(int u,int v) { edge[num].v=v; edge[num].next=head[u]; head[u]=num++; } void input() { for(int i=1;i<=m;i++) scanf("%d%d",&s[i],&e[i]); for(int i=1;i<=m;i++) for(int j=i+1;j<=m;j++) if((s[i]<=s[j]&&s[j]<=e[i]&&e[i]<=e[j])||(s[j]<=s[i]&&s[i]<=e[j]&&e[j]<=e[i])) { add(i,j+m); add(j+m,i); add(i+m,j); add(j,i+m); } } void dfs(int u) { int x; dfn[u]=low[u]=++cnt; stack[top++]=u; ins[u]=true; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if(ins[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { snum++; do{ x=stack[--top]; ins[x]=false; scc[x]=snum; }while(x!=u); } } void tarjan() { memset(dfn,0,sizeof(dfn)); memset(ins,false,sizeof(ins)); cnt=top=snum=0; for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i); } void solve() { for(int i=1;i<=m;i++) if(scc[i]==scc[i+m]) { printf("the evil panda is lying again\n"); return; } printf("panda is telling the truth...\n"); } int main() { while(~scanf("%d%d",&n,&m)) { init(); input(); tarjan(); solve(); } return 0; }