BZOJ 2140 稳定婚姻

来源:互联网 发布:朗读英语的软件 编辑:程序博客网 时间:2024/05/18 02:54

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

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


#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;}


原创粉丝点击