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





相关文章
|
1天前
|
云安全 人工智能 自然语言处理
|
9天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
648 56
Meta SAM3开源:让图像分割,听懂你的话
|
6天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
319 116
|
5天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
21天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
439 32
|
4天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
272 132

热门文章

最新文章