離散作業

whtfffffff發表於2020-12-16
#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 5;
int vis[N],a[N][N],link[N],cnt,n,t;

int dfs(int x)
{
	for (int i = 1; i <= n ;i++)
	{
		if (!a[x][i]||vis[i]) continue;
		vis[i] = 1;
		if (link[i] == 0 || dfs(link[i]))
		{
			link[i] = x;
			return 1;
		}
	}
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t--)
	{
		cin >> n;  
		memset(link,0,sizeof(link));
		cnt = 0;
		
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n ;j ++)
			{
				cin >> a[i][j];
			}
		}
		for (int i = 1; i <= n; i++)
		{
			memset(vis, 0, sizeof(vis));
			dfs(i);
		}	
		for (int i = 1; i <= n; i++)
		{
			cout << link[i] << " ";
			if(link[i] > 0)//如果匹配上了 
				cnt++;
		}
		if(cnt == n) puts("Yes");//是完美匹配 
		else puts("No");//不是完美匹配 
	}
}

相關文章