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;
  }
}





相关文章
|
4月前
|
机器学习/深度学习 算法 数据处理
灰狼优化算法(GWO)改进LightGBM - 光伏功率预测附Matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 🍎 往期回顾关注个人主页:Matlab科研工作室 👇 关注我领取海量matlab电子书和数学建模资料 🍊个人信条:格物致知,完整Matlab代码获取及仿真咨询内容私信。 🔥 内容介绍 光伏功率输出受太阳辐照度、环境温度、风速等多因素耦合影响,具有强随机性、波动性与非线性特征,精准预测是提升光伏系统消纳效率与电网稳定性的关键。传统 Light
|
移动开发 前端开发 JavaScript
淘宝小部件 Canvas 渲染流程与原理全解析
淘宝小部件 Canvas 渲染流程与原理全解析
939 0
淘宝小部件 Canvas 渲染流程与原理全解析
|
存储 消息中间件 缓存
Flink(十二)【容错机制】(3)
Flink(十二)【容错机制】
|
监控 网络协议 数据安全/隐私保护
确定 OSPF 邻居关系问题原因的方法
【8月更文挑战第24天】
460 0
|
设计模式 存储 Java
【类图、类与类的关系、多态】
学习Java面向对象,掌握UML类图绘制,包括14种图形,使用PowerDesigner演示类图创建。理解类与类的关系,如继承、实现、依赖、关联、聚合、组合,以及`instanceof`关键字。学习简单工厂设计模式,实现多态,理解其在面试和设计原则中的重要性。通过实例操作,如String类常用方法、汽车与4S店案例,加深对面向对象概念的理解。最后,探讨面向对象设计原则,如单一职责、开闭原则、里氏替换原则、依赖倒置、接口隔离、迪米特法则和组合/聚合复用原则。
432 1
|
弹性计算 Oracle 固态存储
阿里云ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选?
阿里云服务器ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选择?不同性能级别对应的单盘IOPS性能上限、IO和吞吐量都不同,ESSD云盘容量越大可选择的PL级别越高,性能级别PL越高价格也越贵,阿里云百科来详细说下阿里云ESSD云盘不同性能级别区别以及选择方法:
5934 0
阿里云ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选?
|
Unix Linux Perl
sed删除指定行
sed删除指定行
2337 1
|
机器学习/深度学习 存储 API
GGML 非官方中文文档(2)
GGML 非官方中文文档
1384 0
|
Ubuntu Linux iOS开发
如何实现多个Python环境的Python版本切换
【8月更文挑战第4天】如何实现多个Python环境的Python版本切换
4034 5
|
存储 消息中间件 算法
RocketMQ平台的消息灰度方案(1)
RocketMQ平台的消息灰度方案
923 0