题意:给你一个有向图,问是否对于任意的X,Y两个顶点,可以从X走到Y,或从Y走到X
思路:是或!!!如果没有或字的话那么就是简单的判断一个图是否为强连通。有或字了的话就相当于转化为原图中每一个强连通分量之间是否连通的问题。为什么呢,因为强连通分量内部是肯定可以从X走到Y和Y走到X的了。然后缩点,对缩点形成的图最多只能由一个入度为0的点,如果有多个入度为0的点则它们肯定不连通,缩点形成的图是一棵树,入度为0的点就是这棵树的树根,而且这棵树只能是单链,一旦有分叉,那么分叉点就不满足题目。相当于找一个全序,直接拓扑排序就可以了。
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
#define maxn 1000+100
#define LL long long
int cas=1,T;
vector<int>G[maxn];
vector<int>NewG[maxn];
int pre[maxn];
int lowlink[maxn];
int sccno[maxn];
int dfs_clock,scc_cnt;
int indegree[maxn];
int n,m;
stack<int>S;
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for (int i = 0;i<G[u].size();i++)
{
int v = G[u][i];
if (!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if (!sccno[v])
{
lowlink[u] = min (lowlink[u],pre[v]);
}
}
if (lowlink[u] == pre[u])
{
scc_cnt++;
for (;;)
{
int x = S.top();S.pop();
sccno[x] = scc_cnt;
if (x==u)
break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=0;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for (int i = 1;i<=n;i++)
if (!pre[i])
dfs(i);
}
int in[maxn];
int out[maxn];
bool topo()
{
queue<int> Q;
int sum = 0;
for (int i = 1;i<=scc_cnt;i++)
if (indegree[i]==0)
Q.push(i);
while (!Q.empty())
{
if (Q.size()>1) //队列里面只有一个元素才能保证是全序
return false;
int u = Q.front();Q.pop();
sum++;
for (int i = 0;i<NewG[u].size();i++)
{
int v = NewG[u][i];
if (--indegree[v]==0)
Q.push(v);
}
}
return sum == scc_cnt;
}
int main()
{
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i = 0;i<=n;i++)
{
G[i].clear();
NewG[i].clear();
}
memset(indegree,0,sizeof(indegree));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for (int i = 1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
find_scc(n);
for (int i = 1;i<=scc_cnt;i++)
{
in[i]=out[i]=1;
}
for (int u = 1;u<=n;u++)
for (int i =0;i<G[u].size();i++)
{
int v = G[u][i];
if (sccno[u] != sccno[v])
{
in[sccno[v]]=out[sccno[u]]=0;
NewG[sccno[u]].push_back(sccno[v]);
indegree[sccno[v]]++;
}
}
printf("%s\n",topo()?"Yes":"No");
}
return 0;
}
题目