【邻接表+匈牙利算法模板】Elementary Math

http://acm.bnu.edu.cn/v3/external/gym/101485.pdf

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 const int maxn=2502;
  5 struct node
  6 {
  7     ll x;
  8     ll y;
  9     int id;
 10 }a[maxn];
 11 int n;
 12 ll b[maxn*3];
 13 int cnt; 
 14 struct Edge
 15 {
 16     int to;
 17     int nxt;
 18     int w;
 19 }e[maxn*6];
 20 int tot;
 21 int head[maxn];
 22 map<ll,int> mp;
 23 bool vis[maxn*3];
 24 int used[maxn*3];
 25 char op[maxn];
 26 void init()
 27 {
 28     tot=0;
 29     memset(head,-1,sizeof(head));
 30     memset(vis,false,sizeof(vis));
 31     memset(used,-1,sizeof(used));
 32 }
 33 void add(int u,int v)
 34 {
 35     e[tot].to=v;
 36     e[tot].w=1;
 37     e[tot].nxt=head[u];
 38     head[u]=tot++;
 39 }
 40 int dfs(int u)
 41 {
 42     for(int i=head[u];i!=-1;i=e[i].nxt)
 43     {
 44         int v=e[i].to;
 45         if(!vis[v]&&e[i].w)
 46         {
 47             vis[v]=true;
 48             if(used[v]==-1||dfs(used[v]))
 49             {
 50                 used[v]=u;
 51                 return 1;
 52             }
 53         }
 54     }
 55     return 0;
 56 }
 57 
 58 void output()
 59 {
 60     for(int i=0;i<cnt;i++)
 61     {
 62         if(used[mp[b[i]]]==-1) continue;
 63         int id=used[mp[b[i]]];
 64         if(a[id].x+a[id].y==b[i]) op[id]='+';
 65         else if(a[id].x-a[id].y==b[i]) op[id]='-';
 66         else op[id]='*';
 67     }
 68     for(int i=1;i<=n;i++)
 69     {
 70         printf("%lld %c %lld",a[i].x,op[i],a[i].y);
 71         ll res;
 72         if(op[i]=='+') res=a[i].x+a[i].y;
 73         else if(op[i]=='-') res=a[i].x-a[i].y;
 74         else if(op[i]=='*') res=a[i].x*a[i].y;
 75         printf(" = %lld
",res);
 76     }
 77 }
 78 int main()
 79 {
 80     while(~scanf("%d",&n))
 81     {
 82         init();
 83         mp.clear();
 84         cnt=0; 
 85         for(int i=1;i<=n;i++)
 86         {
 87             scanf("%lld%lld",&a[i].x,&a[i].y);
 88             a[i].id=i;
 89             b[cnt++]=a[i].x+a[i].y;
 90             b[cnt++]=a[i].x-a[i].y;
 91             b[cnt++]=a[i].x*a[i].y;
 92         }
 93         sort(b,b+cnt);
 94         cnt=unique(b,b+cnt)-b;
 95 //        for(int i=0;i<cnt;i++)
 96 //        {
 97 //            cout<<b[i]<<" ";
 98 //        }
 99 //        cout<<endl;
100         for(int i=0;i<cnt;i++)
101         {
102             mp[b[i]]=i+1;
103         }
104         for(int i=1;i<=n;i++)
105         {
106             ll x=a[i].x+a[i].y;
107             ll y=a[i].x-a[i].y;
108             ll z=a[i].x*a[i].y;
109             int posx=mp[x];
110             int posy=mp[y];
111             int posz=mp[z];
112         //    cout<<posx<<" "<<posy<<" "<<posz<<endl;
113             add(i,posx);
114             if(posy!=posx) add(i,posy);
115             if(posz!=posy && posz!=posx)    add(i,posz);
116         }
117         int ans=0;
118         for(int i=1;i<=n;i++)
119         {
120             memset(vis,false,sizeof(vis));
121             ans+=dfs(i);
122         }
123         if(ans<n)
124         {
125             puts("impossible");
126         }
127         else
128         {
129             output();
130         }
131     }
132     return 0;
133 }
View Code
原文地址:https://www.cnblogs.com/itcsl/p/7617428.html