POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT)
找好矛盾关系。矛盾关系是(2,5)和(3,6)这两个仅仅能一个在外边,一个在里边。利用这个矛盾关系来建图。
能够用在外边和里边来当1和0,最后推断每对是否出现矛盾。
代码例如以下:
- #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 __int64
- const 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;
- }
版权声明:本文博主原创文章,博客,未经同意,不得转载。
POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT)的更多相关文章
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...
- [2-SAT] poj 3207 Ikki's Story IV - Panda's Trick
题目链接: id=3207">http://poj.org/problem? id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1 ...
- Ikki's Story IV - Panda's Trick (poj 3207 2-SAT)
Language: Default Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072K Total S ...
- 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 (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
- 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)
Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 6691 ...
- POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT,基础)
题意: 有一个环,环上n个点,现在在m个点对之间连一条线,线可以往圆外面绕,也可以往里面绕,问是否必定会相交? 思路: 根据所给的m条边可知,假设给的是a-b,那么a-b要么得绕环外,要么只能在环内, ...
- poj 3207 Ikki's Story IV - Panda's Trick【2-SAT+tarjan】
注意到相交的点对一定要一里一外,这样就变成了2-SAT模型 然后我建边的时候石乐志,实际上不需要考虑这个点对的边是正着连还是反着连,因为不管怎么连,能相交的总会相交,所以直接判相交即可 然后tarja ...
随机推荐
- Oracle中decode函数 列变成行
create table t_class(c_Id number(10) primary key ,stuName varchar2(50), --人名c_Name varchar2(50), ...
- 《JavaScript设计模式与开发实践》读书笔记之策略模式
1.策略模式 定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换 1.1 传统实现 根据工资基数和年底绩效来发送年终奖 var calculateBonus= function (perfo ...
- Android编程之LayoutInflater的inflate方法实例
假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...
- 同时显示多个 Notification
主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...
- Android (1) - Activity
onCreate(Bundle status) --> setContentView(View view) --> findViewById(int id) Intent intentFo ...
- C语言 cgi(2)
1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #3, 40 pointsJune 10, 2014This lab ...
- 局域网连接SQL Server数据库配置
首先要保证两台机器位于同一局域网内,然后打开配置工具→SQL Server配置管理器进行配置.将MSSQLSERVER的协议的TCP/IP的(IP1.IP2)TCPport改为1433,已启用改为是. ...
- 十步完全理解SQL(转)
本文由 伯乐在线 - 水果泡腾片 翻译.未经许可,禁止转载!英文出处:Lukas Eder.欢迎加入翻译组. 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同 ...
- decorate pattern 装饰模式
[装饰模式的优缺点]装饰模式的优点:1.比静态继承更灵活:2.避免在层次结构高层的类有太多的特征装饰模式的缺点:1.使用装饰模式会产生比使用继承关系更多的对象.并且这些对象看上去都很想像,从而使得查错 ...
- go语言实现遍历目录,及查找特定的文件类型
// filelist.go package main import ( //"flag" "fmt" "os" "path/fi ...