MZOJ #70 FFF团
分析
没太多好说的,强连通缩点,然后拓扑排序,一旦某一时刻有两个及以上入度为零的点,则不可能
代码
1 /************************** 2 User:Mandy.H.Y 3 Language:c++ 4 Problem:FFF 5 Apgorithm: 6 **************************/ 7 8 #include<bits/stdc++.h> 9 #define Max(x,y) ((x) > (y) ? (x) : (y)) 10 #define Min(x,y) ((x) < (y) ? (x) : (y)) 11 12 using namespace std; 13 14 const int maxn = 1005; 15 const int maxm = 6005; 16 17 int t,n,m,size,first[maxn],first1[maxn],size1; 18 int dfn[maxn],low[maxn],tot,cnt,ind[maxn],father[maxn]; 19 int s[maxn],tp; 20 bool vis[maxn]; 21 22 struct Edge{ 23 int u,v,nt; 24 }edge[maxm],edge1[maxm]; 25 26 template<class T>inline void read(T &x){ 27 x = 0;bool flag = 0;char ch = getchar(); 28 while(!isdigit(ch)) flag |= ch == '-',ch = getchar(); 29 while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar(); 30 if(flag) x = -x; 31 } 32 33 template<class T>void putch(const T x){ 34 if(x > 9) putch(x / 10); 35 putchar(x % 10 | 48); 36 } 37 38 template<class T>void put(const T x){ 39 if(x < 0) putchar('-'),putch(-x); 40 else putch(x); 41 } 42 43 void file(){ 44 freopen("FFF.in","r",stdin); 45 freopen("FFF.out","w",stdout); 46 } 47 48 void init(){ 49 memset(first,0,sizeof(first)); 50 memset(father,0,sizeof(father)); 51 memset(dfn,0,sizeof(dfn)); 52 memset(low,0,sizeof(low)); 53 memset(vis,0,sizeof(vis)); 54 memset(edge,0,sizeof(edge)); 55 memset(edge1,0,sizeof(edge1)); 56 size = 0;tp = 0;size1 = 0;cnt = 0; 57 tot = 0; 58 } 59 60 void eadd(int u,int v){ 61 edge[++size].v = v; 62 edge[size].u = u; 63 edge[size].nt = first[u]; 64 first[u] = size; 65 } 66 67 void eadd1(int u,int v){ 68 edge1[++size1].v = v; 69 edge1[size1].u = u; 70 edge1[size1].nt = first1[u]; 71 first1[u] = size1; 72 } 73 74 75 void tarjan(int u){ 76 dfn[u] = low[u] = ++tot; 77 vis[u] = 1; 78 s[tp++] = u; 79 80 for(int i = first[u];i;i = edge[i].nt){ 81 int v = edge[i].v; 82 if(!dfn[v]){ 83 tarjan(v); 84 low[u] = min(low[u],low[v]); 85 }else if(vis[v]) low[u] = min(low[u],dfn[v]); 86 } 87 88 if(dfn[u] == low[u]){ 89 int x;++cnt; 90 do{ 91 x = s[tp-1]; 92 tp--; 93 father[x] = cnt; 94 vis[x] = 0; 95 }while(x != u); 96 } 97 } 98 99 bool tpsort(){ 100 tp = 0; 101 for(int i = 1;i <= cnt; ++ i) if(!ind[i]) s[tp] = i,tp++; 102 if(tp >= 2) return 0; 103 while(tp){ 104 if(tp >= 2) return 0; 105 int u = s[tp-1]; 106 tp--; 107 for(int i = first1[u];i;i = edge1[i].nt){ 108 int v = edge1[i].v; 109 --ind[v]; 110 if(!ind[v]) s[tp] = v,tp++; 111 if(tp >= 2) return 0; 112 } 113 if(tp >= 2) return 0; 114 } 115 return 1; 116 } 117 118 void readdata(){ 119 read(n);read(m); 120 for(int i = 1;i <= m; ++ i){ 121 int u,v; 122 read(u);read(v); 123 eadd(u,v); 124 } 125 126 } 127 128 void Tarjan(){ 129 for(int i = 1;i <= n; ++ i){ 130 if(!dfn[i]) tarjan(i); 131 ind[i] = first1[i] = 0; 132 } 133 134 } 135 136 void EADD(){ 137 for(int i = 1;i <= m; ++ i){ 138 int u = edge[i].u; 139 int v = edge[i].v; 140 if(father[u] != father[v]){ 141 ind[father[v]]++; 142 eadd1(father[u],father[v]); 143 } 144 } 145 146 } 147 148 void work(){ 149 init(); 150 readdata(); 151 Tarjan(); 152 EADD(); 153 if(tpsort()){ 154 puts("I love you my love and our love save us!"); 155 }else puts("Light my fire!"); 156 } 157 158 int main(){ 159 // file(); 160 read(t); 161 while(t--) 162 work(); 163 return 0; 164 }
非做顽石不可,哪管他敬仰暗唾