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)
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

2017 Multi-University Training Contest - Team 9

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165

分析:缩点为DAG,则如果在拓扑序中出现了有两个及以上入度为0的点则不合法

下面给出AC代码:

  1 #include <iostream>
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 const int MAXN=1010;
  5 const int MAXM=6010;
  6 struct Edge{
  7     int to,next;
  8 }edge[MAXM],edge2[MAXM];
  9 int head[MAXN],head2[MAXN],tot,tot2;
 10 int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
 11 int Index,top;
 12 int scc;
 13 bool Instack[MAXN];
 14 int num[MAXN];
 15 int in[MAXN],out[MAXN];
 16 void addedge(int u,int v){
 17     edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;
 18 }
 19 void addedge2(int u,int v){
 20     edge2[tot2].to=v;edge2[tot2].next=head2[u];head2[u]=tot2++;
 21 }
 22 void Tarjan(int u){
 23     int v;
 24     Low[u]=DFN[u]=++Index;
 25     Stack[top++]=u;
 26     Instack[u]=true;
 27     for(int i=head[u];i!=-1;i=edge[i].next){
 28         v=edge[i].to;
 29         if(!DFN[v]){
 30             Tarjan(v);
 31             if(Low[u]>Low[v])Low[u]=Low[v];
 32         }
 33         else if(Instack[v]&&Low[u]>DFN[v])
 34             Low[u]=DFN[v];
 35     }
 36     if(Low[u]==DFN[u]){
 37         scc++;
 38         do{
 39             v=Stack[--top];
 40             Instack[v]=false;
 41             Belong[v]=scc;
 42             num[scc]++;
 43         }
 44         while(v!=u);
 45     }
 46 }
 47 void solve(int N){
 48     memset(DFN,0,sizeof(DFN));
 49     memset(Instack,false,sizeof(Instack));
 50     memset(num,0,sizeof(num));
 51     Index=scc=top=0;
 52     for(int i=1;i<=N;i++){
 53         if(!DFN[i])
 54             Tarjan(i);
 55     }
 56 }
 57
 58 bool map2[MAXN][MAXN];
 59 void build(int n){
 60     memset(map2,false,sizeof(map2));
 61     memset(in,0,sizeof(in));
 62     memset(out,0,sizeof(out));
 63     memset(head2,-1,sizeof(head2));tot2=0;
 64     for(int i=1;i<=n;i++){
 65         for(int j=head[i];j!=-1;j=edge[j].next){
 66             int v=edge[j].to;
 67             int a=Belong[i];
 68             int b=Belong[v];
 69             if(a==b)continue;
 70             if(!map2[a][b]){
 71                 addedge2(a,b);
 72                 map2[a][b]=true;
 73                 in[b]++;out[a]++;
 74             }
 75         }
 76     }
 77 }
 78
 79 void init(){
 80     tot=0;
 81     memset(head,-1,sizeof(head));
 82 }
 83
 84 bool Top(){
 85     queue<int >q;
 86     while(!q.empty())q.pop();
 87     for(int i=1;i<=scc;i++){
 88         if(in[i]==0)q.push(i);
 89     }
 90
 91     while(!q.empty()){
 92         if(q.size()!=1)return false;
 93         int u=q.front();
 94         q.pop();
 95         for(int i=1;i<=scc;i++){
 96             if(map2[u][i]==true) {
 97                 in[i]--;
 98                 if(in[i]==0)q.push(i);
 99             }
100         }
101     }
102     return true;
103 }
104
105 int n,m;
106 int main()
107 {
108     int T;
109     scanf("%d",&T);
110     while(T--){
111         scanf("%d%d",&n,&m);
112         init();
113         for(int i=0;i<m;i++){
114             int u,v;
115             scanf("%d%d",&u,&v);
116             addedge(u,v);
117         }
118         solve(n);
119         build(n);
120
121         if(!Top()){printf("Light my fire!\n");}
122         else printf("I love you my love and our love save us!\n");
123     }
124
125     return 0;
126 }
时间: 2024-10-01 04:41:34

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

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

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s): 562 Problem Description Sample Input 1 a 2 aa bb 3 a ba abc Sample Output Case #1: 25 Case #2: 132

2017&quot;百度之星&quot;程序设计大赛 - 复赛1005&amp;&amp;HDU 6148 Valley Numer【数位dp】

Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 311    Accepted Submission(s): 165 Problem Description 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有出现先递增接

HDU校赛 | 2019 Multi-University Training Contest 3

2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 1004. Distribution of books 考虑二分答案,设当前二分出来的是\(x\). 设\(f_i\)表示前\(i\)个能分成最多的段数,使得每一段和都\(\leqslant x\). 转移显然,枚举一个\(j\),若\(s_i-s_j\leqslant x\)则转移,\(s_i\)表示前

HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2

思路来源于 FXXL - - 一个比较奇怪的地方就是第三步可以不做,也就是ans至少为1,听说场内有提问的,然后 admin 说可以不做- - (wa的我心烦) /* HDU 6049 - Sdjpx Is Happy [ 枚举,剪枝 ] | 2017 Multi-University Training Contest 2 题意: 长度为N的排列 N <= 3000 排序分三个步骤: 1.原数组分为不相交的K段 2.每段都独立排序 3.选择其中两段swap 问按步骤能成功排序的K能取到的最大是多

hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5802 Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 333 Problem Description Long long ago, there was a

HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has

hdu 5774 Where Amazing Happens(2016 Multi-University Training Contest 4——打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5774 Where Amazing Happens Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 443    Accepted Submission(s): 300 Problem Description As the premier m

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an