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 ...
随机推荐
- Eclipse中设置格式化jsp自动换行
JSP代码换行:Window->Preferences->Web->JSP Files->Editor->Line width
- Android 关于SD的操作
1 http://www.cnblogs.com/shaoyangjiang/archive/2012/03/09/2388178.html 2
- quick-cocos2d-x3.2 scheduler使用注意事项
近期在使用scheduler时发现例如以下问题 调用: local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") fun ...
- Linux系统字符集乱码问题
假设你在安装Linux的过程中就选择了中文.可能能够省去步骤1.2.反之.假设你先安装了英文环境,而后希望它支持中文,则能够1.2步 1.首先在linux中安装中文包安装中文简体包rpm -ivh k ...
- Spring Boot从入门到实战:整合通用Mapper简化单表操作
数据库访问是web应用必不可少的部分.现今最常用的数据库ORM框架有Hibernate与Mybatis,Hibernate貌似在传统IT企业用的较多,而Mybatis则在互联网企业应用较多.通用Map ...
- C#中Lock静态字段和实例字段
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS 字符属性NSAttributedString描述【转载】
/* 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFontAttributeName;(字体) NSString *const N ...
- xml初学简单介绍
什么是XML? 1.全称Extensible Markup Language,可扩展标记语言,W3C组织公布. 2.XML用来保存有一定结构关系的数据. 3.标签的嵌套,实质是一串字符串. 4.跨平台 ...
- 第四篇: Ansible 常用模块使用
一. command | shell 模块 该模块为:执行命令模块 (由于命令模块较为强大,可根据自身对操作系统的熟练程度而增加.所以这里不再介绍类似可以实现同功能模块,比如service模块,需要重 ...
- 【Caffe】caffemodel的大小计算(转载)
看到一篇将如何计算caffemodel大小的blog,感觉对理解模型大小很有帮助. 原文地址:http://blog.csdn.net/u014696921/article/details/52413 ...