PARADOX dfs

本文介绍了一种解决Spoj平台Paradox问题的高效算法,通过检查路径长度来判断是否存在矛盾循环,从而识别出导致悖论的情况。

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

http://www.spoj.com/problems/PARADOX/

这题不知道为啥被归成dfs,一艘题解,甚是惊讶,竟然找出规律来了,佩服+1

多种讨论http://www.quora.com/What-is-the-algorithmic-approach-to-solving-the-problem-Paradox-on-SPOJ

A straightforward problem, if you can observed that there will be PARADOX if and only if there will be cycle containing an statement i and having odd number of false implies in that cycle.
For example,
A -> B false
B-> C false
C -> A false
There will be a contradiction at vertex A as in the Cycle A->B->C->A there will be odd number of false.
A simple ASAP algorithm will check that. Simply assign  edge weight  1 if statement a is saying statement b is false, and 2 as true and check whether path length from A to A having odd length.

#include <iostream>
using namespace std;

#define MX 10000000

int main() {
	// your code goes here
	int T;
	while(1)
	{
		int N,i,j,k;
		cin>>N;	
		if(N==0)
			return 0;
		int dis[N+1][N+1];
		for(i=0; i<=N ; i++)
			for(j=0 ; j<=N ; j++)
				dis[i][j] = MX;

		for(i=1 ; i<=N ; i++)
		{
			int x;
			string str;
			cin>>x>>str;
			if(str == "true")
				dis[i][x] = 2;
			else
				dis[i][x] = 1;
		}

		for(k=1;k<=N;k++)
		{
			for(i=1;i<=N;i++)
			{	
				for(j=1;j<=N;j++)
				{
					if(dis[i][j]>dis[i][k]+dis[k][j])
					{
						dis[i][j]=dis[i][k]+dis[k][j];
					}
				}
			}
		}

		int temp=0;
		for(i=1 ; i<=N ; i++)
		{
			if( dis[i][i] < MX/ 2 && dis[i][i] % 2 == 1)
			{
				cout<<"PARADOX"<<endl;
				temp = 1;
				break;
			}

		}
		if(temp == 0)
		{
			cout<<"NOT PARADOX"<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值