题目链接

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. Jeewx 捷微管家操作配置文档(开源版本号)

    1.1.1.  公众帐号管理 (1)捷微是第三方微信公众帐号管理平台,使用本平台前,请自行注冊申请微信公众帐号,操作流程请參照百度经验[怎样注冊微信公众帐号]: http://jingyan.baid ...

  2. POJ 2375 Cow Ski Area (强连通分量)

    题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...

  3. Python:Dom生成XML文件(写XML)

    http://www.ourunix.org/post/327.html 在python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文 ...

  4. window 服务(二)

    接Window服务(一) ServiceController方法调用 public partial class Service1 : ServiceBase { public Service1() { ...

  5. linux 进程的 5 大段

    BSS段:BSS段(bss segment)通常是指用来存放程序中数据段:数据段(data segment)通常是指用来存放程序中已初始化的全局变量的一块内存区域.数据段属于静态内存分配. 数据段:数 ...

  6. 人不在囧途 便携式3G上网设备+套餐推介

    来源: http://network.pconline.com.cn/317/3174920_all.html [PConline资讯]过年回家,本该是再高兴不过的事,可一想到要在路上颠簸数十个小时, ...

  7. Theme.AppCompat.Light无法找到问题

    使用adt开发新建一个Android app.选择支持的SDK版本号假设小于11(Android3.0)就会报例如以下错误. error: Error retrieving parent for it ...

  8. 如何自定义View

    1. 首先 在values目录下建立attrs.xml文件,添加属性内容 ·在布局文件中添加新的命名空间xmlns,然后可以使用命名空间给自定义的空间设置属性 attrs.xml <resour ...

  9. linux下的Java开发 intellij idea+tomcat+maven

    前期准备:安装intellij idea.下载tomcat.下载maven(注意我用的是tomcat6.maven 3.2.1.jdk1.6.0_45,之前maven用的3.5结果报错,搞了好久,建议 ...

  10. 深入了解Erlang 垃圾回收机制以及其重要性(转)

    声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...