当前位置:网站首页>Parallel search exercise 1: circle of friends
Parallel search exercise 1: circle of friends
2022-06-21 22:52:00 【yitahutu79】
Title Description
A circle of friends , Not all of them know each other directly .
for example : Xiao Zhang's friend is Xiao Li , Xiao Li's friend is Xiao Wang , Then the three of them belong to a circle of friends .
Now give some people's friends , People follow from 1 To n In the middle of this, the number will ask whether two people belong to a circle of friends , Please write a program , To achieve this process .
Input
Enter two integers on the first line n,m(1≤n≤10000,3≤m≤100000), Represent the number of people and operands respectively .
Next m That's ok , Three whole... Per line a,b,c(a∈[1,2], 1≤b,c≤n)
When a=1 when , Represents adding a new piece of known information ,b,c It's a friend.
When a=2 when , According to the above information , inquiry b,c Whether it's a friend
Output
For each a=2 The operation of , Output 『Yes』 or 『No』 The representative asked whether the two people were friends .
The sample input
6 5
1 1 2
2 1 3
1 2 4
1 4 3
2 1 3
Sample output
No
Yes
Data scale and agreement
The time limit :1 s
Memory limit :64 M
100% Data assurance for n,m(1≤n≤10000,3≤m≤100000)
Method :
1.quick_find
#include <stdio.h>
#include <stdlib.h>
typedef struct UnionSet {
int* color;
int n;
}UnionSet;
UnionSet* init(int n) {
UnionSet* u = (UnionSet*)malloc(sizeof(UnionSet));
u->color = (int*)malloc(sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++) {
u->color[i] = i;
}
u->n = n;
return u;
}
int find(UnionSet* u, int x) {
return u->color[x];
}
int merge(UnionSet* u, int a, int b) {
if (find(u, a) == find(u, b)) return 0;
int color_a = u->color[a];
for (int i = 1; i <= u->n; i++) {
if (u->color[i] - color_a) continue;
u->color[i] = u->color[b];
}
return 1;
}
void clear(UnionSet* u) {
if (u == NULL) return;
free(u->color);
free(u);
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
UnionSet* u = init(n);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
switch (a) {
case 1: merge(u, b, c); break;
case 2:printf("%s\n", find(u, b) == find(u, c) ? "Yes" : "No"); break;
}
}
clear(u);
return 0;
}


2.quick_union
#include <stdio.h>
#include <stdlib.h>
typedef struct UnionSet {
int* father;
int n;
}UnionSet;
UnionSet* init(int n) {
UnionSet* u = (UnionSet*)malloc(sizeof(UnionSet));
u->father = (int*)malloc(sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++) {
u->father[i] = i;
}
u->n = n;
return u;
}
int find(UnionSet* u, int x) {
if (u->father[x] == x) return x;
return find(u, u->father[x]);
}
int merge(UnionSet* u, int a, int b) {
int fa =find(u, a), fb = find(u, b);
if (fa == fb) return 0;
u->father[fa] = fb;
return 1;
}
void clear(UnionSet* u) {
if (u == NULL) return;
free(u->father);
free(u);
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
UnionSet* u = init(n);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
switch (a) {
case 1: merge(u, b, c); break;
case 2:printf("%s\n", find(u, b) == find(u, c) ? "Yes" : "No"); break;
}
}
clear(u);
return 0;
}


- Optimize 1:weighted quick_union
#include <stdio.h>
#include <stdlib.h>
#define swap(a,b) {
\ a ^= b,b ^= a, a ^= b;\ }
typedef struct UnionSet {
int* father, *size;
int n;
}UnionSet;
UnionSet* init(int n) {
UnionSet* u = (UnionSet*)malloc(sizeof(UnionSet));
u->father = (int*)malloc(sizeof(int) * (n + 1));
u->size = (int*)malloc(sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++) {
u->father[i] = i;
u->size[i] = 1;
}
u->n = n;
return u;
}
int find(UnionSet* u, int x) {
if (u->father[x] == x) return x;
return find(u, u->father[x]);
}
int merge(UnionSet* u, int a, int b) {
int fa =find(u, a), fb = find(u, b);
if (fa == fb) return 0;
if (u->size[fa] < u->size[fb]) swap(fa, fb);
u->father[fb] = fa;
u->size[fa] += u->size[fb];
return 1;
}
void clear(UnionSet* u) {
if (u == NULL) return;
free(u->father);
free(u->size);
free(u);
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
UnionSet* u = init(n);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
switch (a) {
case 1: merge(u, b, c); break;
case 2:printf("%s\n", find(u, b) == find(u, c) ? "Yes" : "No"); break;
}
}
clear(u);
return 0;
}


- Optimize 2: Path compression + weighted quick_union
#include <stdio.h>
#include <stdlib.h>
#define swap(a,b) {
\ a ^= b,b ^= a, a ^= b;\ }
typedef struct UnionSet {
int* father, *size;
int n;
}UnionSet;
UnionSet* init(int n) {
UnionSet* u = (UnionSet*)malloc(sizeof(UnionSet));
u->father = (int*)malloc(sizeof(int) * (n + 1));
u->size = (int*)malloc(sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++) {
u->father[i] = i;
u->size[i] = 1;
}
u->n = n;
return u;
}
int find(UnionSet* u, int x) {
if (u->father[x] == x) return x;
return u->father[x] = find(u, u->father[x]);
}
int merge(UnionSet* u, int a, int b) {
int fa =find(u, a), fb = find(u, b);
if (fa == fb) return 0;
if (u->size[fa] < u->size[fb]) swap(fa, fb);
u->father[fb] = fa;
u->size[fa] += u->size[fb];
return 1;
}
void clear(UnionSet* u) {
if (u == NULL) return;
free(u->father);
free(u->size);
free(u);
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
UnionSet* u = init(n);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
switch (a) {
case 1: merge(u, b, c); break;
case 2:printf("%s\n", find(u, b) == find(u, c) ? "Yes" : "No"); break;
}
}
clear(u);
return 0;
}


- Optimize 3: Path compression quick_union
#include <stdio.h>
#include <stdlib.h>
typedef struct UnionSet {
int* father;
int n;
}UnionSet;
UnionSet* init(int n) {
UnionSet* u = (UnionSet*)malloc(sizeof(UnionSet));
u->father = (int*)malloc(sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++) {
u->father[i] = i;
}
u->n = n;
return u;
}
int find(UnionSet* u, int x) {
if (u->father[x] == x) return x;
return u->father[x] = find(u, u->father[x]);
}
int merge(UnionSet* u, int a, int b) {
int fa =find(u, a), fb = find(u, b);
if (fa == fb) return 0;
u->father[fb] = fa;
return 1;
}
void clear(UnionSet* u) {
if (u == NULL) return;
free(u->father);
free(u);
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
UnionSet* u = init(n);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
switch (a) {
case 1: merge(u, b, c); break;
case 2:printf("%s\n", find(u, b) == find(u, c) ? "Yes" : "No"); break;
}
}
clear(u);
return 0;
}

边栏推荐
猜你喜欢

Flink real-time risk control rule engine

Translation software Bob installation tutorial

4. ESP8266通过OLED实时显示DHT11温湿度参数

WPF dependent properties
Swiftui basic learning journal (XI) SQLite data operation

numpy矩阵初等变换

UWP 阴影效果

Better manage all kinds of music, professional DJ music management software pioneer DJ rekordbox

. File header parsing of BMP pictures

更好的管理各种音乐,专业的DJ音乐管理软件Pioneer DJ rekordbox
随机推荐
1016. 子串能表示从 1 到 N 数字的二进制串
Design summary of Verilog divider
Pycharm下可以正常运行,Pyinstaller打包软件报出Fatal error
翻译软件Bob安装教程
2022-06-21:golang选择题,以下golang代码输出什么?A:3;B:4;C:100;D:编译失败。 package main import (
postgis 如何用米制单位 buffer
Go服务平台项目(一)数据库表的设计与Gendry库的使用
Bitmap使用注意事项
Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
Pycharm使用指南
小程序如何关联微信小程序二维码,并实现二码合一聚合
花200W买流量,不如0成本起步做独立站私域运营收益高!
[wustctf2020] plain and unpretentious -1
C WindowFromPoint is invalid in 64 bit programs
UWP 确认是否有弹窗显示
Five minutes, Xie Yunyuan
Matlab2020a使用App Designer如何导出exe
Livres obligatoires
语音断点检测(短时改进子带谱熵)
matplotlib画图显示中文