poj3207 Ikki's Story IV - Panda's Trick 2-sat问题
题意:给定一个圈,m条边(给定),边可以通过外面连,也可以通过里面连,
问连完这m条边后,是否可以做到边两两不相交
题解:
将连里面和连外面分别当做一种决策(即每条边都是决策点),
如果有两条边相冲突,即如果这两条边都连里面就会导致不合法,那就
x --- > y' , y --- > x',
额。。。那怎么判断不合法?
注意到被边u ---> v(u < v)割成两半的分别是:
u ~ v,其他,
一条边不经过这条边的充要条件是:两个端点都在这条边的同一侧。
也就是要么都属于u ~ v,要么都属于其他。
x = x * 2,表示连里面
x = x ^ 1,表示连外面
连边的时候记得双向建边,不然是不可能有大于1的强联通分量的(因为对于任意x点只有出边,任意x'点只有入边)
找到一组冲突的就连x --- > y', y --- > x'.
表示x连里面,y就要放外面,反之同理
#include<fstream>//文件输入输出
using namespace std;
#define R register int
#define getchar() *o++
#define AC 2200
#define ac 4000100
char READ[],*o=READ;
int n, m, all, tt, cnt;
int low[AC], dfn[AC], belong[AC];
int date[ac], Next[ac], Head[AC], tot;
int s[AC], top;
bool z[AC];
struct node{
int x,y;
}way[AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void add(int f,int w)
{
date[++tot] = w, Next[tot] = Head[f], Head[f] = tot;
date[++tot] = f, Next[tot] = Head[w], Head[w] = tot;//反之也成立,所以要加双向边,不然是不可能有几个连一起的
// printf("%d ---> %d\n",f,w);
} inline void upmin(int &a,int b)
{
if(b < a) a = b;
} void pre()
{
n=read(), m=read(), all = n * ;
for(R i=;i<=m;i++)
{
way[i].x = read(), way[i].y = read();
if(way[i].x > way[i].y) swap(way[i].x, way[i].y);
for(R j=;j<i;j++)//防止重边
{
if(way[j].x > way[i].x && way[j].y < way[i].y) continue;
if(way[j].x < way[i].x && way[j].y < way[i].x) continue;
if(way[j].x > way[i].y && way[j].y > way[i].y) continue;
if(way[j].x < way[i].x && way[j].y > way[i].y) continue;//剩下的就都是冲突的
add(i * , (j * ) ^ );
add(j * , (i * ) ^ );
}
}
} void tarjan(int x)
{
int now;
dfn[x] = low[x] = ++tt;
s[++top] = x, z[x] = true;
for(R i = Head[x]; i ; i = Next[i])
{
now = date[i];
if(!dfn[now])
{
tarjan(now);
upmin(low[x], low[now]);
}
else if(z[now])
upmin(low[x], low[now]);
}
int b = ++cnt;
if(dfn[x] == low[x])
{
while(now = s[top--])
{
belong[now] = b;
z[now] = false;
if(now == x) break;
}
}
} void work()
{
for(R i = ; i <= all; i += )
{
if(belong[i] == belong[i ^ ])
{
printf("the evil panda is lying again\n");
return ;
}
}
printf("panda is telling the truth...\n");
} int main()
{
freopen("in.in","r",stdin);
fread(READ, , , stdin);
pre();
for(R i=;i<=all;i++)
if(!dfn[i]) tarjan(i);
work();
fclose(stdin);
return ;
}
poj3207 Ikki's Story IV - Panda's Trick 2-sat问题的更多相关文章
- POJ3207 Ikki's Story IV – Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9426 Accepted: 3465 Description liym ...
- POJ3207 Ikki's Story IV - Panda's Trick 【2-sat】
题目 liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikk ...
- poj3207 Ikki’s Story IV – Panda’s Trick
2-SAT. tarjan缩点.强连通分量的点要选一起选. #include<cstdio> #include<algorithm> #include<cstring&g ...
- POJ-3207 Ikki's Story IV - Panda's Trick 2sat
题目链接:http://poj.org/problem?id=3207 题意:在一个圆圈上有n个点,现在用线把点两两连接起来,线只能在圈外或者圈内,现给出m个限制,第 i 个点和第 j 个点必须链接在 ...
- poj3207 Ikki's Story IV - Panda's Trick 2-SAT
题目传送门 题意:在一个圆上顺时针安放着n个点,给出m条线段连接端点,要求线段不相交,线段可以在圆内也可以在圆外,问是否可以. 思路:假设一条线段,放在圆外是A,放在园内是A',那么两条线段如果必须一 ...
- 【POJ3207】Ikki's Story IV - Panda's Trick
POJ 3207 Ikki's Story IV - Panda's Trick liympanda, one of Ikki's friend, likes playing games with I ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...
- POJ 3207 Ikki's Story IV - Panda's Trick
Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7296 ...
- poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
随机推荐
- Jsp刷新分页模板,很全
1.用来实现上一页下一页,我直接写到查询页面上 <%--page的分页--%> <style type="text/css"> a { color: # ...
- 14、Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch ...
- Andorid自定义attr的各种坑
本文来自网易云社区 作者:孙有军 在开发Andorid应用程序中,经常会自定义View来实现各种各样炫酷的效果,在实现这吊炸天效果的同时,我们往往会定义很多attr属性,这样就可以在XML中配置我们想 ...
- 一个CookieContainer的拓展类
最近项目中需要频繁用到服务器返回的Cookie,由于项目采用的是HttpClient,并且用CookieContainer自动托管Cookie,在获取Cookie的时候不太方便.所以就写了个拓展类. ...
- 成员变量:对象vs指针
一旦类初始化,那么对象必然会被创建,指针则可以在需要时候再去初始化所指向.
- Java: Replace a string from multiple replaced strings to multiple substitutes
Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...
- 初学Direct X(3)
初学Direct X(3) 1.获取外设输入--键盘以及鼠标 无论是获取鼠标还是键盘的设备,首先得初始化DirectInput,不过先把必要的环境先配置好: 所要用到的头文件以及库文件是(相比于前两次 ...
- 【C++模版之旅】项目中一次活用C++模板(traits)的经历 -新注解
问题与需求: 请读者先看这篇文章,[C++模版之旅]项目中一次活用C++模板(traits)的经历. 对于此篇文章提出的问题,我给出一个新的思路. talking is cheap,show me t ...
- 【WXS数据类型】Boolean
属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...
- lintcode174 删除链表中倒数第n个节点
删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 注意事项 链表中的节点个数大于等于n 您在真实的面试中是否遇到过这个题? Yes 样例 给出链表1->2 ...