contest link : http://codeforces.com/gym/101485
就瞎写一题的题解吧,当时有事没去打
题意:给你n对数,问你有没有一种方法,让每一对经过 +、-、*操作,能得到n个不同的数
解题思路:很显然的二分图最大匹配了,唯一需要注意的是保存下来答案,显然直接建图是不行的,link数组开不到那么大,这时候用类似于离散化的思想,把res转换成编号(当然,要用另外一个数组存下来的),然后再跑Hungary就行了
这题感觉挺无聊的,就是无聊了想写个博客。。
#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lowbit(x) x & (-x)
#define PII pair<int, int>
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)2500;
using namespace std;
using __gnu_cxx::crope;
vector<int> G[maxn];
map<ll, int> M;
ll a[maxn], b[maxn];
ll res[3*maxn], ans[maxn];
int link[3*maxn];
bool vis[3*maxn];
int n, cnt = 1;
bool dfs(int i){
if(i == -1) return true;
for(auto t : G[i]){
if(vis[t]) continue;
vis[t] = true;
if(dfs(link[t])){
link[t] = i;
return true;
}
}
return false;
}
int solve(){
memset(link, -1, sizeof(link));
int ret = 0;
rep(i, 1, n){
memset(vis, 0, sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
}
int work(ll x){
if(!M[x]){
M[x] = cnt++;
res[M[x]] = x;
}
return M[x];
}
int main()
{
FAST;
cin >> n;
rep(i, 1, n){
cin >> a[i] >> b[i];
G[i].pb(work(a[i] + b[i]));
G[i].pb(work(a[i] * b[i]));
G[i].pb(work(a[i] - b[i]));
}
if(solve() < n) return 0 * puts("impossible");
rep(i, 1, cnt) if(link[i] != -1) ans[link[i]] = res[i];
rep(i, 1, n){
char ch;
if(a[i] + b[i] == ans[i]) ch = '+';
else if(a[i] * b[i] == ans[i]) ch = '*';
else ch = '-';
cout << a[i] << ' ' << ch << ' ' << b[i] << " = " << ans[i] << endl;
}
return 0;
}