[HDU 6165]FFF at Valentine
来源:互联网 发布:网络作家村 编辑:程序博客网 时间:2024/06/13 21:30
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
At Valentine’s eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, Boss of FFF Group caught both of them, and locked them into two separate cells of the jail randomly. But as the saying goes: There is always a way out , the lovers made a bet with LSH: if either of them can reach the cell of the other one, then LSH has to let them go.
The jail is formed of several cells and each cell has some special portals connect to a specific cell. One can be transported to the connected cell by the portal, but be transported back is impossible. There will not be a portal connecting a cell and itself, and since the cost of a portal is pretty expensive, LSH would not tolerate the fact that two portals connect exactly the same two cells.
As an enthusiastic person of the FFF group, YOU are quit curious about whether the lovers can survive or not. So you get a map of the jail and decide to figure it out.
Input
∙Input starts with an integer T (T≤120), denoting the number of test cases.
∙For each case,First line is two number n and m, the total number of cells and portals in the jail.(2≤n≤1000,m≤6000),Then next m lines each contains two integer u and v, which indicates a portal from u to v.
Output
If the couple can survive, print “I love you my love and our love save us!”
Otherwise, print “Light my fire!”
Sample Input
35 51 22 32 43 54 53 31 22 33 15 51 22 33 13 44 5
Sample Output
Light my fire!I love you my love and our love save us!I love you my love and our love save us!
Source
2017 Multi-University Training Contest - Team 9
题意:求问一个有向图中任意两个点对(i,j),是否符合其中的一个点能够到达另一个点。
题解:tarjan缩环,然后在DAG上进行拓扑排序,如果每层拓扑进入队列中的点数大于1,则这些点不可能互相到达。
(还是犯傻了,数组没开够……RE无数次,之前还写的dfs,脑子估计是不存在的。)
#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<stack>#include<queue>#define LiangJiaJun main#define ll long longusing namespace std;int T;queue<int>q;struct edge{ int to,nt;}e[12004],ed[12004];int n,m,h[2004],ne,scc,belong[2004],dfn[2004],low[2004];int cnt,l[2004],ru[2004];bool inq[2004];stack<int>st;void add(int u,int v){e[++ne].to=v;e[ne].nt=h[u];h[u]=ne;}void add2(int u,int v){ed[++cnt].to=v;ed[cnt].nt=l[u];l[u]=cnt;}void tarjan(int x){ dfn[x]=low[x]=++cnt; inq[x]=1;st.push(x); for(int i=h[x];i;i=e[i].nt){ if(!dfn[e[i].to]){ tarjan(e[i].to); low[x]=min(low[x],low[e[i].to]); } else if(inq[e[i].to])low[x]=min(low[x],dfn[e[i].to]); } if(dfn[x]==low[x]){ ++scc;int now=0; while(now!=x){ now=st.top();st.pop(); inq[now]=0;belong[now]=scc; } }}void rebuild(){ cnt=1; for(int i=1;i<=n;i++){ for(int j=h[i];j;j=e[j].nt){ if(belong[e[j].to]!=belong[i]){ add2(belong[i],belong[e[j].to]); ru[belong[e[j].to]]++; } } }}int w33ha(){ memset(belong,0,sizeof(belong)); memset(h,0,sizeof(h)); memset(ru,0,sizeof(ru)); memset(inq,0,sizeof(inq)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(l,0,sizeof(l)); while(!q.empty())q.pop(); scc=0;ne=1;cnt=0; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); } for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i); rebuild(); for(int i=1;i<=scc;i++)if(ru[i]==0)q.push(i); while(!q.empty()){ if(q.size()>1)return puts("Light my fire!"),0; int x=q.front();q.pop(); for(int i=l[x];i;i=ed[i].nt){ --ru[ed[i].to]; if(!ru[ed[i].to])q.push(ed[i].to); } } puts("I love you my love and our love save us!"); return 0;}int LiangJiaJun (){ scanf("%d",&T); while(T--)w33ha(); return 0;}
- HDU 6165 FFF at Valentine
- HDU-6165 FFF at Valentine
- HDU 6165 FFF at Valentine
- HDU 6165 FFF at Valentine
- hdu--6165--FFF at Valentine
- HDU 6165 FFF at Valentine
- hdu 6165 FFF at Valentine
- hdu-6165 FFF at Valentine
- [HDU 6165]FFF at Valentine
- HDU-6165-FFF at Valentine
- FFF at Valentine HDU
- FFF at Valentine HDU
- Hdu 6165 FFF at Valentine【Tarjan强连通+暴搜】
- hdu 6165 FFF at Valentine (数学思想 || 缩点)
- hdu 6165 FFF at Valentine (Tarjan算法,scc+dp)
- 【多校训练】hdu 6165 FFF at Valentine
- hdu 6165 FFF at Valentine(传递闭包)
- hdu 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- HDU 1003
- JS函数声明和预解析的理解
- 编译Android 7.0 内核报错
- 数据库知识整合
- POJ
- [HDU 6165]FFF at Valentine
- Apache Shiro学习笔记(二)身份验证获取SecurityManager
- Python进行参数调优GridSearchCV和RandomizedSearchCV
- grep查找
- ztree异步加载全部数据,数据量过万,如何缩短加载时间
- Scala柯里化函数
- 通过mnist数字识别理解卷积神经网络
- HDU 1176
- 备份还原sqlsever数据库