大佬们求助,二分图的做法,不知道哪里错了
查看原帖
大佬们求助,二分图的做法,不知道哪里错了
445864
美少女☁楼主2021/2/22 17:32
#include<iostream>
#include<cstring>
#include<map>

using namespace std;

const int N = 500000;

int n,m;
int maxn;
int h[N],e[N],ne[N],idx;
int match[N];
int vis[N];
map<string,int > mp;
int xh;
int A,B;

void insert(int a,int b);
bool find(int now);

int main()
{
	cin>>n;
	memset(h,-1,sizeof(h));
	for(int i=1;i<=n;i++)
	{
		string a,b;
		cin>>a>>b;
		mp[a] = ++xh;
		mp[b] = ++xh;
		match[mp[a]] = mp[b];
		match[mp[b]] = mp[a];
		insert(mp[a],mp[b]);
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		string a,b;
		cin>>a>>b;
		insert(mp[a],mp[b]);
	}
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof(vis));
		A = i,B = match[A];
		match[B] = 0;
		if(find(i)) cout<<"Unsafe"<<endl;
		else cout<<"Safe"<<endl;
		match[B] = A;
	}
	return 0;
}

void insert(int a,int b)
{
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}

bool find(int now)
{
	for(int i=h[now];i!=-1;i=ne[i])
	{
		int spot = e[i];
		if(now==A&&spot==B) continue;
		if(vis[spot]==0)
		{
			vis[spot] = 1;
			if(match[spot]==0||find(match[spot]))
			{
				match[spot] = now;
				return true;
			}
		}
	}
	return false;
}
2021/2/22 17:32
加载中...