题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5449

题意

给出一个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 【二分匹配】的更多相关文章

  1. UVALive 6525 Attacking rooks 二分匹配 经典题

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...

  2. BNUOJ 12756 Social Holidaying(二分匹配)

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=12756 Social Holidaying Time Limit: 3000ms Memo ...

  3. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  4. HDU 3468 BFS+二分匹配

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...

  5. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  6. hdu3729二分匹配

    I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

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

  8. 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 ...

  9. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

随机推荐

  1. LeetCode-Minimum Window Substring -- 窗口问题

    题目描述 Given a string S and a string T, find the minimum window in S which will contain all the charac ...

  2. Android 虚线切割线

    drawable下新建一个虚线的xml.dash_line.xml <? xml version="1.0" encoding="utf-8"?> ...

  3. iOS仿支付宝首页效果

    代码地址如下:http://www.demodashi.com/demo/12776.html 首先看一下效果 状态栏红色是因为使用手机录屏的原因. 1.问题分析 1.导航栏A有两组控件,随着tabl ...

  4. [译] JavaScript 的事件循环

    译者注 本译文基本是按原文的意思来翻译,但对于 JavaScript 的事件循环,个人感觉还是 Philip Roberts 的视频讲解更形象些,思路和本文大致相同,不过他把事件表理解为 Web AP ...

  5. 代码设置UIButton文字、图片位置

    假设有按钮rButton的 imageEdgeInsets和contentEdgeInsets可以设置按钮的标题和图片的位置,如下代码,设置标题居右 NSString * rBtnTitle = @& ...

  6. https 加载问题

    https的网站,加载的资源要全部https,如果里面有http的资源,很多浏览器是加载不进来 要地址栏变绿,网站内部全部引用都是https的

  7. iostat命令分析磁盘io

    1.安装 yum install sysstat 2.参数 建议将man 文档看一遍 3.简单判断io状况 iostat -d -k 2 -x Device: rrqm/s wrqm/s r/s w/ ...

  8. Mac下安装LNMP(Nginx+PHP5.6)环境(转)

    安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10.1)为例,记录一下从零开始安装Mac下LNMP环境的过程 确保系统已经安装xcode,然后使用一行命令安装 ...

  9. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  10. git删除所有历史提交记录,只留下最新的干净代码

    git删除所有历史提交记录,只留下最新的干净代码 1.Checkout git checkout --orphan latest_branch 2. Add all the files git add ...