[POJ3207]Ikki's Story IV - Panda's Trick

2023-11-11 01:30
文章标签 trick iv panda story ikki poj3207

本文主要是介绍[POJ3207]Ikki's Story IV - Panda's Trick,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

poj3207-Ikki’s Story IV - Panda’s Trick

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

输入
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 a line, either “panda is telling the truth…” or “the evil panda is lying again”.

样例输入
4 2
0 1
3 2

样例输出
panda is telling the truth…

SOL
2-SAT模板。
题面的大致意思是:在一个圆上有n个点,m条边,边可以在圆外或圆内,边与边之间不能相交,求是否有合法解。
对于这道题,相交只有一种情况:
在这里插入图片描述
那么有2种方法可以使其不相交:
在这里插入图片描述
于是考虑将每条边转换成两个点,表示圆外边和圆内边,于是就可以转换成2-SAT问题
效率是 O ( M 2 ) O(M^2) O(M2)的(即枚举连边的复杂度)

代码:

#include<stack>
#include<cstdio>
#include<iostream>
#define N 1005
#define M 505
#define T 1000005
#define re register
#define ll long long
using namespace std;
inline int rd(){int data=0;static char ch=0;ch=getchar();while(!isdigit(ch))ch=getchar();while(isdigit(ch))data=(data<<1)+(data<<3)+ch-'0',ch=getchar();return data;
}
int n,m,scc,tim,col[N],low[N],dfn[N];bool vis[N];
stack<int>s;
struct edge{int u,v;}E[M];
struct node{int v,nxt;}e[T];int cnt,first[N];
inline void add(int u,int v){e[++cnt].v=v;e[cnt].nxt=first[u];first[u]=cnt;}
void dfs(int u){low[u]=dfn[u]=++tim;s.push(u);vis[u]=1;for(int re i=first[u];i;i=e[i].nxt){int re v=e[i].v;if(!dfn[v]){dfs(v);low[u]=min(low[u],low[v]);}else if(vis[v])low[u]=min(low[u],dfn[v]);}if(low[u]==dfn[u]){++scc;while(1){int re t=s.top();s.pop();col[t]=scc;vis[t]=0;if(t==u)break;}}
}
inline bool check(int a,int b){if(E[a].v<=E[b].u||E[a].u>=E[b].v)return true;if(E[a].u<=E[b].u&&E[a].v>=E[b].v)return true;if(E[b].u<=E[a].u&&E[b].v>=E[a].v)return true;	return false;
}
inline void con(int u,int v){add(u,v),add(v,u);}
inline void link(int u,int v){con(u<<1,v<<1|1),con(v<<1,u<<1|1);}
int main(){n=rd();m=rd();for(int re i=0;i<m;i++){int re x=rd(),y=rd();if(x>y)swap(x,y);E[i].u=x,E[i].v=y;}for(int re i=0;i<m;i++)for(int re j=0;j<i;j++)if(!check(i,j))link(i,j);for(int re i=0;i<m*2;i++)if(!dfn[i])dfs(i);for(int re i=0;i<m;i++){if(col[i<<1]==col[i<<1|1]){cout<<"the evil panda is lying again";return 0;};}cout<<"panda is telling the truth...";return 0;
}

这篇关于[POJ3207]Ikki's Story IV - Panda's Trick的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/386639

相关文章

【python因果推断库7】使用 pymc 模型的工具变量建模 (IV)2

目录 与普通最小二乘法 (OLS) 的比较 应用理论:政治制度与GDP 拟合模型:贝叶斯方法  多变量结果和相关性度量 结论 与普通最小二乘法 (OLS) 的比较 simple_ols_reg = sk_lin_reg().fit(X.reshape(-1, 1), y)print("Intercept:", simple_ols_reg.intercept_, "Bet

【python因果推断库6】使用 pymc 模型的工具变量建模 (IV)1

目录 使用 pymc 模型的工具变量建模 (IV) 使用 pymc 模型的工具变量建模 (IV) 这份笔记展示了一个使用工具变量模型(Instrumental Variable, IV)的例子。我们将会遵循 Acemoglu, Johnson 和 Robinson (2001) 的一个案例研究,该研究尝试解开强大的政治机构对于以国内生产总值(GDP)衡量的经济生产力的影响。本示例借鉴

iOS CoreAudio学习笔记(二)—— The Story of Sound

在上一章,我们初次尝试了CoreAudio API:它提供了什么以及怎样调用它的函数。现在是时候往回一步来看看一张更大的图:一开始CoreAudio访问的问题。 这一章将介绍基础的声音科学,它是什么,它怎样工作。事实证明,计算机的数字化天性使它们并不那么适合处理连续的模拟信号。这引导了对信号采样的思想,或者将平滑的声波斩为频率足够大的离散值,而人耳无法注意到差别。这一章覆盖了这些采样在数字化形态

【LLM】文生视频相关开源数据集(VidGen、Panda、Cogvideox等)

note 总结了VidGen数据集、Panda-70m数据集、Openvid数据集、OpenVid-1M数据集、Cogvideox训练数据准备过程、ShareGPT4Video数据集等在一篇综述中还总结了评估指标包括:峰值信噪比(PSNR)、结构相似性指数(SSIM)、Inception 分数(IS)、Fréchet Inception 距离(FID)、CLIP 分数、视频 Inception

网络层 IV(ARP、DHCP、ICMP)【★★★★★★】

(★★)代表非常重要的知识点,(★)代表重要的知识点。 一、地址解析协议(ARP)(★★) 在局域网中,由于硬件地址已固化在网卡上的 ROM 中,因此常常将硬件地址称为物理地址。因为在局域网的 MAC 帧中的源地址和目的地址都是硬件地址,因此硬件地址又称为 MAC 地址。即物理地址、硬件地址和 MAC 地址常常作为同义词出现。 1. IP 地址与 MAC 地址 从层次的角度看,

Leetcode面试经典150题-188.买卖股票的最佳时机IV

解法都在代码里,不懂就留言或者私信,这个稍微难点,我提供了两种解法 /**基本的动态规划求解的过程 */public static int maxProfit2(int k, int[] prices) {/**题目给的数组不会为null,这里习惯性的健壮性判断如果交易日小于2,不可能获得任何的利润 */if(prices == null || prices.length < 2) {retu

Python | Leetcode Python题解之第377题组合总和IV

题目: 题解: class Solution:def combinationSum4(self, nums: List[int], target: int) -> int:dp = [1] + [0] * targetfor i in range(1, target + 1):for num in nums:if num <= i:dp[i] += dp[i - num]return dp

C++ | Leetcode C++题解之第377题组合总和IV

题目: 题解: class Solution {public:int combinationSum4(vector<int>& nums, int target) {vector<int> dp(target + 1);dp[0] = 1;for (int i = 1; i <= target; i++) {for (int& num : nums) {if (num <= i && d

MemSQL Start[c]UP 2.0 - Round 1 C. Magic Trick

Codeforces MemSQL Start[c]UP 2.0 - Round 1 C. Magic Trick 首先,我们先假设有抽出的牌样式为A 则,抽到同样的牌(不是同样类型)的概率为 1 / N 则,抽到不同的牌的概率为 N-1 / N 此时抽到A类型的概率为,在原来的N*M张中去掉我们最先抽出的一张A,再从中抽出剩下的 M-1张A类牌 综上所述,答案为 1 / N + (

算法笔记|Day31动态规划IV

算法笔记|Day31动态规划IV ☆☆☆☆☆leetcode 1049.最后一块石头的重量II题目分析代码 ☆☆☆☆☆leetcode 494.目标和题目分析代码 ☆☆☆☆☆leetcode 474.一和零题目分析代码 ☆☆☆☆☆leetcode 1049.最后一块石头的重量II 题目链接:leetcode 1049.最后一块石头的重量II 题目分析 此题可以将stones