POJ 3207 2-SAT
来源:互联网 发布:八爪盒子 知乎 编辑:程序博客网 时间:2024/04/30 16:35
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 20 13 2
虽然kb大神(orz)说这是一道2-sat的水题
但是我觉得这道题如果能理解了对2-sat算法有很好的理解
他的意思是。线是不能相交的。只能从圆的内部走或者从圆的外部走。给你N条线段。问你这些线段可否按照前面的规则画出来
假设一个圆上有4个点 1 3 连在一起 2 4 连在一起 那么1 3 和2 4 不能同侧
于是就应该判断一下 如果这两条线不能在圆的一边 那么就应该把它们分开来 (代码中的那一长串的IF判断)
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; //HDU3207 //****************************************** //2-SAT 强连通缩点 const int MAXN = 2010; const int MAXM = 4000010; struct Edge { int to,next; }edge[MAXM]; int head[MAXN],tot; void init() { tot = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值1~scc int Index,top; int scc; bool Instack[MAXN]; int num[MAXN]; void Tarjan(int u) { int v; Low[u] = DFN[u] = ++Index; Stack[top++] = u; Instack[u] = true; for(int i = head[u];i != -1;i = edge[i].next) { v = edge[i].to; if( !DFN[v] ) { Tarjan(v); if(Low[u] > Low[v])Low[u] = Low[v]; } else if(Instack[v] && Low[u] > DFN[v]) Low[u] = DFN[v]; } if(Low[u] == DFN[u]) { scc++; do { v = Stack[--top]; Instack[v] = false; Belong[v] = scc; num[scc]++; } while(v != u); } } bool solvable(int n)//n是总个数,需要选择一半 { memset(DFN,0,sizeof(DFN)); memset(Instack,false,sizeof(Instack)); memset(num,0,sizeof(num)); Index = scc = top = 0; for(int i = 0;i < n;i++) if(!DFN[i]) Tarjan(i); //for(int i = 0;i < n;i += 2) printf("belong[%d] = %d\n belong[%d] = %d\n",i,Belong[i],i^1,Belong[i^1]); for(int i = 0;i < n;i += 2) { if(Belong[i] == Belong[i^1]) return false; } return true; } //************************************************* //拓扑排序求任意一组解部分 /* queue<int>q1,q2; vector<vector<int> > dag;//缩点后的逆向DAG图 char color[MAXN];//染色,为'R'是选择的 int indeg[MAXN];//入度 int cf[MAXN]; void solve(int n) { dag.assign(scc+1,vector<int>()); memset(indeg,0,sizeof(indeg)); memset(color,0,sizeof(color)); for(int u = 0;u < n;u++) for(int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].to; if(Belong[u] != Belong[v]) { dag[Belong[v]].push_back(Belong[u]); indeg[Belong[u]]++; } } for(int i = 0;i < n;i += 2) { cf[Belong[i]] = Belong[i^1]; cf[Belong[i^1]] = Belong[i]; } while(!q1.empty())q1.pop(); while(!q2.empty())q2.pop(); for(int i = 1;i <= scc;i++) if(indeg[i] == 0) q1.push(i); while(!q1.empty()) { int u = q1.front(); q1.pop(); if(color[u] == 0) { color[u] = 'R'; color[cf[u]] = 'B'; } int sz = dag[u].size(); for(int i = 0;i < sz;i++) { indeg[dag[u][i]]--; if(indeg[dag[u][i]] == 0) q1.push(dag[u][i]); } } } */ int main() { int n,m; int a1,a2,c1,c2;int x[1111],y[1111]; scanf("%d%d",&n,&m); init(); for(int i=0;i<m;i++){scanf("%d%d",&x[i],&y[i]);if(x[i]>y[i]){int tem=x[i];x[i]=y[i];y[i]=tem;}}for(int i=0;i<m;i++){for(int j=i+1;j<m;j++){if((x[i]<=x[j]&&y[i]<=y[j]&&y[i]>=x[j])||(x[i]>=x[j]&&y[i]>=y[j]&&y[j]>=x[i])){ //判断他是否能在一边完成 addedge(i*2,j*2+1);addedge(j*2,i*2+1);addedge(i*2+1,j*2);addedge(j*2+1,j*2);}}} if(solvable(m*2)){ printf("panda is telling the truth...\n"); } else printf("the evil panda is lying again\n"); return 0; }
- POJ 3207 2-sat
- poj 3207 2-sat
- 【2-SAT】POJ 3207
- POJ 3207 2-SAT
- poj 3207 2-sat
- poj 3207 2-SAT
- POJ 3207 2-SAT
- POJ 3207 2-sat
- poj 3207(2-sat判断)
- POJ 3207 2SAT入门
- POJ-3207(2-SAT)
- poj 3207(2-SAT)
- 2-SAT ( Tarjan )——POJ 3207
- 2-sat POJ 3678
- poj 3683【2-SAT】
- 【2-SAT】POJ 3648
- 【2-SAT】POJ 3678
- POJ 3678 2-sat
- centos 关闭进程
- 使用unslider.js图片轮播插件创建一个能轮播24节气的网页
- 前端面试题集锦
- Linux 操作系统初探
- RTMP 封包详解
- POJ 3207 2-SAT
- H264视频通过RTMP直播 .
- ubuntu14.04 配置hadoop
- POJ 2586 贪心
- XMPP客户端库Smack 4.1.4版官方开发文档之六
- 基于HTML5的WebGL应用内存泄露分析
- OC基础第一章:OC基础语法
- event_base回调解析
- 中文分词算法