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 ...
随机推荐
- java中按字节获得字符串长度的两种方法 Java问题通用解决代码
jdk本身就自带获取字符串字节长度的api了,但字符串如果包含特殊符号或全半角符号或标点符号获取到的结果会有偏差,最好的证据就是新浪微博的字数统计了 // jdk自带的获取字节长度 //注意getBy ...
- asp.net core mvc视频A:笔记2-2.接收数据
传参方式一:使用内置方法传递 运行结果 其他获取方法 Session对象在HttpContext中 启用Session 使用Session 运行演示 传参方式二:数据绑定 普通类型(string).自 ...
- zabbix自动发现监控远程端口
zabbix监控远程服务器端口,simple checks是zabbix用来监控无agent的主机 脚本和模板地址: https://github.com/mikeluwen/tcpmonitor
- redis 的安装与启动
1.redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis 是一个高性能的key-val ...
- oracle中避免sort操作
1.升序排列 create index ix_table1 on table1 (column1,column2); select column1,column2 from table1 order ...
- bzoj3531【SDOI2014】旅行
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 850 Solved: 433 [Submit][Status ...
- 怎样取消不能改动(仅仅读打开)的word文件的password
作者:iamlaosong 朋友给我一个文档,是加了防改动password的,希望我能帮其取消.由于须要原文档的格式,取消方法例如以下(office2007环境): 1.打开文件.文件打开时,提演示样 ...
- ubuntu 16.04安装visual studio code 提示libnss3版本低:NSS >= 3.26 is required
Linux Ubuntu 1604安装VS CODE之后,执行./code报错误: [3781:0914/160851.489979:FATAL:nss_util.cc(632)] NSS_Versi ...
- publish over ssh 实现 Jenkins 远程部署
Jenkins远程部署,一开始没有任何头绪,想了很多方案. 因为两台机器都是windows系统,所以想到publish over cifs, 但是这个网上资料太少,貌似只能内网使用.又想到了Jenki ...
- web开发之html5---html5 动画特效舞动的雨伞
http://www.cnblogs.com/stoneniqiu/p/4199294.html