我是靠谱客的博主 娇气小鸽子,最近开发中收集的这篇文章主要介绍2023.1.10(总结),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一D

Problem Statement

You are given a simple undirected graph with NN vertices numbered 11 to NN and MM edges numbered 11 to MM. Edge ii connects vertex u_iui​ and vertex v_ivi​.
Find the number of connected components in this graph.

Notes

simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.

subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

  • 1 leq N leq 1001≤N≤100
  • 0 leq M leq frac{N(N - 1)}{2}0≤M≤2N(N−1)​
  • 1 leq u_i, v_i leq N1≤ui​,vi​≤N
  • The given graph is simple.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM
u_1u1​ v_1v1​
u_2u2​ v_2v2​
vdots⋮
u_MuM​ v_MvM​

Output

Print the answer.

Sample 1

InputcopyOutputcopy
5 3
1 2
1 3
4 5
2

The given graph contains the following two connected components:

  • a subgraph formed from vertices 11, 22, 33, and edges 11, 22;
  • a subgraph formed from vertices 44, 55, and edge 33.

Sample 2

InputcopyOutputcopy
5 0
5

Sample 3

InputcopyOutputcopy
4 6
1 2
1 3
1 4
2 3
2 4
3 4
1

 

这个题目的题意我也不知道怎么去解释,如果根据我的理解那就是找出有几课独立的树。

分析:

1,其实这题从本源上来看就是找关系,看有多少颗不同的树。

2,所以这题用并查集写,用压缩路径然后就可以找到一共有多少课树。

代码如下:

#include<stdio.h>
#include<math.h>
#include<string.h>
int a[200],b[200];
int f(int x)
{
if(a[x]==0)
return x;
return a[x]=f(a[x]);
}
int main()
{
int n,m,k=0;
scanf("%d%d",&n,&m);
if(m==0)
printf("%d",n);
else
{
for(int i=1;i<=m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(f(l)!=f(r))
{
a[f(r)]=f(l);
}
}
for(int i=1;i<=n;i++)
b[a[i]]++;
printf("%d",b[0]);
}
}

 

二,A-B

题目描述

给出一串正整数数列以及一个正整数 CC,要求计算出所有满足 A - B = CA−B=C 的数对的个数(不同位置的数字一样的数对算不同的数对)。

输入格式

输入共两行。

第一行,两个正整数 N,CN,C。

第二行,NN 个正整数,作为要求处理的那串数。

输出格式

一行,表示该串正整数中包含的满足 A - B = CA−B=C 的数对的个数。

输入输出样例

输入 #1复制

4 1
1 1 2 3

输出 #1复制

3

说明/提示

对于 75%75% 的数据,1 leq N leq 20001≤N≤2000。

对于 100%100% 的数据,1 leq N leq 2 times 10^51≤N≤2×105,0 leq a_i <2^{30}0≤ai​<230,1 leq C < 2^{30}1≤C<230。

2017/4/29 新添数据两组

分析:

1,对于这个题目我最开始想的是暴力求解,但是看了看数据范围,,,,,,太大了。

2,所以转换了思路,反正只要它相减的值的绝对值得C就行,所以我采用了桶排序。

3,由A-B=C知,A-C=C,转换思路知,我们可以求原数组中有多少个满足条件的B,因此我们先 每个数A的个数先统计出来,然后A-C,最后统计原数组中满足条件的B的个数,即原数组中有多少个A-C。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N], sum = 0;
map<int, int>mp;
int main() {
ll n, c;
cin >> n >> c;
for (int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
a[i] -= c;
}
for (int i = 0; i < n; i++)
sum += mp[a[i]];
cout << sum;
return 0;
}

总结:

1,其实很多算法都有一个最基本的模板,只要根据题目的意思解读题目,然后将条件带入模板就行。

2,很多题目一定要多方面思考,因为肯定有很多不同的路可以走通,我们寻找一条适合自己的、简单的路就行。

3,对于刷题,我的概念是将每一个题目弄明白,不求数量求质量,速度既然慢了下来,那题目就一定要能明白。

最后

以上就是娇气小鸽子为你收集整理的2023.1.10(总结)的全部内容,希望文章能够帮你解决2023.1.10(总结)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部