#include
#include
#include
#define fi first
#define se second
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
PII match[N][N];
int n,m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool st[N][N],g[N][N];
bool find(int x,int y)
{
for(int i = 0;i < 4;i ++ )
{
int x1 = x + dx[i],y1 = y + dy[i];
if(x1 < 1 || y1 < 1 || x1 > n || y1 > n) continue;
if(!st[x1][y1] && !g[x1][y1])
{
st[x1][y1] = true;
PII t = match[x1][y1];
if(t.fi == 0 || find(t.fi,t.se))
{
match[x1][y1] = {x,y};
return true;
}
}
}
return false;
}
int main()
{
cin >> n >> m;
while (m -- )
{
int x,y; cin >> x >> y;
g[x][y] = true;
}
int res = 0;
for(int i = 1;i <= n;i ++ )
{
for(int j = 1;j <= n;j ++ )
{
memset(st, 0, sizeof st);
if(i + j & 1 && !g[i][j])
{
if(find(i,j)) res++;
}
}
}
cout << res << endl;
return 0;
}
#include
//#define int long long
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
using namespace std;
const int N = 1e4 + 100,M = 2e5 + 100,INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], idx;
int n,m;
int lx[N],ly[N];
int color[N];
bool st[N];
void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int bfs(int u)
{
queue<int> q;
q.push(u);
color[u] = 1;
int d = 0,d2 = 0;
while(q.size())
{
int t = q.front(); q.pop();
if(color[t] == 1) d++;
else if(color[t] == 2) d2++;
for(int i = h[t];i != -1;i = ne[i])
{
int j = e[i];
if(color[j] == color[t]) return -1;
else if(color[j] == 0)
{
color[j] = 3 - color[t];
q.push(j);
}
}
}
int w = min(d,d2);
return w;
}
void solve()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for(int i = 1;i <= m;i ++ )
{
int u,v; cin >> u >> v;
add(u,v),add(v,u);
}
int sum = 0;
for(int i = 1;i <= n;i ++ )
{
if(!color[i])
{
int ans = bfs(i);
if(ans == -1)
{
cout << "Impossible" << endl;
return;
}
sum += ans;
}
}
cout << sum << endl;
}
int main()
{
ios;
int T = 1; while(T -- ) solve();
return 0;
}
P1559 运动员最佳匹配问题: 经典KM算法 (暴搜被hack)
#include
//#define int long long
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
using namespace std;
const int N = 30,M = N * 2,INF = 0x3f3f3f3f;
int p[N][N],q[N][N];
bool stx[N],sty[N];
int n;
int lx[N],ly[N];
int pi[N];
int minz;
bool find(int x)
{
stx[x] = true;
for(int i = 1;i <= n;i ++ ) // 遍历所有y点
{
if(!sty[i])
{
int t = lx[x] + ly[i] - (p[x][i] * q[i][x]);
if(t == 0) // 代表这个y点有可能可以匹配
{
sty[i] = true;
if(pi[i] == 0 || find(pi[i]))
{
pi[i] = x;
return true;
}
}
else if(t > 0)// 代表这个y点不可能匹配的,就取一个最小值
{
minz = min(minz,t);
}
}
}
return false;
}
void KM()
{
for(int i = 1;i <= n;i ++ )
{
while(1) // 一直到整体可以下来没问题的时候再说
{
minz = INF;
memset(stx, false, sizeof stx);
memset(sty, false, sizeof sty);
if(find(i)) break;
for(int j = 1;j <= n;j ++ )
if(stx[j])
lx[j] -= minz;
for(int j = 1;j <= n;j ++ )
if(sty[j])
ly[j] += minz;
}
}
}
void solve()
{
cin >> n;
for(int i = 1;i <= n;i ++ )for(int j = 1;j <= n;j ++ )cin >> p[i][j];
for(int i = 1;i <= n;i ++ )for(int j = 1;j <= n;j ++ )cin >> q[i][j];
{
}
for(int i = 1;i <= n;i ++ )
{
for(int j = 1;j <= n;j ++ )
lx[i] = max(lx[i],p[i][j] * q[j][i]); // 初始化二分图左边的图
}
KM();
int ans = 0;
for(int i = 1;i <= n;i ++ )
ans += p[pi[i]][i] * q[i][pi[i]];
cout << ans << endl;
}
int main()
{
ios;
int T = 1; while(T -- ) solve();
return 0;
}
#include
//#define int long long
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int>
using namespace std;
const int N = 55,M = 4e4 + 100,INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], idx;
map<string,int> mp;
int n,m;
bool st[N];
int match[N];
int stu[N],house[N],g[N][N];
void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int find(int u)
{
for(int i = 1;i <= n;i ++ )
{
if(stu[i] && g[u][i] && !st[i])
// 这个人得是学生才有床铺这个人还得跟i这个人认识
{
st[i] = true;
if(match[i] == 0 || find(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
}
void solve()
{
cin >> n;//现在有n个人
memset(match,0,sizeof match);
memset(g,0,sizeof g);
for(int i = 1;i <= n;i ++ ) // 判断一下这个人是不是在校学生
cin >> stu[i];
for(int i = 1;i <= n;i ++ ) // 判断这个人有没有回家
{
cin >> house[i];
if(stu[i] == 0) //不是在校学生,统一让他不回家
house[i] = 0;
}
for(int i = 1;i <= n;i ++ ) // 如果第i个人个第j个人认识,那就是1,否则是0
for(int j = 1;j <= n;j ++ )
{
cin >> g[i][j];
if(i == j && stu[i]) // 这个人跟自己一定认识,也就是这个人可以睡到自己床铺上面
{
g[i][j] = 1;
}
}
for(int i = 1;i <= n;i ++ ) // 枚举每一个没有回家的人,
{
if(!house[i]) // 代表这个人是不回家的人
{
memset(st,false,sizeof st);
if(!find(i))
{
cout << "T_T" << endl;
return;
}
}
}
cout << "^_^" << endl;
}
int main()
{
ios;int T; cin >> T;while(T -- ) solve();
return 0;
}