我不想给题目
题意:
给你一张二维图,上面有石头,然后你就用你TN的意大利炮去轰这些石头吧(意大利炮的威力很大,所以能够直线AOE),问你最少打几炮(只能横着或者竖着打)?输出打炮的位置。
解题思路:
题目等价于最多有多少个石头既不在同一行也不再同一列上。有石头的地方行和列就能匹配,找出最大匹配即可(匈牙利模板 )。
然后是寻找打炮位置,看代码吧。。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
const int maxn=2*1005;
const int maxm=2*1005*1005;
const int INF=0x3f3f3f3f;
int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
int e_max;
int match[maxn];
bool check[maxn];
int g[maxn][maxn];
int ans;
inline void init()
{
memset(fir, -1, sizeof fir);
e_max = 0;
}
inline void add_edge(int s, int t)
{
int e = e_max++;
u[e] = s;
v[e] = t;
nex[e] = fir[u[e]];
fir[u[e]] = e;
}
int n,m,e;
bool dfs(int u)
{
check[u]=1;
for(int e=fir[u];~e;e=nex[e])
{
int vv=v[e];
if(!check[vv])
{
check[vv]=1;
if(match[vv]==-1||dfs(match[vv]))
{
match[vv]=u;
match[u]=vv;
return 1;
}
}
}
return 0;
}
int hungary()
{
ans=0;
memset(match,-1,sizeof match);
for(int i=1;i<=n;i++)
{
if(match[i]==-1)
{
memset(check,0,sizeof check);
if(dfs(i))
ans++;
}
}
}
int main()
{
int a,b;
while(scanf("%d%d%d",&n,&m,&e)!=EOF&&(m+n+e))
{
init();
memset(g,0,sizeof g);
for(int i=0;i<e;i++)
{
scanf("%d%d",&a,&b);
add_edge(a,b+n);
}
hungary();
printf("%d",ans);
memset(check,0,sizeof check);
for(int i=1;i<=n;i++)
{
if(match[i]==-1)
dfs(i);
}
for(int i=1;i<=n;i++)
if(!check[i])
printf(" r%d",i);
for(int i=n+1;i<=m+n;i++)
if(check[i])
printf(" c%d",i-n);
printf("\n");
}
}