当前位置:网站首页>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);
	}
}





原网站

版权声明
本文为[Weng Weiqiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241510456544.html