UVALive - 7427 the math 【二分匹配】
题目链接
题意
给出一个n
然后有n行
每行给出两个数
这两个数之间可以用三种操作 分别是 + - *
如果这n对数通过操作后 得到的结果都是不同的,那么这个方案就是符合条件的 输出任意一种可行方案
如果存在相同结果,那么就是不可行的
思路
可以把N对数看成一个点,把一对数通过三种操作后得到的结果也看成一个点,将他们之间连一条边
然后二分匹配 如果最大匹配数 == n 那么就有可行解
输出就好了
linker[] 里存放的就是匹配的方案
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 7e3 + 5e2 + 10;
const int MOD = 6;
const int MAXN = 7510;//点数的最大值
const int MAXM = 50010;//边数的最大值
struct Edge
{
int to, next;
}edge[MAXM];
int head[MAXN], tot;
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v)
{
edge[tot].to = v; edge[tot].next = head[u];
head[u] = tot++;
}
int linker[MAXN];
bool used[MAXN];
int uN;
bool dfs(int u)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!used[v])
{
used[v] = true;
if (linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker, -1, sizeof(linker));
for (int u = 0; u < uN; u++)//点的编号0~uN-1
{
memset(used, false, sizeof(used));
if (dfs(u))res++;
}
return res;
}
ll A[MAXN], B[MAXN], C[MAXN];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
init();
map <ll, int> mp;
int pos = 0;
for (int i = 0; i < n; i++)
{
scanf("%lld%lld", &A[i], &B[i]);
ll num;
num = A[i] + B[i];
if (mp[num] == 0)
{
C[pos] = num;
mp[num] = ++pos;
}
addedge(mp[num] - 1, i);
num = A[i] - B[i];
if (mp[num] == 0)
{
C[pos] = num;
mp[num] = ++pos;
}
addedge(mp[num] - 1, i);
num = A[i] * B[i];
if (mp[num] == 0)
{
C[pos] = num;
mp[num] = ++pos;
}
addedge(mp[num] - 1, i);
}
uN = pos;
if (hungary() < n)
puts("impossible");
else
{
for (int i = 0; i < n; i++)
{
ll num = C[linker[i]];
char c;
if (A[i] + B[i] == num)
c = '+';
else if (A[i] - B[i] == num)
c = '-';
else
c = '*';
printf("%lld %c %lld = %lld\n", A[i], c, B[i], num);
}
}
}
}
UVALive - 7427 the math 【二分匹配】的更多相关文章
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- BNUOJ 12756 Social Holidaying(二分匹配)
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=12756 Social Holidaying Time Limit: 3000ms Memo ...
- [kuangbin带你飞]专题十 匹配问题 二分匹配部分
刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...
- HDU 3468 BFS+二分匹配
九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- hdu3729二分匹配
I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
随机推荐
- hadoop:WordCount问题总结
今天玩了一下hadoop的MapReduce,中途遇到了几个问题,在此记录一下. 1.一切按照配置完成之后,hadoop namenode format,start-all.sh启动,使用jps查看进 ...
- LoadRunner变量到参数的互换
作者QQ:764714258,转载请说明出处,阅读此文需要良好的C基础 LoadRunner中,web性能测试使用的脚步是C语言编写的.C语言中有变量的概念,LoadRunner工具中带有参数这个概念 ...
- 摘录 LDAP
1.LDAP就是 light DAP, 轻量级目录访问协议 LDAP是轻量目录访问协议(Lightweight Directory Access Protocol)的缩写 LDAP标准 ...
- 浅谈"壳"(一)
壳,即坚硬的外皮,当壳的厚度与其曲面率半径的比值小于0.5时.称为"薄壳".反之称为"厚壳".由壳演化来的胸甲,盾牌. 在计算机这个注重创意又不失从文化科技中汲 ...
- ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine. 本文想针对某个Model,自定义该Model的专属视图 ...
- [转载]Axis2 and CXF的比较
在Celtix 和XFire 宣布合并的同年,另一个著名开源Web 服务框架Axis 的后继者Axis2 也诞生了.Axis2 并非Axis 的2.0 版,而是完全重写了Axis 的新项目.作为功能和 ...
- Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar
额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...
- C语言中的main函数以及main函数是如何被调用的
main函数是C语言中比较特殊的函数,C程序总是从main函数开始执行,main函数的原型是: int main(int argc, char *argv[]); 其中argc是命令行参数的个数,ar ...
- Nginx 配置指令的执行顺序
在一个 location 中使用 content 阶段指令时,通常情况下就是对应的 Nginx 模块注册该 location 中的“内容处理程序”.那么当一个 location 中未使用任何 cont ...
- k8s集群日志
硬件环境: 三台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-schedule ...