Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

ANS=N-最大匹配,附25行代码

Posted by yousiki at 2016-07-28 15:39:07 on Problem 1422
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N = 155;
int T, t, n, m, G[N][N], mch[N], vis[N], ans, x, y;
inline bool find(int u) {
	for (int i = 1; i <= n; i++)if (G[u][i] && !vis[i] && (vis[i] = 1))
		if (mch[i] == -1 || find(mch[i])) {
			mch[i] = u;
			return true;
		}
	return false;
}
signed main(void) {
	for (scanf("%d", &T), t = 1; t <= T; t++) {
		memset(G, 0, sizeof(G));
		memset(mch, -1, sizeof(mch));
		for (scanf("%d%d", &n, &m); m; m--)
			scanf("%d%d", &x, &y), G[x][y] = 1;
		for (x = 1, ans = 0; x <= n; x++)
			memset(vis, 0, sizeof(vis)),ans += find(x);
		printf("%d\n", n - ans);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator