poj3207:http://poj.org/problem?id=3207

题意::平面上有一个圆,圆的边上按顺时针放着0..n-1共n个点。现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接。给你的信息中,每个点最多只能连一条边。问是否可以连接这m条边,使这些边都不相交。

题解:第一道2-sat。看了别人的题解,才理解了这样的想法。对于一条边,要么在圆内要么在圆外。两条相交的边不能同时在园内或者同时在圆外,所以一条在园内,一条在圆外。这样就是2-sat问题了。这一题一定要注意边的数量,不是500,是很大的。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
const int M=;
struct Node{
int u;
int v;
}e[N*];
struct Edge{
int to,next;
} edge[M]; int n,m,cnt,dep,top,atype;
int dfn[N],low[N],vis[N],head[N],st[N],belong[N],in[N],out[N],sum[N];
//sum[i]记录第i个连通图的点的个数,in[i],out[i],表示缩点之后点的入度和初度。
void init(){
cnt=dep=top=atype=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(belong,,sizeof(belong));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
}
void addedge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
st[top++]=u;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=st[--top];
belong[j]=atype;
sum[atype]++; //记录每个连通分量中点的个数
vis[j]=;
}
while(u!=j);
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=m;i++){
scanf("%d%d",&e[i].u,&e[i].v);
}
init();
for(int i=;i<=m;i++){
for(int j=i+;j<=m;j++){
int a=e[i].u;int b=e[i].v;
int c=e[j].u;int d=e[j].v;
if((a<c&&c<b&&b<d)||(c<a&&a<d&&d<b)){
addedge(i,j+m);
addedge(j+m,i);
addedge(j,i+m);
addedge(i+m,j);
}
}
}
for(int i=;i<=m*;i++)
if(!dfn[i])Tarjan(i);
bool flag=false;
for(int i=;i<=m;i++){
if(belong[i]==belong[i+m]){
flag=true;
} }
if(!flag){
printf("panda is telling the truth...\n");
}
else
printf("the evil panda is lying again\n");
}
}

Ikki's Story IV - Panda's Trick的更多相关文章

  1. 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   ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. 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   ...

  6. POJ3207 Ikki's Story IV – Panda's Trick

    Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9426   Accepted: 3465 Description liym ...

  7. 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 ...

  8. 图论--2-SAT--POJ Ikki's Story IV - Panda's Trick

    Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping ...

  9. 【POJ】3207 Ikki's Story IV - Panda's Trick

    http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...

  10. poj3207 Ikki’s Story IV – Panda’s Trick

    2-SAT. tarjan缩点.强连通分量的点要选一起选. #include<cstdio> #include<algorithm> #include<cstring&g ...

随机推荐

  1. android 75 新闻列表页面

    new.xml <?xml version="1.0" encoding="UTF-8" ?> <newslist> <news& ...

  2. 【转】Enable ARC in a Cocos2D Project: The Step-by-Step-How-To-Guide Woof-Woof!

    On April 5, 2012, in idevblogaday, by Steffen Itterheim http://www.learn-cocos2d.com/2012/04/enablin ...

  3. 用标准版的Eclipse搭建PHP环境

    用标准版的Eclipse搭建PHP环境 ——@梁WP 摘要:用标准版的Eclipse搭建PHP环境. 一.下载Eclipse的PHP插件 百度搜索phpeclipse,看到某条结果是带有SourceF ...

  4. HDU-1020(水题)

    Encoding Problem Description Given a string containing only 'A' - 'Z', we could encode it using the ...

  5. php strtotime函数服务器和本地不相同

    遇到过一种情况strtotime 在本地和服务器不相同 服务器返回的是-1 strtotime($sa_sagyo_ymd."23:59:59") 如果这样用不了,就只能换一种写法 ...

  6. C#语法糖之第二篇: 参数默认值和命名参数 对象初始化器与集合初始化器

    今天继续写上一篇文章C#4.0语法糖之第二篇,在开始今天的文章之前感谢各位园友的支持,通过昨天写的文章,今天有很多园友们也提出了文章中的一些不足,再次感谢这些关心我的园友,在以后些文章的过程中不断的完 ...

  7. [日历] C#修改CNDate日历帮助类 (转载)

    点击下载 CNDate.rar 主要功能如下 .传回公历y年m月的总天数 .根据日期值获得周一的日期 .获取农历 #region 私有方法 private static long[] lunarInf ...

  8. 关于wordpress忘记密码 找回密码的方式

    1.通过直接修改数据库中密码的加密字符(如果wordpress的版本不同,那么此方法是不好实现的) 2.使用找回密码的方式:通过邮箱找回密码 前端登录密码错误后 会显示   错误); 方法二 打开WP ...

  9. 3DTouch

    3DTouch 一.主屏按压(Home Screen Quik Actions) 1.静态标签 在info.plist文件中新增项 关键字 意义 UIApplicationShortcutItems ...

  10. PHP 学习笔记 (一)

    1. 在PHP中设置最长执行时间: PHP中的PHP.ini文件中,max_execution_time 项指定了PHP最长执行时间,默认是30秒.有两种方案可以对其进行修改: 1. 直接在PHP.i ...