FFF at Valentine

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060    Accepted Submission(s): 506

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
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!
Source
分析:缩点为DAG,则如果在拓扑序中出现了有两个及以上入度为0的点则不合法
下面给出AC代码:
 #include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN=;
const int MAXM=;
struct Edge{
int to,next;
}edge[MAXM],edge2[MAXM];
int head[MAXN],head2[MAXN],tot,tot2;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[MAXN];
int num[MAXN];
int in[MAXN],out[MAXN];
void addedge(int u,int v){
edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;
}
void addedge2(int u,int v){
edge2[tot2].to=v;edge2[tot2].next=head2[u];head2[u]=tot2++;
}
void Tarjan(int u){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
for(int i=head[u];i!=-;i=edge[i].next){
v=edge[i].to;
if(!DFN[v]){
Tarjan(v);
if(Low[u]>Low[v])Low[u]=Low[v];
}
else if(Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if(Low[u]==DFN[u]){
scc++;
do{
v=Stack[--top];
Instack[v]=false;
Belong[v]=scc;
num[scc]++;
}
while(v!=u);
}
}
void solve(int N){
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
memset(num,,sizeof(num));
Index=scc=top=;
for(int i=;i<=N;i++){
if(!DFN[i])
Tarjan(i);
}
} bool map2[MAXN][MAXN];
void build(int n){
memset(map2,false,sizeof(map2));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(head2,-,sizeof(head2));tot2=;
for(int i=;i<=n;i++){
for(int j=head[i];j!=-;j=edge[j].next){
int v=edge[j].to;
int a=Belong[i];
int b=Belong[v];
if(a==b)continue;
if(!map2[a][b]){
addedge2(a,b);
map2[a][b]=true;
in[b]++;out[a]++;
}
}
}
} void init(){
tot=;
memset(head,-,sizeof(head));
} bool Top(){
queue<int >q;
while(!q.empty())q.pop();
for(int i=;i<=scc;i++){
if(in[i]==)q.push(i);
} while(!q.empty()){
if(q.size()!=)return false;
int u=q.front();
q.pop();
for(int i=;i<=scc;i++){
if(map2[u][i]==true) {
in[i]--;
if(in[i]==)q.push(i);
}
}
}
return true;
} int n,m;
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
}
solve(n);
build(n); if(!Top()){printf("Light my fire!\n");}
else printf("I love you my love and our love save us!\n");
} return ;
}

2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  2. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  3. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. iOS OC Swift3.0 TableView 中tableviewcell的线左边不到边界

    Swift 3.0 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt index ...

  2. iOS支付宝支付相关问题

    支付宝实现以及相关问题:http://www.jianshu.com/p/f81578954974 1.支付宝支付流程 1.用户点击支付2.客户端请求服务器用户支付3.服务器接收请求生成金额订单等要给 ...

  3. Nodejs进阶:crypto模块中你需要掌握的安全基础

    本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址. 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信 ...

  4. UWP 使用OneDrive云存储2.x api(二)【全网首发】

    接上一篇 http://www.cnblogs.com/hupo376787/p/8032146.html 上一篇提到为了给用户打造一个完全无缝衔接的最佳体验,UWP开发者最好也要实现App设置和数据 ...

  5. Linux(CentOS6.5)下编译安装Nginx官方最新稳定版(nginx-1.10.0)

    注:此文已经更新为新版:http://comexchan.cnblogs.com/p/5815753.html ,请直接查看新版,谢谢! 本文地址http://comexchan.cnblogs.co ...

  6. ASP.NET Core文件上传与下载(多种上传方式)

    前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在整理吧. ASP.NET Core 2.0 发展到现在,已经 ...

  7. FPGA 设计总结(1)

    1. set_input_delay和set_output_delay的选项-max和-min的理解 首先 input/output其实是模拟数据在端口外的延时,是一个外部约束条件,目的是为了约束FP ...

  8. 5 秒创建 k8s 集群 - 每天5分钟玩转 Docker 容器技术(115)

    据说 Google 的数据中心里运行着超过 20 亿个容器,而且 Google 十年前就开始使用容器技术. 最初,Google 开发了一个叫 Borg 的系统(现在命令为 Omega)来调度如此庞大数 ...

  9. 我 对jvm 创建线程的一些了解

    1.jvm 每创建一个线程都会对应产生一个该线程的虚拟机栈,栈大小通过-Xss参数来设置,JDK1.5之后默认为1M 2.JVM创建线程需要内存,但这部分内存不使用堆内存(毕竟JVM虚拟机栈).对于3 ...

  10. js拖拽的封装

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...