专栏首页CSDN旧文图论--2-SAT--POJ Ikki's Story IV - Panda's Trick

图论--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 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 2
0 1
3 2

Sample Output

panda is telling the truth...

题意

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

分析:

如1 5连边,2,6连边,由于点是顺序排列的,一画图就可以发现,这两条边必须一个从圆外面连,一个从内部连,否则就会相交。如果再加入3 7这条边,那么就必须相交了。

这样,就可以转化成标准的2-sta问题:

1:每个边看成2个点:分别表示在内部连接和在外部连接,只能选择一个。计作点i和点i'

2:如果两条边i和j必须一个画在内部,一个画在外部(一个简单判断就可以)

建图:看我博客2-SAT详解,比较水的建图方式,就不再这写了。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 4000
#define MAXM 500000
#define INF 10000000
using namespace std;
int n, m;
struct Edge
{
	int s, t;//起点 终点 
}num[MAXM];//存储每条线
vector<int> G[MAXN]; 
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
void init()
{
	for(int i = 1; i <= 3*m; i++) G[i].clear();
}
bool judge(int a, int b, int c, int d)//判断是否相交
{
	return (c > a && b > c && d > b) || (a > c && d > a && b > d);
} 
void getMap()
{
	for(int i = 1; i <= m; i++) scanf("%d%d", &num[i].s, &num[i].t);
	/* i + n表内 i +2*n表外*/ 
	for(int i = 1; i <= m; i++)
	{
		for(int j = 1; j < i; j++)
		{
			if(judge(num[i].s, num[i].t, num[j].s, num[j].t))//相交
			{
				G[i + 2*m].push_back(j + m);//i外 -> j内
				G[i + m].push_back(j + 2*m);//i内 -> j外 
				G[j + m].push_back(i + 2*m);//j内 -> i外 
				G[j + 2*m].push_back(i + m);//j外 -> i内 
			} 
		}
	}
}
void tarjan(int u, int fa)
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u); 
	Instack[u] = true;
	for(int i = 0 ;i < G[u].size(); i++)
	{
		v = G[u][i];
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[v], low[u]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u])
	{
		scc_cnt++;
		for(;;)
		{
			v = S.top(); S.pop();
			Instack[v] = false;
			sccno[v] = scc_cnt;
			if(v == u) break;
		}
	}
}
void find_cut(int l, int r)//tarjan求SCC 
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(sccno, 0, sizeof(sccno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = scc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1);
}
void solve()
{
	bool flag = true;
	for(int i = 1; i <= m; i++)
	{
		if(sccno[i + m] == sccno[i + 2*m])
		{
			flag = false;
			printf("the evil panda is lying again\n");
			break;
		}
	}
	if(flag)
	printf("panda is telling the truth...\n");
}
int main()
{
	scanf("%d%d", &n, &m);
	init();
	getMap();
	find_cut(1, 3*m);//按边处理  总编号为3*m 
	solve();
	return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • poj1251 Jungle Roads Kruskal算法+并查集

    风骨散人Chiam
  • 使用高级程序设计语言实现集合的交并差运算

    风骨散人Chiam
  • POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 248...

    风骨散人Chiam
  • 终极一战:为了编程面试!

    过去常常读一个问题,然后花几分钟把它映射到我以前见过的类似问题上。如果我可以映射它,我将只关注这个问题与父问题相比有哪些不同约束。如果这是一个新问题,那么我会尝...

    量化投资与机器学习微信公众号
  • Java整数相加溢出怎么办?Java 8 还是厉害!

    blog.csdn.net/qq_33330687/article/details/81626157

    趣学程序-shaofeer
  • Java整数相加溢出怎么办?Java 8 还是厉害!

    blog.csdn.net/qq_33330687/article/details/81626157

    JAVA葵花宝典
  • StackOverflow热帖:Java整数相加溢出怎么办?Java8一步搞定~

    来源:blog.csdn.net/qq_33330687/article/details/81626157

    良月柒
  • StackOverflow热帖:Java整数相加重叠怎么办?Java8一步搞定〜

    在之前刷题的时候遇见一个问题,需要解决int相加后如何判断是否重叠,如果重叠就返回Integer.MAX_VALUE

    会呼吸的Coder
  • Leetcode 111 Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number o...

    triplebee
  • HDU 3450 Counting Sequences(线段树)

    Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 3276...

    ShenduCC

扫码关注云+社区

领取腾讯云代金券