【链接】http://acm.hdu.edu.cn/showproblem.php?pid=6165
【题意】
一张有向图,n个点,m条边,保证没有重边和自环。询问任意两个点能否满足任何一方能够到达另外一方。
【题解】
用Tarjan算法,先把有向图的强连通分量缩成一个点,缩完点之后,剩下的就是一张有向无环图了.
对其进行拓扑排序.一定要唯一的拓扑排序才能够满足题目的要求.
也即,为一条链的时候.
一旦某个时刻做拓扑排序的队列大小大于1就输出无解
【错的次数】
0
【反思】
在这了写反思
【代码】
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) #define ri(x) scanf("%d",&x) #define rl(x) scanf("%lld",&x) #define rs(x) scanf("%s",x) #define oi(x) printf("%d",x) #define ol(x) printf("%lld",x) #define oc putchar(' ') #define os(x) printf(x) #define all(x) x.begin(),x.end() #define Open() freopen("F:\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 1e3; vector <int> G[N+10],g[N+10]; int n,m,tot = 0,top = 0,dfn[N+10],low[N+10],z[N+10],totn,du[N+10],in[N+10]; int bh[N+10]; queue <int> dl; void dfs(int x){ dfn[x] = low[x] = ++ tot; z[++top] = x; in[x] = 1; int len = G[x].size(); rep1(i,0,len-1){ int y = G[x][i]; if (!dfn[y]){ dfs(y); low[x] = min(low[x],low[y]); }else if (in[y] && dfn[y]<low[x]){ low[x] = dfn[y]; } } if (low[x]==dfn[x]){ int v = 0; totn++; while (v!=x){ v = z[top]; in[v] = 0; bh[v] = totn; top--; } } } bool ju(){ return ((int) dl.size()) > 1; } bool ok(){ while (!dl.empty()) dl.pop(); rep1(i,1,n) if (du[i]==0) dl.push(i); while (!dl.empty()){ if (ju()) return false; int x = dl.front(); dl.pop(); int len = g[x].size(); rep1(i,0,len-1){ int y = g[x][i]; du[y]--; if (du[y]==0){ dl.push(y); } } } return true; } int main(){ //Open(); //Close(); int T; ri(T); while (T--){ ms(dfn,0); ms(du,0); ms(in,0); tot = 0,totn = 0; ri(n),ri(m); rep1(i,1,n) G[i].clear(),g[i].clear(); rep1(i,1,m){ int x,y; ri(x),ri(y); G[x].pb(y); } rep1(i,1,n) if (dfn[i]==0) dfs(i); rep1(i,1,n){ int len = G[i].size(); int xx = bh[i]; rep1(j,0,len-1){ int y = G[i][j]; int yy = bh[y]; if (xx!=yy){ g[xx].pb(yy); du[yy]++; } } } n = totn; if (!ok()) puts("Light my fire!"); else puts("I love you my love and our love save us!"); } return 0; }