【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1529

 

【题目大意】

  给出一张n个点n条边的有向图,问选取几个点为起点可以遍历全图

 

【题解】

  由于是n条边,因此图为基环森林,选取环上点一定可以到达连通块内所有点,
  因此只要统计连通块个数即可。

 

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1000100;
int f[N],n;
int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
int main(){
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=n;i++){
        	int x; 
            scanf("%d",&x);
            f[sf(i)]=sf(x);
        }int ans=0;
        for(int i=1;i<=n;i++)if(sf(i)==i)ans++;
        printf("%d\n",ans);
    }return 0;
}

相关文章:

  • 2022-12-23
  • 2022-01-31
  • 2022-02-17
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-11-06
  • 2021-06-21
猜你喜欢
  • 2021-10-26
  • 2022-01-17
  • 2021-06-10
  • 2021-10-27
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案