D1. Mocha and Diana (Easy Version)<738,div2>

简介: D1. Mocha and Diana (Easy Version)<738,div2>

D1. Mocha and Diana (Easy Version)

time limit per test

1 second

memory limit per test

256 megabytes


input

standard input


output

standard output


This is the easy version of the problem. The only difference between the two versions is the constraint on nn. 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 11 to nn, 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)(u,v) is added to Mocha's forest, then an edge (u,v)(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 nn, m1m1 and m2m2 (1≤n≤10001≤n≤1000, 0≤m1,m2<n0≤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 m1m1 lines contains two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) — the edges in Mocha's forest.


Each of the next m2m2 lines contains two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) — the edges in Diana's forest.


Output


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


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


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


Examples


input


Copy

3 2 2

1 2

2 3

1 2

1 3


output

Copy

0


input

Copy

5 3 2

5 4

2 1

4 3

4 3

1 4


output

Copy

1

2 4


input

Copy

8 1 2

1 7

2 6

1 5


output

Copy

5

5 2

2 3

3 4

4 7

6 8


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int>p;
int a[10010];
int n = 10005;
int father[10005], father1[10005];
void init(int n) {
  for (int i = 1; i <= n; ++i) {
    father[i] = i;
    father1[i] = i;
  }
}
int find1(int u) {//根
  return u == father1[u] ? u : father1[u] = find1(father1[u]);
}
int find(int u) {//根
  return u == father[u] ? u : father[u] = find(father[u]);
}
bool same(int u, int v) {
  u = find(u);
  v = find(v);
  return u == v;
}
int main() {
  int m1, m2;
  cin >> n >> m1 >> m2;
  init(n);//父亲节点是自己
  while (m1--) {
    int u, v;
    cin >> u >> v;
    int x = find(u), y = find(v); //树建立
    if (x != y)
      father[x] = y;
  }
  while (m2--) {
    int u, v;
    cin >> u >> v;
    int x = find1(u), y = find1(v);
    if (x != y)
      father1[x] = y;
  }
  vector<p>v;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int x = find(i), xx = find1(i), y = find(j), yy = find1(j);
      if (x != y && yy != xx) {
        father[x] = y;
        father1[xx] = yy;
        v.push_back(p(i, j));
      }
    }
  }
  int cnt = v.size();
  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}





相关文章
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
471 0
|
4月前
|
JavaScript 编译器
成功解决:Module build failed: Error: Vue packages version mismatch
这篇文章记录了解决Vue项目中遇到的"Module build failed: Error: Vue packages version mismatch"错误的步骤,原因是项目中Vue依赖的版本不一致,解决方法是删除`node_modules`后重新安装指定版本的Vue和`vue-template-compiler`,确保版本匹配,最终成功运行项目。
成功解决:Module build failed: Error: Vue packages version mismatch
|
4月前
Electron——node_modules\ffi-napi\build\Release\ffi_bindings.node is not a valid Win32 application.
Electron——node_modules\ffi-napi\build\Release\ffi_bindings.node is not a valid Win32 application.
66 0
|
5月前
|
JavaScript
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
|
7月前
|
Ruby Perl
The version of CocoaPods used to generate the lockfile
The version of CocoaPods used to generate the lockfile
128 0
Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey` option ...
Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey` option ...
157 0
下载node-gyp依赖包报错:gyp ERR! find VS msvs_version not set from command line or npm config
就是有一点需要注意,我这里安装vs的时候,可用里面没有vs社区版2017,只有2022,只能安装了2022
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
1132 0
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
|
C++
VS Code注释插件doxygen documentation generator
VS Code注释插件doxygen documentation generator
778 0
VS Code注释插件doxygen documentation generator
【解决方案】成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息
成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息
【解决方案】成功解决ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects报错信息