P1407 [国家集训队]稳定婚姻 day12

 昨天CF没有做出D

原因竟是不会二分

令人感叹!

不过这道题很有意思啊!

每次断一次正经关系 跑一遍匈牙利 要是成功的话 也就是跑过的地方都匹配

最开始想的是连边 偶数环染色 但是感觉太复杂了 确实是没注意到n<4000 可以O(n^2)过啊!

#include <bits/stdc++.h>
using namespace std;
const int N = 2e4+10;
const int M = 1e4+10;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define Endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int n,m,h[N],e[N<<1],ne[N<<1],idx,match[N<<1];
bool st[N<<1];
map<string,int>a;
map<string,int>b;
void add(int a,int b){
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
int find(int x){
    for(int i=h[x];~i;i=ne[i]){
        int j=e[i];
        if(!st[j]){
            st[j]=true;
            if(j==x+n)continue;
            if(!match[j]||find(match[j]))return true;
        }
    }
    return false;
}
signed main(){
    fast
    cin>>n;
    memset(h,-1,sizeof h);
    for(int i=1;i<=n;i++){
        string A,B;cin>>A>>B;
        a[A]=i,b[B]=i+n;
        match[i]=i+n,match[i+n]=i;
        add(i,i+n);
    }
    cin>>m;
    for(int i=1;i<=m;i++){
        string A,B;cin>>A>>B;
        add(a[A],b[B]);
    }
    for(int i=1;i<=n;i++){
        match[i+n]=0;
        if(find(i))cout<<"Unsafe"<<Endl;
        else cout<<"Safe"<<endl;
        match[i+n]=i;
        memset(st,false,sizeof st);
    }
    return 0^0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值