hdu 5874 构造



链接:戳这里


Friends and Enemies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
On an isolated island, lived some dwarves. A king (not a dwarf) ruled the island and the seas nearby, there are abundant cobblestones of varying colors on the island. Every two dwarves on the island are either friends or enemies. One day, the king demanded that each dwarf on the island (not including the king himself, of course) wear a stone necklace according to the following rules:
  
  For any two dwarves, if they are friends, at least one of the stones from each of their necklaces are of the same color; and if they are enemies, any two stones from each of their necklaces should be of different colors. Note that a necklace can be empty.
  
  Now, given the population and the number of colors of stones on the island, you are going to judge if it's possible for each dwarf to prepare himself a necklace.
 
Input
Multiple test cases, process till end of the input. 
  
  For each test case, the one and only line contains 2 positive integers M,N (M,N<231) representing the total number of dwarves (not including the king) and the number of colors of stones on the island.
 
Output
For each test case, The one and only line of output should contain a character indicating if it is possible to finish the king's assignment. Output ``T" (without quotes) if possible, ``F" (without quotes) otherwise.
 
Sample Input
20 100
 
Sample Output
T


题意:

有n个人,m种颜色的石头,任意两个人之间的关系可以是敌人也可以是朋友,但是只能是其中一种,每个人可以用选择若干石头或者不带,要求朋友的关系是两个人之间至少有一种颜色相同的石头,敌人是不能有相同颜色的石头,注意若一个人没有携带石头则与任意的人都是敌人,现在问最坏的情况,m种颜色的石头是否能满足条件(这里的满足条件是指所有n个人之间所有的关系都可以构造出来)


思路:

反正OEIS就对了

链接


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll n,m;
int main(){
    while(scanf("%I64d%I64d",&n,&m)!=EOF){
        ll x=floor(n*n/4LL);
        if(m>=x) printf("T\n");
        else printf("F\n");
    }
    return 0;
}


<think>我们正在处理用户关于HDU3062的请求。首先,我们需要明确HDU3062的具体内容。HDU(杭州电子科技大学在线评测系统)的题目编号3062对应具体的题目。根据常见的HDU题目,3062是“Party”问题,这是一个典型的2-SAT(2-Satisfiability)问题。2-SAT问题是给定一组布尔变量,以及由这些变量组成的二元子句(每个子句是两个文字的析取),要求确定是否存在一组真值赋值使得所有子句都满足。题目大意:有n对夫妻被邀请参加一个聚会,因为场地问题,每对夫妻中只有一个人可以参加。在这些人中,存在一些矛盾关系(比如两个人之间有过节),因此他们不能同时出现在聚会上。要求确定是否存在一种方案,使得每对夫妻恰好有一个人参加,且没有矛盾的人同时在场。输入格式:输入包含多个测试用例。每个测试用例的第一行是两个整数n和m,分别表示夫妻的对数和矛盾关系的数量。接下来m行,每行四个整数a,i,b,j,表示第a对夫妻的i(0表示妻子,1表示丈夫)和第b对夫妻的j不能同时出现。输出格式:对于每个测试用例,如果存在可行方案,输出"YES",否则输出"NO"。解决2-SAT问题通常使用强连通分量(SCC)算法。基本步骤:1.构造有向图:每个变量x有两个结点,分别表示x为真和x为假(在本题中,每对夫妻用变量x表示,x为真表示丈夫参加,x为假表示妻子参加)。然后根据矛盾关系建立有向边。规则是:如果条件“a的i和b的j不能同时出现”,那么可以推导出两个蕴含关系:-如果a的i出现,则b的j不能出现,即b的j必须由另一个人(即b的1-j)出现。所以有一条边:a_i->b_{1-j}-同理,如果b的j出现,则a的i不能出现,即a_i必须由另一个人(即a的1-i)出现。所以有一条边:b_j->a_{1-i}2.求强连通分量:使用Tarjan算法或Kosaraju算法求出有向图中的所有强连通分量。3.判断可行性:对于每个变量x,如果x和¬x在同一个强连通分量中,则问题无解;否则有解。因此,我们可以编写代码解决此问题。下面给出一个使用Tarjan算法求SCC的解决方案(使用C++语言,因为HDU主要接受C++/C/Java等语言):</think>### HDU 3062 "Party" 问题分析与解决方案 **问题描述** 给定 $n$ 对夫妻参加聚会,每对夫妻仅允许一人出席(丈夫或妻子)。同时存在 $m$ 组矛盾关系:每组矛盾指定两对夫妻中的特定成员(如第 $a$ 对夫妻的 $i$ 号成员与第 $b$ 对夫妻的 $j$ 号成员)不能同时出席。要求判断是否存在合法的出席方案,满足所有约束条件。 #### 核心算法:2-SAT 模型 该问题可抽象为 **2-SAT(2-Satisfiability)问题**: 1. **变量定义**:为每对夫妻 $k$ 定义布尔变量 $x_k$: - $x_k = \text{true}$ → 丈夫出席 - $x_k = \text{false}$ → 妻子出席 2. **矛盾关系转化为子句**: 对于矛盾关系 $(a,i,b,j)$,生成两个蕴含子句: - 若 $a$ 的 $i$ 出席,则 $b$ 的 $j$ 必须缺席 → $x_a = i \implies x_b \neq j$ - 若 $b$ 的 $j$ 出席,则 $a$ 的 $i$ 必须缺席 → $x_b = j \implies x_a \neq i$ 等价于析取子句: $$ (x_a \neq i) \lor (x_b \neq j) $$ 3. **建图规则**:构建有向图 $G=(V,E)$,其中: - 每个变量 $x_k$ 拆分为两个结点:$k_{\text{true}}$(丈夫)、$k_{\text{false}}$(妻子) - 子句 $(p \lor q)$ 转化为两条边:$\neg p \to q$ 和 $\neg q \to p$ **示例**:矛盾 $(a,0,b,1)$ 的建边: - $a_{\text{true}} \to b_{\text{false}}$(若 $a$ 的丈夫出席,则 $b$ 的妻子必须出席) - $b_{\text{true}} \to a_{\text{false}}$(若 $b$ 的丈夫出席,则 $a$ 的妻子必须出席) #### 求解步骤 ```python # 使用Tarjan算法求强连通分量(SCC) def tarjan(u): global index, scc_count index += 1 dfn[u] = low[u] = index stack.append(u) in_stack[u] = True for v in graph[u]: if not dfn[v]: tarjan(v) low[u] = min(low[u], low[v]) elif in_stack[v]: low[u] = min(low[u], dfn[v]) if dfn[u] == low[u]: scc_count += 1 while True: v = stack.pop() in_stack[v] = False scc_id[v] = scc_count if v == u: break # 主求解函数 def solve(n, m): # 初始化图:共2n个结点(每对夫妻两个状态) graph = [[] for _ in range(2 * n)] # 添加矛盾关系对应的边 for _ in range(m): a, i, b, j = map(int, input().split()) # 将夫妻编号转换为变量索引:a_true = 2a, a_false = 2a+1 node_a = 2 * a + (1 - i) # 矛盾中a的相反状态 node_b = 2 * b + j # 矛盾中b的指定状态 # 添加边: ¬a → b 和 ¬b → a graph[node_a].append(node_b) graph[node_b ^ 1].append(node_a ^ 1) # 注意:¬b对应b状态的异或1 # 求SCC global dfn, low, scc_id, stack, in_stack, index, scc_count size = 2 * n dfn = [0] * size; low = [0] * size; scc_id = [0] * size stack = []; in_stack = [False] * size; index = scc_count = 0 for i in range(size): if not dfn[i]: tarjan(i) # 检查可行性:若x与¬x在同一SCC则无解 for i in range(0, size, 2): if scc_id[i] == scc_id[i ^ 1]: return "NO" return "YES" ``` #### 算法正确性证明 2-SAT 问题有解当且仅当: $$ \forall x_i,\quad \text{SCC}(x_i) \neq \text{SCC}(\neg x_i) $$ 此性质由蕴含图的对称性保证[^5]。Tarjan 算法在 $O(n+m)$ 时间内完成求解,符合题目要求($n \leq 1000$)。 #### 输入输出示例 **输入**: ``` 1 # 测试用例数 2 1 # 2对夫妻,1组矛盾 0 1 1 0 # 第0对丈夫与第1对妻子矛盾 ``` **输出**: ``` YES ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值