BZOJ 2140 稳定婚姻 - Tarjan_SCC

和稳定婚姻、匹配没有任何关系啊有木有。。。

大概就是把婚外情的家庭编号用单向边连接(男方编号指向女方编号),如果这些编号存在自环,则其家庭之间已经出现满足出轨条件的状况了。


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
 
using namespace std;
 
const int maxn=4005;
const int maxm=20005;
 
map<string,int>num;
 
struct edge
{
    int next,to;
}e[maxm];
 
int n,m,cnt,scccnt,dfs_clock,top;
int head[maxn],dfn[maxn],low[maxn];
int belong[maxn],sccnum[maxn],stack[maxn];
bool vst[maxn];
 
void insert(int a,int b)
{
    e[++cnt].to=b;e[cnt].next=head[a];head[a]=cnt;
}
void tarjan(int x)
{
    dfn[x]=low[x]=++dfs_clock;
    stack[++top]=x;
    vst[x]=true;
    for(int i=head[x];i;i=e[i].next)
    {
        int y=e[i].to;
        if(!dfn[y])
            tarjan(y),low[x]=min(low[x],low[y]);
        else if(vst[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x])
    {
        int now=-1;
        ++scccnt;
        while(now!=x)
        {
            now=stack[top--];
            belong[now]=scccnt;
            vst[now]=false;
            sccnum[scccnt]++;
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        string st;
        cin>>st;
        num[st]=i;
        cin>>st;
        num[st]=n+i;
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        string st1,st2;
        cin>>st1;
        cin>>st2;
        insert(num[st1],num[st2]-n);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i);
    for(int i=1;i<=n;i++)
        printf(sccnum[belong[i]]>1?"Unsafe\n":"Safe\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值