#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
const int N=1e3+5,M=2e4+5;
int n,m;
int res=0;
int head1[N],head2[N],cnt1,cnt2;
bool st1[N],st2[N];
struct node
{
int v,next;
}edge1[M],edge2[M];
void addedge1(int u,int v)
{
cnt1++;
edge1[cnt1].v=v;
edge1[cnt1].next=head1[u];
head1[u]=cnt1;
}
void addedge2(int u,int v)
{
cnt2++;
edge2[cnt2].v=v;
edge2[cnt2].next=head2[u];
head2[u]=cnt2;
}
void dfs(int u,int head[],bool st[],node edge[])
{
st[u]=1;
for(int i=head[u];i;i=edge[i].next)
{
int j=edge[i].v;
if(!st[j])dfs(j,head,st,edge);
}
}
int main()
{
cin>>n>>m;
while(m--)
{
int a,b;cin>>a>>b;
addedge1(a,b),addedge2(b,a);
}
int res=0;
for(int i=1;i<=n;i++)
{
memset(st1,0,sizeof st1);
memset(st2,0,sizeof st2);
dfs(i,head1,st1,edge1);
dfs(i,head2,st2,edge2);
int s=0;
for(int j=1;j<=n;j++)
{
if(st1[j]||st2[j])s++;
}
if(s==n)res++;
}
cout<<res<<endl;
}
[CSP]通信网络(链式前向星)
最新推荐文章于 2025-12-22 09:43:16 发布
4万+

被折叠的 条评论
为什么被折叠?



