hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
来源:互联网 发布:linux编写c语言 编辑:程序博客网 时间:2024/06/11 17:19
FFF at Valentine
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
3
5 5
1 2
2 3
2 4
3 5
4 5
3 3
1 2
2 3
3 1
5 5
1 2
2 3
3 1
3 4
4 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!
题意:给你一个无自环、无重边的有向图,问图中任意两点之间是否都存在一条路径可达
思路:可能有环存在,因此我们可以先用tarjan求出强连通分量,然后缩点建立新图,再利用拓扑排序判断新图中的每一层是否只有一个入度为0的点(每一层只有一个入度为0的点时满足题意)
代码:
#include<bits/stdc++.h>using namespace std;const int maxn=1000+5;struct edge{ int v,next;} E[maxn*6];int DFN[maxn],LOW[maxn],Belong[maxn],Stack[maxn];int first[maxn],in[maxn];bool Instack[maxn];int n,m,len,top,Index,Bcnt;vector<int>p[maxn];void Init(){ memset(in,0,sizeof(in)); memset(DFN,0,sizeof(DFN)); memset(LOW,0,sizeof(LOW)); memset(Stack,0,sizeof(Stack)); memset(first,-1,sizeof(first)); memset(Belong,0,sizeof(Belong)); memset(Instack,false,sizeof(Instack)); len=top=Index=Bcnt=0; for(int i=1; i<=n; ++i) p[i].clear();}void add_edge(int u,int v){ E[len].v=v,E[len].next=first[u],first[u]=len++;}void tarjan(int u){ int v; DFN[u]=LOW[u]=++Index; Stack[++top]=u; Instack[u]=true; for(int i=first[u]; ~i; i=E[i].next) { v=E[i].v; if(!DFN[v]) { tarjan(v); LOW[u]=min(LOW[u],LOW[v]); } else if(Instack[v]) LOW[u]=min(LOW[u],DFN[v]); } if(DFN[u]==LOW[u]) { ++Bcnt; do { v=Stack[top--]; Instack[v]=false; Belong[v]=Bcnt; } while(u!=v); }}bool judge(){ int num=0,u; queue<int>q; for(int i=1; i<=Bcnt; ++i) if(!in[i]) ++num,q.push(i); if(num>1) return false; while(!q.empty()) { u=q.front(); q.pop(),num=0; for(int i=0; i<p[u].size(); ++i) { --in[p[u][i]]; if(!in[p[u][i]]) ++num,q.push(p[u][i]); } if(num>1) return false; } return true;}int main(){ int t,u,v; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); Init(); for(int i=0; i<m; ++i) { scanf("%d%d",&u,&v); add_edge(u,v); } for(int i=1; i<=n; ++i) if(!DFN[i]) tarjan(i); for(int i=1; i<=n; ++i) { u=Belong[i]; for(int j=first[i]; ~j; j=E[j].next) { v=Belong[E[j].v]; if(u!=v) p[u].push_back(v),++in[v]; } } if(judge()) printf("I love you my love and our love save us!\n"); else printf("Light my fire!\n"); } return 0;}
tarjan算法求强连通分量:九野
tarjan算法模板:sentimental_dog
- hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
- hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
- 文章标题 HDU 6165: FFF at Valentine(强连通分量缩点)
- hdu 6165 FFF at Valentine(强连通分量缩点+dfs)
- hdu 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- hdu6165 FFF at Valentine【强联通缩点+拓扑排序】
- hdu6165 FFF at Valentine 强联通分量+拓扑排序
- HDU 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- HDU 6165 FFF at Valentine(强连通 缩点 17多校第九场)
- HDU --- 6165 FFF at Valentine 多校第九场 【强联通缩点 + 维护拓扑序】
- HDU 6165 FFF at Valentine(强连通缩点)
- Hdu 6165 FFF at Valentine【Tarjan强连通+暴搜】
- HDU 6165 FFF at Valentine (tarjan缩点+拓扑判任意两点联通)
- 2017多校九 05题 hdu 6165 FFF at Valentine 缩点 dp找最长链/拓扑排序
- HDU6165 FFF at Valentine(并查集+tarjan缩点+拓扑排序)
- 2017年多校赛第九场 1005 FFF at Valentine(缩点+拓扑排序)
- hdu 6165 FFF at Valentine (数学思想 || 缩点)
- hdu6165 FFF at Valentine 强联通缩点
- 截图表格转可编辑Word文本教程
- Effective Java中文第三章第12节(个人渣翻)
- php删除指定位置数组元素
- 生活源于专注
- maven--pom.xml
- hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
- 开发单点登录系统(SSO)时需要注意的几个问题
- 裸机实验体验之usb启动配合dnw工具下载
- ZOJ-3732 Graph Reconstruction(判定无向图度数序列能否可图化)
- 事件回调 和通知方式的总结(EventBus框架的使用)
- OnClose()和 OnDestroy()
- 【安全牛学习笔记】WEBSHELL
- OpenCV-将图像转化为灰度图,然后边缘检测
- c++ inline variable 内联变量 c++17