HDU 6165 FFF at Valentine (Tarjan indent + topology sort)

Source: Internet
Author: User

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

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 the separate cells of the jail randomly. But as the saying Goes:there are always a-to-do, the lovers made a bet with lsh:if either of them can reach the cell O f the other one and then LSH have to let them go.
The jail is formed of several cells and each cell have some special portals connect to a specific cell. One can be transported to the connected cell by the portal, but being transported back is impossible. There are not being a portal connecting a cell and itself, and since the cost of a portal are pretty expensive, LSH would not Tolerate the fact that the portals connect exactly the same and the cells.
As an enthusiastic person of the FFF group, your is quit curious about whether the lovers can survive or not. So, 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 a 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 the integer u and V, which indicates a portal from U to v.

Outputif The couple can survive, print "I love your My Love and we love Save Us!"
Otherwise, print "Light My fire!"

Sample Input35 51 22 32 43 54 5 3 31 22 33 1 5 51 22 33 13 44 5

Sample Outputlight My fire! I Love your My Love and we love Save Us! I Love your My Love and we love Save Us!

Title Link: HDU 6165

Seems to compare the template of a topic, at the beginning of the thought is once a point after a contraction point out of more than 2, it is actually possible, such as $ \to 2$,$2 \to 3$,$1 \to 3$, this is three connected components and 1 out of 2, but any two points $a,b$ or can be $a$ Arrive at $b$ or $b$ to reach $a$, so go further should consider whether there are two feasible points in the topological sequence, if there is a description can be divided fork so that at least two points on the road is unreachable

Code:

#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 your My Love and your love to Save Us!"): Puts ("Light my fire!"); } return 0;}

HDU 6165 FFF at Valentine (Tarjan indent + topology sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.