POJ 3207 Ikki's Story IV, pojikki
Address: POJ 3207
Find the conflicting relationship. The two can only be one outside and the other can be one inside.
It can be used outside and inside to be 1 and 0, and finally judge whether there is a conflict between each pair.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;int head[1100], cnt, top, ans, index;int dfn[1100], low[1100], instack[1100], stak[1100], belong[1100];struct node{ int u, v, next;}edge[1000000];struct N{ int l, r;}xian[10000];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}int pan(N x, N y){ if((x.l>y.l&&x.l<y.r&&x.r>y.r)||(x.r>y.l&&x.r<y.r&&x.l<y.l)) return 1; return 0;}void tarjan(int u){ dfn[u]=low[u]=++index; instack[u]=1; stak[++top]=u; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) { low[u]=min(dfn[v],low[u]); } } if(dfn[u]==low[u]) { ans++; while(1) { int v=stak[top--]; instack[v]=0; belong[v]=ans; if(u==v) break; } }}void init(){ memset(head,-1,sizeof(head)); cnt=top=ans=index=0; memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack));}int main(){ int n, m, i, j; scanf("%d%d",&n,&m); init(); for(i=0;i<m;i++) { scanf("%d%d",&xian[i].l,&xian[i].r); if(xian[i].l>xian[i].r) swap(xian[i].l,xian[i].r); } for(i=0;i<m;i++) { for(j=0;j<i;j++) { if(pan(xian[i],xian[j])) { add(i<<1,j<<1|1); add(j<<1,i<<1|1); add(i<<1|1,j<<1); add(j<<1|1,i<<1); //printf("%d %d\n%d %d\n",i<<1,j<<1|1,j<<1,i<<1|1); } } } for(i=0;i<2*m;i++) { if(!dfn[i]) tarjan(i); } int flag=0; for(i=0;i<m;i++) { if(belong[i<<1]==belong[i<<1|1]) { flag=1; //printf("%d\n",i); break; } } if(flag) puts("the evil panda is lying again"); else puts("panda is telling the truth..."); return 0;}