2 条题解

  • -2
    @ 2023-7-16 15:45:13
    using namespace std;
    int f[47843],e[43665],ans[54647];
    int get(int x)
    {
    	if(f[x]==x) return x;
    	return f[x]=get(f[x]);
    }
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
    	int n,k,x,y;
    	char opt;
    	cin>>n>>k;
    	for(int i=1;i<=n;i++) f[i]=i;
    	memset(e,0,sizeof(e));
    	for(int i=1;i<=k;i++)
    	{
    		cin>>opt>>x>>y;
    		if(opt=='F') f[get(x)]=get(y);
    		if(opt=='E')
    		{
    			if(e[x]==0) e[x]=get(y);
    			else f[get(y)]=get(e[x]);
    			if(e[y]==0) e[y]=get(x);
    			else f[get(x)]=get(e[y]);
    		}
    	}
    	int cnt=0;
    	memset(ans,0,sizeof(ans));
    	for(int i=1;i<=n;i++) ans[get(i)]++;
    	for(int i=1;i<=n;i++) if(ans[i]>0) cnt++;
    	cout<<cnt;
    	return 0;
    }
    
    
    • -3
      @ 2023-7-16 14:47:12
      #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[20010], n, m, x, y, tx, ty, ans;
      vector<int> enmy[20010];
      char opt;
      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<=2*n; i++) fth[i]=i;
      	for(int i=0; i<m; i++)
      	{
      		cin>>opt>>x>>y;
      		if(opt=='F') fth[fnd(x)]=fnd(y);
      		else
      		{
      			fth[fnd(x+n)]=fnd(y);
      			fth[fnd(y+n)]=fnd(x);
      		}
      	}
      	for(int i=1; i<=n; i++) if(fth[i]==i) ans++;
      	cout<<ans;
      	return 0;
      } 
      
      • 1

      信息

      ID
      457
      时间
      1000ms
      内存
      128MiB
      难度
      4
      标签
      递交数
      42
      已通过
      23
      上传者