ZOJ1740(Message System)用并查集

Message System

Time Limit: 1 Second      Memory Limit: 32768 KB

Little Kawaii has built a simple message system to process the Academy Cute Message(ACM). The message system consists of many endpoints, which are connected by message channels. The messages can only pass across the channels, and will be originated at any endpoint. When receiving or originating a message, each endpoint will process the message then send the message to the channels that it connects to, except the one from which the message comes. In case the endpoint is the originator, the message will be send to all the channels it connects to. Academy Cute Message is very important, so if any message is generated, it requires that all endpoints will receive it properly. However, processing the message requires tremendous resources, so Little Kawaii hopes that the endpoints will not receive duplicated messages. Given the description of the message system, Little Kawaii asks you to write a program to determine whether the system could fulfill the requirements.

Input

The input consists of several test cases, separated by a blank line. Each case starts with 2 integer N and M, separated by white spaces. N represents the number of endpoints in the system, 1 <= N <= 1000; 0 <= M <= N * (N - 1) / 2. Then M lines of input follows, each consists of 2 integer A and B, separated by white spaces, 1 <= A, B <= N, meaning there is a message channel between endpoint A and B. There will be at most one channel between two endpoints, and there is no channel connects an endpoint to itself. A test case starting with two 0(zero) signals the end of the input, you should not process it.

Output

For each test case, output "Yes" in a single line, if the message system fulfills Little Kawaii's requirements, otherwise output "No" in a single line.

Sample Input
4 3
1 2
2 3
3 4
3 1
2 3
0 0
Sample Output
Yes
No
用并查集做的时候需判断二个问题:
1.是否连通
2.是否有回路
第一个问题好解决,因为并查集本来就有“查”这一特性,
第二个问题的解决之道在于并得时候查找合并的二个集合是否属于同一个集合,若是,则说明有回路。
这是我用第一种并查集做的,其效率“并”是,“查”是
源代码:
ExpandedBlockStart.gifContractedBlock.gif/**//*Message is very important, so if any message is generated, 
it requires that all endpoints will receive it properly. However, 
processing the message requires tremendous resources, 
so Little Kawaii hopes that the endpoints will not receive duplicated messages.
*/

#include 
<iostream>
using namespace std;

const int MAX = 1003;

int cnt[MAX];
int n, m;

int findRoot(int x)
ExpandedBlockStart.gifContractedBlock.gif
{
    
return cnt[x];
}


void Merge(int a, int b)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int i, j, k;
    i 
= cnt[a] < cnt[b]? cnt[a] : cnt[b];
    j 
= cnt[a] > cnt[b]? cnt[a] : cnt[b];
    
for(k = 1; k <= n; k++)
        
if(cnt[k] == j)
            cnt[k] 
= i;
}


int main()
ExpandedBlockStart.gifContractedBlock.gif
{

    
int i, a, b, ra, rb;
    
bool flag ;//判断有无回路
    while(cin>>n>>m)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(!&& !m)
            
break;
        
for(i = 1; i <= n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            cnt[i] 
= i;
        }

        flag 
= true;
        
for(i = 1; i <= m; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            scanf(
"%d%d",&a,&b);
            
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ra 
= findRoot(a);
                rb 
= findRoot(b);
                
if(ra == rb)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    flag 
= false;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Merge(a, b);
                }

            }

        }

        
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for(i = 2; i <= n; i++)//判断是否连通
                if(cnt[i] != cnt[1])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    flag 
= false;
                    
break;
                }

        }

        
if(flag)
            cout
<<"Yes"<<endl;
        
else
            cout
<<"No"<<endl;
    }

    
return 0;
}
然后从学长那边找到一种效率更高的并查集实现方法,代码如下:
#include<stdio.h>
int un(int a[],int l)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(a[l]==l)return l;
    
else return un(a,a[l]);
}

int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
int n,m,i,x,y,k,p,a[1005],flag;
    
while(scanf("%d %d",&n,&m)!=EOF)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(n==0&&m==0)break;
        
for(i=0;i<=n;i++)a[i]=i;
        flag
=1;
        
for(i=1;i<=m;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            scanf(
"%d %d",&x,&y);
            k
=un(a,x);
            p
=un(a,y);
            
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if(k==p)flag=0;
                
else if(p==y&&k!=x)a[p]=x;
                
else if(k==x&&p!=y)a[x]=y;
                
else if(k!=x&&p!=y)a[k]=y;
                
else if(x==k&&p==y)a[y]=x;
            }

        }

        
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            x
=un(a,1);
            
for(i=2;i<=n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(un(a,i)!=x){flag=0;break;}
        }

        
if(flag)printf("Yes\n");
        
else printf("No\n");
    }

    
return 0;
}

当然并查集的第二种实现方法是树形结构,其“并”效率为,“查”效率为


转载于:https://www.cnblogs.com/Xredman/archive/2009/03/30/1424772.html

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值