HDU 6165 FFF at Valentine(tarjan缩点+拓扑排序)
来源:互联网 发布:c语言为什么叫c语言 编辑:程序博客网 时间:2024/05/20 06:27
FFF at Valentine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 967 Accepted Submission(s): 466
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
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!”
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
题意:
给你一个图,含环,让你判断图内的任意两点能从一个点到另一个点,单向即满足要求。
如果有不满足要求的,则不行。反之则行。
POINT:
先tarjan算法缩点,变成有向无环图。
开始拓扑排序,若途中有两个入度为0的点,则不满足要求。
排序可以结束则满足要求。
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <stack>using namespace std;const int maxn=1100;const int maxm=6600;int low[maxn],DFN[maxn],instack[maxn],tm,cor[maxn],cornum;vector<int> G[maxn],GG[maxn];stack<int>q;void tarjan(int u){ DFN[u]=low[u]=++tm; instack[u]=1; q.push(u); for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(DFN[v]==0) { tarjan(v); if(low[u]>low[v]) low[u]=low[v]; } else if(instack[v]&&low[u]>DFN[v]) low[u]=DFN[v]; } if(DFN[u]==low[u]) { cornum++; int v; do { v=q.top(); cor[v]=cornum; instack[v]=0; q.pop(); }while(v!=u); }}void init(){ for(int i=1;i<maxn;i++) { G[i].clear(); GG[i].clear(); } memset(low,0,sizeof low); memset(DFN,0,sizeof DFN); memset(instack,0,sizeof instack); memset(cor,0,sizeof cor); tm=0; cornum=0;}void add(int u,int v){ G[u].push_back(v);}int ru[maxn];bool Topo(){ int vis[maxn]; memset(vis,0,sizeof vis); for(int i=1;i<=cornum;i++) { int k=0; for(int j=1;j<=cornum;j++) { if(!vis[j]&&ru[j]==0) { if(k!=0) return 0; k=j; } } vis[k]=1; for(int j=0;j<GG[k].size();j++) { ru[GG[k][j]]--; } } return 1;}int main(){ int T; scanf("%d",&T); int uu[maxm],vv[maxm]; while(T--) { init(); int n,m;scanf("%d %d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d %d",&uu[i],&vv[i]); add(uu[i],vv[i]); } for(int i=1;i<=n;i++) if(DFN[i]==0) tarjan(i); // for(int i=1;i<=n;i++) printf("%d ",cor[i]); memset(ru,0,sizeof ru); for(int i=1;i<=m;i++) { if(cor[uu[i]]==cor[vv[i]]) continue; GG[cor[uu[i]]].push_back(cor[vv[i]]); ru[cor[vv[i]]]++; } if(Topo()) { printf("I love you my love and our love save us!\n"); } else printf("Light my fire!\n"); }}
阅读全文
0 0
- hdu 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- HDU 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- HDU 6165 FFF at Valentine (tarjan缩点+拓扑判任意两点联通)
- hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
- hdu 6165 FFF at Valentine(强连通分量缩点+拓扑排序)
- HDU6165 FFF at Valentine(并查集+tarjan缩点+拓扑排序)
- hdu6165 FFF at Valentine【强联通缩点+拓扑排序】
- 2017多校九 05题 hdu 6165 FFF at Valentine 缩点 dp找最长链/拓扑排序
- hdu 6165 FFF at Valentine (Tarjan算法,scc+dp)
- HDU --- 6165 FFF at Valentine 多校第九场 【强联通缩点 + 维护拓扑序】
- 2017年多校赛第九场 1005 FFF at Valentine(缩点+拓扑排序)
- hdu 6165 FFF at Valentine (数学思想 || 缩点)
- HDU 6165 FFF at Valentine(强连通缩点)
- Hdu 6165 FFF at Valentine【Tarjan强连通+暴搜】
- 文章标题 HDU 6165: FFF at Valentine(强连通分量缩点)
- HDU 6165 FFF at Valentine
- HDU-6165 FFF at Valentine
- HDU 6165 FFF at Valentine
- CentOS7下Tomcat启动慢的原因及解决方案
- mysql java开发注意事项
- hdu 6053
- 有关小波的几个术语及常见的小波基介绍
- Linux--线程与进程的总结
- HDU 6165 FFF at Valentine(tarjan缩点+拓扑排序)
- hdu 6171 ida*
- mysql @value -= 用法
- C++之MFC系统图标任务托盘显示
- 【04】淘淘商城-Service层递归拼装ItemCatResult详解
- 字符串模式匹配
- Android开发初级00_1如何在安卓开发之发送广播
- 双色球选号及确认中几等奖
- Codeforces 788B Weird journey (欧拉回路 组合数计数)