当前位置:网站首页>Collective search + drawing creation
Collective search + drawing creation
2022-06-24 21:58:00 【Weng Weiqiang】
subject :https://codeforces.com/problemset/problem/1559/D1
The main idea of the topic :
Two undirected graphs , Given n vertices m1,m2 side , Ask how many more edges can be added to ensure that no loop will be formed , Add two figures at the same time .
Answer key :
1. Use the union search set to find whether it will form a loop , If both figures meet i The vertices yes j The ancestor of node , It means that a loop is formed , dissatisfaction . otherwise , Satisfy
#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<iostream>
using namespace std;
const int N = 1000;
int f1[N + 1];
int f2[N + 1];
int n;
int find1(int x)
{
return x == f1[x] ? x : f1[x] = find1(f1[x]);
}
int find2(int x)
{
return x == f2[x] ? x : f2[x] = find2(f2[x]);
}
void init()
{
for (int i = 1; i <= n; i++)
{
f1[i] = i;
f2[i] = i;
}
}
int main()
{
int m1, m2;
int u, v;
scanf("%d%d%d", &n, &m1, &m2);
init();
for (int i = 1; i <= m1; ++i)
{
scanf("%d %d", &u,&v);
f1[find1(u)] = find1(v);
}
for (int i = 1; i <= m2; ++i)
{
scanf("%d %d", &u, &v);
f2[find2(u)] = find2(v);
}
vector<pair<int, int>>ans;
for (int i = 1; i <= n; i++)
{
for (int j = i+1; j <= n; j++)
{
if (find1(i) != find1(j) && find2(i) != find2(j))
{
ans.push_back({ i,j });
f1[find1(i)] = find1(j);
f2[find2(i)] = find2(j);
}
}
}
printf("%d\n", ans.size());
for (auto it : ans)
{
printf("%d %d\n", it.first, it.second);
}
}
边栏推荐
- 《各行业零代码企业应用案例集锦》正式发布
- leetcode_1470_2021.10.12
- [camera Foundation (I)] working principle and overall structure of camera
- linq查询集合类入门 案例武林高手类
- Introduce the overall process of bootloader, PM, kernel and system startup
- 机器学习:梯度下降法
- 性能测试工具wrk安装使用详解
- Blender FAQs
- cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
- openGauss内核:简单查询的执行
猜你喜欢

【吴恩达笔记】多变量线性回归

Implementing DNS requester with C language

我国SaaS产业的发展趋势与路径

socket(1)

Introduce the overall process of bootloader, PM, kernel and system startup

You are using pip version 21.1.2; however, version 22.1.2 is available

【Camera基础(一)】Camera摄像头工作原理及整机架构

Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network

VSCode无网环境快速迁移开发环境(VIP典藏版)
随机推荐
【无标题】
linq查询集合类入门 案例武林高手类
C language - keyword 1
面试官:你说你精通Redis,你看过持久化的配置吗?
leetcode:1504. 统计全 1 子矩形的个数
Multithreaded finalization
MySQL optimizes query speed
leetcode_ 1470_ 2021.10.12
socket(2)
03--- antireflective film
将二维数组方阵顺时针旋转90°
01--- conditions for interference of two trains of waves at the meeting place
拖动拖动拖动
suspense组件和异步组件
Kubernetes 集群中流量暴露的几种方案
Datakit 代理实现局域网数据统一汇聚
Datakit agent realizes unified data aggregation in LAN
滤波数据分析
印刷行业的ERP软件的领头羊
Xinlou: Huawei's seven-year building journey of sports health