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





相关文章
|
2月前
|
人工智能 JSON 编解码
Wan2.2+ComfyUI 本地制作外星人趣味感人短片全流程实战:分镜、角色一致性、批量渲染与后期自动化脚本
本项目为AI短片《星星花》全流程技术指南:基于ComfyUI,使用Wan2.2 T2V/I2V模型,RTX4090显卡实测优化;含8镜分镜表、IP-Adapter角色一致性方案、Python批量渲染+FFmpeg自动合成脚本,附真实踩坑清单与夸克资源包。
311 1
|
27天前
|
人工智能 安全 API
Codex 运行速度突然变慢怎么办?大量用户反馈 GPT-5.6 更新后执行效率下降,原因分析与解决方法
Codex更新GPT-5.6后普遍变慢:思考延迟、Agent耗时、文件操作卡顿。主因或为模型推理策略调整、服务端负载增加、上下文膨胀及安全检查增强。临时方案:降Thinking等级、减少Agent、控制上下文、禁用插件、换用Terra。最新客户端下载见codexdown.cc。(239字)
395 0
|
4月前
|
数据采集 人工智能 监控
从SEO到GEO:B2B出海企业AI认知工程的12步闭环架构与实践
基于B2B外贸出海实践,见增出海提出一套可落地的GEO技术实施框架与12步闭环架构,涵盖内容结构化(Schema.org/JSON-LD)、多信源矩阵分发、AI爬虫策略、客户信号追踪及销售触达闭环。重点探讨GEO如何同时服务Inbound(海外买家主动验证)与Outbound(基于预热信号的精准触达)两条路径,避免"做了GEO就等客上门"的常见误区。适合技术团队、架构师及出海企业CTO参考。
430 1
从SEO到GEO:B2B出海企业AI认知工程的12步闭环架构与实践
|
5月前
|
供应链 安全 计算机视觉
实验室器皿目标检测数据集| 7200张YOLO实验室自动化数据集
本数据集含7200张真实实验室场景图像,标注21类常用器皿(如烧杯、量筒、分液漏斗等),覆盖遮挡、倾斜、复杂光照与杂乱背景,支持YOLO/COCO/VOC格式,专为实验室自动化巡检、机械臂引导及细粒度目标检测研究设计。
|
6月前
|
人工智能 前端开发 安全
我受够了在四个 AI 编程工具之间当"复制粘贴工程师",于是写了 Roundtable
Roundtable 是一款零依赖、纯本地的 AI 编程助手协作框架。让 Claude Code、Codex、Reasonix、ZCode 自动组队分工——发任务、认领、交付,全靠共享文件看板与轻量 CLI。不联网、不上传、不设服务器,1000 行 Python 实现 AI 团队自治。你,只管喝茶。
|
6月前
|
人工智能 自然语言处理 搜索推荐
蚂蚁百宝箱正式发布AI构建能力:自然语言一键生成企业级智能体,助力业务创新提效
5月21日,蚂蚁百宝箱上线全新AI构建能力,支持自然语言一键生成智能体、营销活动与场景化Skill,深度融合行业资产与工程化能力,零代码、高可用、可交付。新用户注册即赠海量tokens,速体验!
1168 2
使用Excel随机分配固定人数到不同组里
本文介绍了如何使用Excel为学生随机分配学科或分组。通过插入=RAND()生成随机数,并结合RANK.AVG和ROUNDUP函数实现按固定人数分组,或用CHOOSE与RANDBETWEEN实现非固定分组。操作简单实用,适用于各类随机分配场景。
使用Excel随机分配固定人数到不同组里
|
移动开发 前端开发 JavaScript
淘宝小部件 Canvas 渲染流程与原理全解析
淘宝小部件 Canvas 渲染流程与原理全解析
1118 0
淘宝小部件 Canvas 渲染流程与原理全解析
|
弹性计算 Oracle 固态存储
阿里云ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选?
阿里云服务器ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选择?不同性能级别对应的单盘IOPS性能上限、IO和吞吐量都不同,ESSD云盘容量越大可选择的PL级别越高,性能级别PL越高价格也越贵,阿里云百科来详细说下阿里云ESSD云盘不同性能级别区别以及选择方法:
6611 0
阿里云ESSD云盘性能级别PL0、PL1、PL2和PL3怎么选?
|
NoSQL MongoDB 开发者
Python与MongoDB的亲密接触:从入门到实战的代码指南
本文详细介绍了Python与MongoDB结合使用的实战技巧,涵盖环境搭建、连接管理、CRUD操作、高级查询、索引优化、事务处理及性能调优等内容。通过15个代码片段,从基础到进阶逐步解析,帮助开发者掌握这对黄金组合的核心技能。内容包括文档结构设计、批量操作优化、聚合管道应用等实用场景,适合希望高效处理非结构化数据的开发者学习参考。
723 0