当前位置:网站首页>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);
}
}
边栏推荐
- The most important thing at present
- Several classes of manual transactions
- 权限想要细化到按钮,怎么做?
- Summary of papers on traveling salesman problem (TSP)
- leetcode-201_ 2021_ 10_ seventeen
- 心楼:华为运动健康的七年筑造之旅
- 【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
- Redis+caffeine two-level cache enables smooth access speed
- 壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
- Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
猜你喜欢

使用region折叠代码
![[camera Foundation (I)] working principle and overall structure of camera](/img/5d/c29d636a90d01e5c3852df2a0dd833.png)
[camera Foundation (I)] working principle and overall structure of camera

将二维数组方阵顺时针旋转90°

EasyBypass

二叉搜索树模板

Several classes of manual transactions
![[notes of wuenda] fundamentals of machine learning](/img/71/6192a75446fa7f79469a5483ececc6.jpg)
[notes of wuenda] fundamentals of machine learning

刷题笔记(十八)--二叉树:公共祖先问题

Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)

2022 international women engineers' Day: Dyson design award shows women's design strength
随机推荐
刷题笔记(十八)--二叉树:公共祖先问题
Graduation design of phase 6 of the construction practice camp
[untitled]
Summary of papers on traveling salesman problem (TSP)
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
leetcode_ 191_ 2021-10-15
leetcode1720_2021-10-14
传输层 udp && tcp
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
Multi task model of recommended model: esmm, MMOE
建木持续集成平台v2.5.0发布
《各行业零代码企业应用案例集锦》正式发布
多线程收尾
并查集+建图
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
Tournament sort
权限想要细化到按钮,怎么做?
2022 international women engineers' Day: Dyson design award shows women's design strength
Suspend component and asynchronous component