1 solutions

  • 0
    @ 2026-2-26 16:30:27
    #include<bits/stdc++.h>
    using namespace std;
    int n,m,cnt;
    vector<int>g[2010];
    bool st[2010];
    void dfs(int a)
    {
    	st[a]=true;
    	for(auto b:g[a])
    	{
    		if(!st[b])
    		{
    			dfs(b);
    		}
    	}
    }
    signed main()
    {
    	cin>>n>>m;
    	while(m--)
    	{
    		int a,b;
    		cin>>a>>b;
    		g[a].push_back(b);
    	}
    	for(int i=1;i<=n;i++)
    	{
    		memset(st,false,sizeof st);
    		dfs(i);
    		for(int i=1;i<=n;i++)
    		{
    			if(st[i])
    			{
    				cnt++;
    			}
    		}
    	}
    	cout<<cnt;
        return 0;
    }
    
    • 1

    Information

    ID
    2763
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    4
    Accepted
    3
    Uploaded By