CF #738(div2)D1. Mocha and Diana (Easy Version)(暴力,并查集)

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF #738(div2)D1. Mocha and Diana (Easy Version)(暴力,并查集)相关的知识,希望对你有一定的参考价值。

problem

D1. Mocha and Diana (Easy Version)
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is the easy version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved.

A forest is an undirected graph without cycles (not necessarily connected).

Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that:

After adding edges, both of their graphs are still forests.
They add the same edges. That is, if an edge (u,v) is added to Mocha’s forest, then an edge (u,v) is added to Diana’s forest, and vice versa.
Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.

Input
The first line contains three integers n, m1 and m2 (1≤n≤1000, 0≤m1,m2<n) — the number of nodes and the number of initial edges in Mocha’s forest and Diana’s forest.

Each of the next m1 lines contains two integers u and v (1≤u,v≤n, u≠v) — the edges in Mocha’s forest.

Each of the next m2 lines contains two integers u and v (1≤u,v≤n, u≠v) — the edges in Diana’s forest.

Output
The first line contains only one integer h, the maximum number of edges Mocha and Diana can add (in each forest).

Each of the next h lines contains two integers u and v (1≤u,v≤n, u≠v) — the edge you add each time.

If there are multiple correct answers, you can print any one of them.

Examples
inputCopy
3 2 2
1 2
2 3
1 2
1 3
outputCopy
0
inputCopy
5 3 2
5 4
2 1
4 3
4 3
1 4
outputCopy
1
2 4
inputCopy
8 1 2
1 7
2 6
1 5
outputCopy
5
5 2
2 3
3 4
4 7
6 8
Note
In the first example, we cannot add any edge.

In the second example, the initial forests are as follows.

We can add an edge (2,4).

D1。摩卡与戴安娜(简易版)
每次测试的时间限制1秒
每个测试的内存限制 256 兆字节
输入标准输入
输出标准输出
这是问题的简单版本。两个版本之间的唯一区别是对 n 的约束。只有当问题的所有版本都得到解决时,您才能进行黑客攻击。

森林是无环的无向图(不一定是连通的)。

Mocha 和 Diana 是枝江的朋友,他们都有一个森林,节点编号从 1 到 n,他们想给他们的森林添加边,使得:

添加边后,它们的两个图仍然是森林。
它们添加了相同的边。也就是说,如果一条边 (u,v) 添加到 Mocha 的森林中,那么一条边 (u,v) 会添加到 Diana 的森林中,反之亦然。
Mocha 和 Diana 想知道他们可以添加的最大边数,以及要添加的边。

输入
第一行包含三个整数 n、m1 和 m2 (1≤n≤1000, 0≤m1,m2<n) — Mocha 森林和 Diana 森林中的节点数和初始边数。

接下来的 m1 行中的每一行都包含两个整数 u 和 v(1≤u,v≤n,u≠v)——摩卡森林中的边缘。

接下来的 m2 行中的每一行都包含两个整数 u 和 v(1≤u,v≤n,u≠v)——戴安娜森林中的边缘。

输出
第一行只包含一个整数 h,即 Mocha 和 Diana 可以添加的最大边数(在每个森林中)。

接下来的 h 行中的每一行都包含两个整数 u 和 v (1≤u,v≤n, u≠v)——你每次添加的边。

如果有多个正确答案,您可以打印其中任何一个。

例子
输入副本
3 2 2
1 2
2 3
1 2
1 3
输出副本
0
输入副本
5 3 2
5 4
2 1
4 3
4 3
1 4
输出副本
1
2 4
输入副本
8 1 2
1 7
2 6
1 5
输出副本
5
5 2
2 3
3 4
4 7
6 8
笔记
在第一个示例中,我们无法添加任何边。

在第二个示例中,初始森林如下。

我们可以添加一条边 (2,4)。

solution

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn = 1010;

int fa1[maxn+10], c1 = 1;
void init1(int n){for(int i = 1; i <= n; i++)fa1[i]=i;}
int find1(int x){return x==fa1[x]?x:fa1[x]=find1(fa1[x]);}
void merge1(int x, int y){x=find1(x);y=find1(y);if(x!=y)fa1[x]=y;}

int fa2[maxn+10], c2 = 1;
void init2(int n){for(int i = 1; i <= n; i++)fa2[i]=i;}
int find2(int x){return x==fa2[x]?x:fa2[x]=find2(fa2[x]);}
void merge2(int x, int y){x=find2(x);y=find2(y);if(x!=y)fa2[x]=y;}


int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n, m1, m2;  cin>>n>>m1>>m2;
	init1(n); init2(n);
	for(int i = 1; i <= m1; i++){
		int x, y;  cin>>x>>y;
		if(find1(x)!=find1(y)){
			c1++;
			merge1(x,y);
		}
	}
	for(int i = 1; i <= m2; i++){
		int x, y;  cin>>x>>y;
		if(find2(x)!=find2(y)){
			c2++;
			merge2(x,y);
		}
	}
	if(c1==n||c2==n){
		cout<<0<<"\\n";
		return 0;
	}
	vector<pair<int,int> >ans;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(i==j)continue;
			int i1 = find1(i), j1=find1(j);
			int i2 = find2(i), j2=find2(j);
			if(i1!=j1 && i2!=j2){
				merge1(i1,j1);
				merge2(i2,j2);
				c1++;
				c2++;
				ans.push_back(make_pair(i,j));
				if(c1==n||c2==n)break;
			}
		}
		if(c1==n||c2==n)break;
	}
	cout<<ans.size()<<"\\n";
	for(auto p : ans){
		cout<<p.first<<" "<<p.second<<"\\n";
	}
	return 0;
}

以上是关于CF #738(div2)D1. Mocha and Diana (Easy Version)(暴力,并查集)的主要内容,如果未能解决你的问题,请参考以下文章

CF #738(div2)C. Mocha and Hiking(构造)

CF #738(div2)B. Mocha and Red and Blue(构造)

CF #738(div2)D2. Mocha and Diana (Hard Version)(贪心,并查集)

解题报告CF DIV2 #ROUND 721 A~C

Codeforces Round #738 (Div. 2) A - D1 题解

Codeforces Round #738 (Div. 2) A - D1 题解