[National Training Team] stable marriage and stable marriage
2140: stable marriage Time Limit: 2 Sec Memory Limit: 259 MB
Submit: 1058 Solved: 498
[Submit] [Status] [Discuss] Description China's divorce rate has risen for seven consecutive years. In the first two quarters of this year, nearly 5000 couples are divorced on average every day, and the divorce rate in big cities has risen fastest. Experts who study marital issues believe that, it is related to simplifying divorce procedures. The 25-Year-Old Shanshan and her boyfriend will get married after half a year and get divorced less than two months after getting married. This is a typical example of "Flash divorce". the fuse of divorce is that two people compete for computer games, the husband blew up the computer. According to some social workers, there are more and more help cases after the 80 s, and some are related to excessive parental intervention. According to statistics from the Ministry of Civil Affairs, the top five divorce cities in China are Beijing, followed by Shanghai, Shenzhen, Guangzhou and Xiamen. Why did China become a divorce power? According to expert analysis, the rapid development of China's economy, coupled with the increasing independence of women, has simplified divorce procedures in recent years. -- The above content is taken from the first video portal. Modern Life puts more and more pressure on people. The increasing divorce rate has become a major problem in modern society. Many of these cases are caused by the unstable factors in the marriage. After a quarrel between his wife and her husband, her heart was like a pain point, so she sought comfort from her ex-boyfriend. The conflict between her husband and wife intensified and eventually ended in divorce. There were countless cases similar to the above. We know the marital status of n couples, that is, the male of the I couples is Bi, and the female is Gi. If a male Bi has been in contact with a female Gj (whether in college, high school, or kindergarten, I? j ), then, when a Party has a problem with its spouse (Bi, Gi, Bj, and Gj), they may be able to run away. It may be suggested that Bi and its spouse Gi do not feel the same, so Bi and Gj are revived, and Bj is unhappy because of being wearing a green hat, and contacted his first love lover Gk ...... A series of divorce events are like dominoes. If, on the premise of divorce between Bi and Gi, THE 2n individual can still be combined into n couples, then we call marriage I unsafe, otherwise marriage I is safe. Given the required information, your task is to determine whether each pair of marriages is secure. Input the first line is a positive integer n, indicating the logarithm of the husband and wife. The following n rows contain two strings, indicating the names of the n couples (female and male), separated by a space; line n + 2 contains a positive integer m, indicating the logarithm of a couple who liked each other. Line m below contains two strings, the names of the couples (female, female, and male) liked by m, separated by a space. All name strings only contain uppercase and lowercase English letters. They are case-sensitive and cannot exceed 8 characters. Ensure that each pair of names appears only once in the input file, the last m row of the input file does not contain names that have not appeared before. The names of the 2n individual are different. 1 ≤ n ≤ 4000,0 ≤ m ≤ 20000. Output
The output file contains n rows. the I-th Act is "Safe" (if I is Safe) or "Unsafe" (if I is insecure ).
Sample Input [Example Input 1]
2
Meleni Ashley
Scarlett Charles
1
Scarlett Ashley
[Example input 2]
2
Meleni Ashley
Scarlett Charles
2
Scarlett Ashley
Melannie Charles
Sample Output [Sample Output 1]
Safe
Safe
[Sample output 2]
Unsafe
Unsafe
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=20000+5; 5 6 struct node{ 7 int net; 8 int to; 9 }a[maxn*2];10 int n,m;11 string gi,bo;12 int low[maxn],dfn[maxn],sum,head[maxn];13 int rs[maxn],st[maxn],top,dep,cnt;14 bool vis[maxn];15 map<string,int>al;16 17 inline void add(int i,int j)18 {19 a[++cnt].to=j;20 a[cnt].net=head[i];21 head[i]=cnt;22 }23 24 void tarjan(int u)25 {26 dfn[u]=low[u]=++dep;27 st[++top]=u;28 vis[u]=true;29 for(int i=head[u];i;i=a[i].net)30 {31 int v=a[i].to;32 if(!dfn[v])33 {34 tarjan(v);35 low[u]=min(low[u],low[v]);36 }37 else if(vis[v])38 {39 low[u]=min(low[u],dfn[v]);40 }41 }42 if(low[u]==dfn[u])43 {44 ++sum;45 int now;46 do47 {48 now=st[top--];49 rs[now]=sum;50 vis[now]=false;51 }while(now!=u);52 }53 }54 55 int main()56 {57 scanf("%d",&n);58 for(int i=1;i<=n;i++)59 {60 cin>>gi;61 cin>>bo;62 al[gi]=i;63 al[bo]=i+n;64 add(i,i+n);65 }66 scanf("%d",&m);67 for(int i=1;i<=m;i++)68 {69 cin>>gi;70 cin>>bo;71 add(al[bo],al[gi]);72 }73 for(int i=1;i<=n*2;i++)74 {75 if(!dfn[i])76 {77 tarjan(i);78 }79 }80 for(int i=1;i<=n;i++)81 {82 if(rs[i]==rs[i+n])83 {84 printf("Unsafe\n");85 }86 else87 printf("Safe\n");88 }89 return 0;90 }