3 条题解

  • 0
    @ 2023-7-16 14:38:22
    #include <bits/stdc++.h>
    #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    #define re register
    using namespace std;
    typedef long long ll;
    int fth[1000010], n, m, x, y, tx, ty, ans, cnt[1000010];
    int fnd(int t)
    {
    	return (fth[t]==t?t:fth[t]=fnd(fth[t]));
    }
    int main()
    {
    	IOS;
    	cin>>n>>m;
    	for(int i=0; i<=n; i++) fth[i]=i;
    	for(int i=0; i<m; i++)
    	{
    		cin>>x>>y;
    		tx=fnd(x), ty=fnd(y);
    		if(tx!=ty) fth[ty]=tx;
    	}
    	for(int i=1; i<=n; i++) cnt[fnd(i)]++, ans=max(ans, cnt[fnd(i)]);
    	cout<<ans;
    	return 0;
    }
    
    • -2
      @ 2023-7-16 14:37:44
      #include<bits/stdc++.h>
      using namespace std;
      int fa[30005],n,m,h[30005],maxx;
      int get(int x){
      	return (fa[x]==x?x:fa[x]=get(fa[x]));
      }
      int main(){
      	ios_base::sync_with_stdio(false);
      	cin.tie(0);
      	cout.tie(0);
      	cin>>n>>m;
      	for(int i=1;i<=n;i++)fa[i]=i;
      	for(int i=1;i<=m;i++){
      		int x,y;
      		cin>>x>>y;
      		fa[get(x)]=get(y);
      	}
      	for(int i=1;i<=n;i++)h[get(i)]++;
      	for(int i=1;i<=n;i++)maxx=max(maxx,h[i]);
      	cout<<maxx;
      	return 0;
      }
      
      • -2
        @ 2023-7-16 14:37:36
        #include<bits/stdc++.h>
        using namespace std;
        int fa[30005],n,m,h[30005],maxx;
        int get(int x){
        	return (fa[x]==x?x:fa[x]=get(fa[x]));
        }
        int main(){
        	ios_base::sync_with_stdio(false);
        	cin.tie(0);
        	cout.tie(0);
        	cin>>n>>m;
        	for(int i=1;i<=n;i++)fa[i]=i;
        	for(int i=1;i<=m;i++){
        		int x,y;
        		cin>>x>>y;
        		fa[get(x)]=get(y);
        	}
        	for(int i=1;i<=n;i++)h[get(i)]++;
        	for(int i=1;i<=n;i++)maxx=max(maxx,h[i]);
        	cout<<maxx;
        	return 0;
        }
        
        • 1

        信息

        ID
        455
        时间
        1000ms
        内存
        256MiB
        难度
        2
        标签
        递交数
        128
        已通过
        58
        上传者