赞
踩
- 作者简介:一名后端开发人员,每天分享后端开发以及人工智能相关技术,行业前沿信息,面试宝典。
- 座右铭:未来是不可确定的,慢慢来是最快的。
- 个人主页:极客李华-CSDN博客
- 合作方式:私聊+
- 这个专栏内容:BAT等大厂常见后端java开发面试题详细讲解,更新数目100道常见大厂java后端开发面试题。
- 我的CSDN社区:https://bbs.csdn.net/forums/99eb3042821a4432868bb5bfc4d513a8
- 微信公众号,抖音,b站等平台统一叫做:极客李华,加入微信公众号领取各种编程资料,加入抖音,b站学习面试技巧,职业规划
简介:本文讲解最新版的IDEA2022如何创建web项目。
点击File->New->Project->New Project
输入项目名称Name
输入项目路径:Location
Build system选择Maven
选择jdk版本
点击创建Create
点击Add Framework Support
一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。
现在要进行 m 个操作,操作共有两种:
M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作;
Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中;
输入格式
第一行输入整数 n 和 m。
接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。
输出格式
对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 No。
每个结果占一行。
数据范围
1≤n,m≤105
输入样例:
4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4
输出样例:
Yes
No
Yes
提交代码
#include<iostream>
using namespace std;
const int N = 100010;
int n, m;
int p[N];
int find(int x) // 找到x的祖先节点
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) p[i] = i;
while (m--)
{
char op;
int a, b;
scanf (" %c%d%d", &op, &a, &b);
if (op == 'M') p[p[find(a)]] = find(b); // 让a的祖先节点指向b的祖先节点
else
{
if (find(a) == find(b)) puts("Yes");
else puts("No");
}
}
return 0;
}
import java.io.*;
public class Main
{
static int N = 100010;
static int n, m;
static int [] p = new int [N];
static int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
String [] str = reader.readLine().split(" ");
n = Integer.parseInt(str[0]);
m = Integer.parseInt(str[1]);
for (int i = 1; i <= n; ++ i) p[i] = i;
while (m -- > 0)
{
String op;
int a, b;
str = reader.readLine().split(" ");
op = str[0];
a = Integer.parseInt(str[1]);
b = Integer.parseInt(str[2]);
if (op.equals("M")) p[find(a)] = find(b);
else
{
if (find(a) == find(b)) System.out.println("Yes");
else System.out.println("No");
}
}
}
}
如果大家觉得有用的话,可以关注我下面的微信公众号,极客李华,我会在里面更新更多行业资讯,企业面试内容,编程资源,如何写出可以让大厂面试官眼前一亮的简历,让大家更好学习编程,我的抖音,B站也叫极客李华。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。