[bzoj-2140]稳定婚姻 题解
2017-10-29 14:06
447 查看
题目传送门
题意解析:题目告诉了我们n对夫妻,和m对情人(???题目就是污),然后假设第i对夫妻有矛盾,那么他们就会离婚去找情人(???),所以最后就可能产生两种情况,一种是,最后一一重新配对,反之有剩余,问对于每对夫妻闹矛盾后的情况是哪一种。
My opinion:看到这题目的大小,n<=4000。有深意,而且对于一一对应的匹配,很容易就想到了二分图匹配,但是事实上,点有2n个,所以这样二分图匹配会超时。但是我们可以发现,如果两个人处在同一个环里是不安全的,而如果不在就是安全的。
总结:
1、输入。
2、跑强联通分量。
3、输出判断。
代码:
题意解析:题目告诉了我们n对夫妻,和m对情人(???题目就是污),然后假设第i对夫妻有矛盾,那么他们就会离婚去找情人(???),所以最后就可能产生两种情况,一种是,最后一一重新配对,反之有剩余,问对于每对夫妻闹矛盾后的情况是哪一种。
My opinion:看到这题目的大小,n<=4000。有深意,而且对于一一对应的匹配,很容易就想到了二分图匹配,但是事实上,点有2n个,所以这样二分图匹配会超时。但是我们可以发现,如果两个人处在同一个环里是不安全的,而如果不在就是安全的。
总结:
1、输入。
2、跑强联通分量。
3、输出判断。
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=a;i>=n;i--) #define Clear(a,x) memset(a,x,sizeof(a)) #define ll long long #define INF 2000000000 #define eps 1e-8 using namespace std; int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } const int maxn=4005,maxm=200005; map<string,int> Name; int vet[maxm<<1],Next[maxm<<1],head[maxn<<1]; int flag[maxn<<1]; int low[maxn<<1],dfn[maxn<<1],q[maxn<<1],c[maxn<<1]; int n,m,num,top,len,Time; void add(int u,int v){ vet[++len]=v; Next[len]=head[u]; head[u]=len; } void tarjan(int u){ dfn[u]=low[u]=++Time; q[++top]=u; flag[u]=1; for (int e=head[u];e;e=Next[e]){ int v=vet[e]; if (!flag[v]){ tarjan(v); low[u]=min(low[u],low[v]); }else if (flag[v]==1) low[u]=min(low[u],dfn[v]); } if (low[u]==dfn[u]){ num++; while (top>0&&q[top]!=u){ c[q[top]]=num,flag[q[top]]=2; top--; } c[u]=num,flag[u]=2; top--; } } void solve(){ num=top=Time=0; rep(i,1,2*n) if (!flag[i]) tarjan(i); rep(i,1,n) if (c[i]==c[i+n]) puts("Unsafe"); else puts("Safe"); } int main(){ n=read(); rep(i,1,n){ char girl[10],boy[10]; scanf("%s%s",girl,boy); Name[girl]=i; Name[boy]=i+n; add(i,i+n); } m=read(); rep(i,1,m){ char girl[10],boy[10]; scanf("%s%s",girl,boy); int G=Name[girl],B=Name[boy]; add(B,G); } solve(); return 0; }
相关文章推荐
- bzoj 2140: 稳定婚姻
- BZOJ 2140: 稳定婚姻
- [二分图完美匹配必要边 强连通分量] BZOJ 2140 稳定婚姻
- BZOJ_2140_稳定婚姻_强连通分量
- BZOJ2140 稳定婚姻 [Tarjan]
- BZOJ 2140 稳定婚姻
- BZOJ 2140 稳定婚姻
- 【BZOJ2140】稳定婚姻【SCC】【二分图完备匹配】
- 【BZOJ2140】稳定婚姻 Tarjan
- BZOJ2140: 稳定婚姻
- BZOJ 2140 稳定婚姻
- BZOJ 2140 稳定婚姻 - Tarjan_SCC
- BZOJ2140: 稳定婚姻 Tarjan求无向图强联通分量
- [bzoj2140]稳定婚姻 Tarjan
- BZOJ 2140 稳定婚姻
- bzoj2140 稳定婚姻 强连通分量
- 【BZOJ】2140 稳定婚姻
- BZOJ 2140 稳定婚姻
- 【tarjan】BZOJ2140-稳定婚姻
- 【BZOJ】2140 稳定婚姻