2-sat->poj 3207 Ikki's Story IV - Panda's Trick

本文介绍了一个关于连线的游戏问题,玩家需要判断是否能在圆内或圆外连接指定的点对而不让连线在圆内或圆外交叉(除了边界)。通过构建图论模型并使用Tarjan算法来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7258 Accepted: 2688

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


2-sta问题:

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

2:如果两条边i和j必须一个画在内部,一个画在外部

连边:

i->j’,

j->i’,

i’->j,

j’->i,



#include <iostream>
#include <cstdio>
#include <string.h>
#include <stack>

using namespace std;

#define MAXN 500 + 10
#define MAXV 1005

int c[MAXN][2];
int n, m;

struct Edge
{
	int v, next;
}edge[MAXV * MAXV];

int head[2 * MAXN], e, cnt;
int dfn[2 * MAXN], low[2 * MAXN], belong[2 * MAXN], ind;
bool ins[2 * MAXN];

void add(int u, int v)
{
	edge[e].v = v;
	edge[e].next = head[u];
	head[u] = e++;
}

void init()
{
	e = cnt = ind = 0;
	memset(head, -1, sizeof(head));
}

stack <int> s;

void tarjan(int u)
{
	dfn[u] = low[u] = ++ind;
	
	ins[u] = true;
	s.push(u);
	
	int v;
	
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		v = edge[i].v;
		if (!dfn[v])
		{
			tarjan(v);
			
			low[u] = min(low[u], low[v]);
		}
		else if (ins[v])
		{
			low[u] = min(low[u], dfn[v]);
		}
	}
	
	if (low[u] == dfn[u])
	{
		cnt++;
		
		int x;
		
		do
		{
			x = s.top();
			s.pop();
			ins[x] = false;
			belong[x] = cnt;
		}while (x != u);
	}
}

bool solve()
{
	for (int i = 0; i < m; i++)
	{
		if (!dfn[i])
		{
			tarjan(i);
		}
	}
	
	for (int i = 0; i < m; i++)
	{
		if (belong[i] == belong[i + m])
		{
			return false;
		}
	}
	
	return true;
}

void input()
{
	init();
	
	cin >> n >> m;
	
	for (int i = 0; i < m; i++)
	{
		cin >> c[i][0] >> c[i][1];
		if (c[i][0] > c[i][1])
		{
			swap(c[i][0], c[i][1]);
		}
	}
	
	for (int i = 0; i < m; i++)
	{
		for (int j = i + 1; j < m; j++)
		{
			if (c[i][0] > c[j][0] && c[i][0] < c[j][1] && c[i][1] > c[j][1] ||
				c[i][1] > c[j][0] && c[i][1] < c[j][1] && c[i][0] < c[j][0])
			{
				add(i, j + m);
				add(j, i + m);
				add(i + m, j);
				add(j + m, i);			
			}
		}
	}
	
	if (solve())
	{
		cout << "panda is telling the truth..."	 << endl;
	}
	else
	{
		cout << "the evil panda is lying again" << endl;
	}
}

int main()
{
	input();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值