博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
家族(并查集)
阅读量:7303 次
发布时间:2019-06-30

本文共 1004 字,大约阅读时间需要 3 分钟。

典型并查集,只需要判断find(  x ) 和find( y) 是否在一个集合里面即可

 

// File Name: wiki1073.cpp// Author: bo_jwolf// Created Time: 2013年08月17日 星期六 16时36分22秒#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 5005 ;int fa[ maxn ] ;struct node{ int x , y ;}edge[ maxn << 2 ] ;int find( int x ){ return fa[ x ] = fa[ x ] == x ? x : find( fa[ x ] ) ;}int main(){ int n , m , p ; while( cin >> n >> m >> p ) { for( int i = 0 ; i <= n ; ++i ) fa[ i ] = i ; for( int i = 1 ; i <= m ; ++i ) { cin >> edge[ i ].x >> edge[ i ].y ; } for( int i = 1 ; i <= m ; ++i ) { int x = find( edge[ i ].x ) ; int y = find( edge[ i ].y ) ; if( x != y ) { fa[ x ] = y ; } } int x , y ; for( int i = 1 ; i <= p ; ++i ) { cin >> x >> y ; if( find( x ) == find( y ) ) cout << "Yes" << endl ; else cout << "No" << endl ; } } return 0;}

 

 

转载地址:http://xxhnm.baihongyu.com/

你可能感兴趣的文章
rsyncd.conf 教程
查看>>
python入门(二)数据库操作
查看>>
Python 中的 10 个常见安全漏洞,以及如何避免(下)
查看>>
如何学习一门新语言或框架
查看>>
mavenjar包下载不了的解决办法
查看>>
Js获取Url中参数及Url中传递中文参数乱码
查看>>
9.新浪微博Swift项目第九天
查看>>
Ajax与JSON的一些总结
查看>>
孩子考98,老师家长为何只关注另外失去的2分
查看>>
javascript中sort()函数的理解
查看>>
利用svn钩子hooks/post-commit实现代码自动部署
查看>>
互联网通用架构技术----分布式事务解决方案
查看>>
mysql安装
查看>>
nginx安装 nginx: [emerg] getpwnam(“www”) failed 错误
查看>>
WebView实现文件下载功能
查看>>
fetch 超时处理
查看>>
乐观锁&悲观锁
查看>>
IntelliJ IDEA 2016.2激活方法汇总
查看>>
10大最重要的Web安全风险之四--A4-不安全的直接对象引用
查看>>
Android Studio创建的Android项目一般需要忽略
查看>>