1 条题解

  • 0
    @ 2025-9-9 23:56:18

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int parent[100010],num[100010];
    int root(int p)
    {
    	if(parent[p]==-1)return p;
    	int t=parent[p];
    	parent[p]=root(parent[p]);
    	num[p]=(num[p]+num[t])%2;
    	return parent[p];
    }
    void merge(int a,int b)
    {
    	int x=root(a),y=root(b);
    	parent[x]=y;
    	num[x]=(num[b]-num[a]+1)%2;
    }
    
    int main()
    {
    	int j,i,m,n;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		memset(parent,-1,sizeof(parent));
    		memset(num,0,sizeof(num));
    		for(i=1;i<=m;i++)
    		{
    			char ch;
    			int a,b;
    			cin>>ch>>a>>b;
    			if(ch=='D')
    			{
    				if(root(a)!=root(b))
    					merge(a,b);
    			}
    			else 
    			{
    				if(root(a)!=root(b))
    					printf("Sorry,I'm not sure.\n");
    				else if(num[a]==num[b])printf("In one alliance.\n");
    				else printf("Not in one alliance.\n");
    			}
    		}
    		
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    2035
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者