3 solutions

  • 0
    @ 2025-6-13 19:19:54
    #include<bits/stdc++.h>
    using namespace std;
    const int N=110;
    int g[N][N];//g[a][b]=1; 
    bool st[N];//st[i]=1表示已经属于某一个家庭了 
    int main()
    {
    	int n,k;
    	cin>>n>>k;
    	while(k--)
    	{
    		int a,b;
    		cin>>a>>b;
    		g[a][b]=g[b][a]=1;
    	}
    	int maxv=0,cnt=0;
    	for(int i=1;i<=n;i++)
    	{
    		if(st[i]==0)
    		{
    			st[i]=1;
    			queue<int> q;
    			q.push(i);
    			cnt++; //家族个数增加 
    			int pcnt=1; //当前家庭的人数
    			while(q.size())  
    			{
    				int t=q.front(); //当前家族的第一个家庭 
    				q.pop();
    				for(int j=1;j<=n;j++) //枚举每个家庭 
    				{
    					if(st[j]==0&&g[t][j]==1) //当前家庭没有加入到家族且和当前家庭有关系 
    					{
    						st[j]=1;
    						pcnt++;
    						q.push(j);
    					}
    				}
    			} 
    			maxv=max(maxv,pcnt);
    		} 
    	}
    	cout<<cnt<<" "<<maxv;
    	return 0;
    }
    
    
    • -3
      @ 2024-5-18 11:38:46

      不给,气不气

      • -3
        @ 2024-5-8 15:48:54

        老师给个题解

        • 1

        Information

        ID
        969
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        5
        Tags
        (None)
        # Submissions
        56
        Accepted
        13
        Uploaded By