HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)
FFF at Valentine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 575 Accepted Submission(s): 281
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 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.
If the couple can survive, print “I love you my love and our love save us!”
Otherwise, print “Light my fire!”
题目链接:HDU 6165
似乎比较模版的一道题目,一开始以为是一旦某个缩点后的点的出度超过2就不行了,实际上是可以的,比如1->2、2->3、1->3,这样是三个连通分量且1的出度为2,但是任意取两个点$a,b$还是可以从$a$到达$b$或者$b$到达$a$的,因此更进一步应该是考虑拓扑序上是否同时存在两个可行的点,如果存在说明可以走分岔路这样一来至少分岔路上的两个点就是无法到达的
代码:
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 10010;
const int M = 60010;
struct edge
{
int to, nxt;
} E[M], e[M];
int head[N], tot;
int dfn[N], low[N], st[N], ins[N], scc, in[N], belong[N], ts, top;
int n, m;
int H[N], Tot; void init()
{
CLR(head, -1);
tot = 0;
CLR(dfn, 0);
CLR(low, 0);
CLR(ins, 0);
scc = 0;
CLR(in, 0);
ts = top = 0;
CLR(H, -1);
Tot = 0;
}
inline void add(int s, int t)
{
E[tot].to = t;
E[tot].nxt = head[s];
head[s] = tot++;
}
inline void Add(int s, int t)
{
e[Tot].to = t;
e[Tot].nxt = H[s];
H[s] = Tot++;
}
void Tarjan(int u)
{
dfn[u] = low[u] = ++ts;
ins[u] = 1;
st[top++] = u;
int v;
for (int i = head[u]; ~i; i = E[i].nxt)
{
v = E[i].to;
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
++scc;
do
{
v = st[--top];
ins[v] = 0;
belong[v] = scc;
} while (u != v);
}
}
int solve()
{
queue<int>Q;
int i, j;
for (i = 1; i <= n; ++i)
if (!dfn[i])
Tarjan(i);
for (i = 1; i <= n; ++i)
{
for (j = head[i]; ~j; j = E[j].nxt)
{
int v = E[j].to;
if (belong[v] == belong[i])
continue;
++in[belong[v]];
Add(belong[i], belong[v]);
}
}
for (i = 1; i <= scc; ++i)
if (!in[i])
Q.push(i);
while (!Q.empty())
{
if (Q.size() >= 2)
return 0;
int u = Q.front();
Q.pop();
for (int i = H[u]; ~i; i = e[i].nxt)
{
int v = e[i].to;
if (--in[v] == 0)
Q.push(v);
}
}
return 1;
}
int main(void)
{
int T, a, b;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d%d", &n, &m);
while (m--)
{
scanf("%d%d", &a, &b);
add(a, b);
}
solve() ? puts("I love you my love and our love save us!") : puts("Light my fire!");
}
return 0;
}
HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)的更多相关文章
- 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6165 FFF at Valentine
题目大意:给出一个有向图,问你这个图中是否对于任意两点\(u,v\),都至少满足\(u\to v\)(\(u\)可到达\(v\),下同)或\(v\to u\)中的一个. 一看就是套路的图论题,我们先把 ...
- [模板]tarjan缩点+拓扑排序
题目:给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 题目简述:先t ...
- [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序
---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...
- 【洛谷 P1073】 最优贸易 (Tarjan缩点+拓扑排序)
题目链接 先\(Tarjan\)缩点,记录每个环内的最大值和最小值. 然后跑拓扑排序,\(Min[u]\)表示到\(u\)的最小值,\(ans[u]\)表示到\(u\)的答案,\(Min\)和\(an ...
- [ZJOI2007]最大半连通子图 (Tarjan缩点,拓扑排序,DP)
题目链接 Solution 大概是个裸题. 可以考虑到,如果原图是一个有向无环图,那么其最大半联通子图就是最长的一条路. 于是直接 \(Tarjan\) 缩完点之后跑拓扑序 DP就好了. 同时由于是拓 ...
- [luogu2272 ZJOI2007] 最大半连通子图 (tarjan缩点 拓扑排序 dp)
传送门 题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向 ...
- 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)
题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...
- bzoj5017 [Snoi2017]炸弹 (线段树优化建图+)tarjan 缩点+拓扑排序
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5017 题解 这个题目方法挺多的. 线段树优化建图 线段树优化建图的做法应该挺显然的,一个炸弹能 ...
随机推荐
- 洛谷题解:P1209 【[USACO1.3]修理牛棚 Barn Repair】
原题传送门:https://www.luogu.org/problemnew/show/P1209 首先,这是一道贪心题. 我们先来分析它的贪心策略. 例如,样例: 4 50 18 3 4 6 ...
- ethereum(以太坊)(三)--合约单继承与多继承
pragma solidity ^0.4.0; // priveta public internal contract Test{ //defualt internal uint8 internal ...
- PPT入门学习笔记1:待修改
一直被比人忽悠实在是累了,我可以接受自己的失误,但我接受不了别人一次又一次的坑我! 做PPT的原则是什么? 1.一个目标: "一个PPT只为一类人服务,针对不同的听众制作不同层次内容的PPT ...
- 分布式爬虫:使用Scrapy抓取数据
分布式爬虫:使用Scrapy抓取数据 Scrapy是Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘. ...
- Wind Of Change
Wind of change until the end 变革的风一直吹直至最后 You will see that I will be your friend 你会看见我成为你的朋友 If you ...
- 2 js的20/80关键知识
1. 2 var a = 1; undefined a 1 alert(a); undefined var b = true; var c = "Hi"; undefined al ...
- Error:Java home supplied via 'org.gradle.java.home' is invalid
Finally i found my solution. In the project root i found gradle.properties configure this java home ...
- ITIBB原创,互联网首部自媒体小说《1024伐木累》-小白篇之入职-总章节一
小序 IT人不懂爱?代码汪是小白?又有谁,懂我情怀? 逗比青年,背上行囊,懵懵懂懂闯帝都!前途似海,来日方长! 青春无梦妄少年!认定就作,不平就说,碰撞火花,如此绚烂…… IT人有比格?其实,那是顽强 ...
- Percona-Tookit工具包之pt-slave-restart
Preface Sometimes,the threads(especially the SQL_Thread) will be terminated by accident.The ...
- 解决ubuntu发热严重的问题
对于双显卡PC安装ubuntu ,风扇狂转,发热严重,原因基本双显卡的优化导致. 解决具体步骤如下: 命令行输入sudo apt-get install bumblebee bumblebee-nvi ...