2023.2.12(总结)

今天主要就是下午进行了一个测试,有三个困难版的题目我没有写出来,打算今天晚上好好磨磨,这里主要就只放一个题目。

C - Don’t be cycle Editorial

 / 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

You are given a simple undirected graph with NN vertices and MM edges. The vertices are numbered 11 to NN, and the ii-th edge connects vertex A_iAi​ and vertex B_iBi​. Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.

What is a simple undirected graph?

What is a cycle?

Constraints

  • 1 \leq N \leq 2 \times 10^51≤N≤2×105
  • 0 \leq M \leq 2 \times 10^50≤M≤2×105
  • 1 \leq A_i, B_i \leq N1≤Ai​,Bi​≤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
A_1A1​ B_1B1​
A_2A2​ B_2B2​
\vdots⋮
A_MAM​ B_MBM​

Output

Print the answer.


Sample Input 1 Copy

Copy

6 7
1 2
1 3
2 3
4 2
6 5
4 6
4 5

Sample Output 1 Copy

Copy

2

One way to remove cycles from the graph is to delete the two edges between vertex 11 and vertex 22 and between vertex 44 and vertex 55.
There is no way to remove cycles from the graph by deleting one or fewer edges, so you should print 22.


Sample Input 2 Copy

Copy

4 2
1 2
3 4

Sample Output 2 Copy

Copy

0

Sample Input 3 Copy

Copy

5 3
1 2
1 3
2 3

Sample Output 3 Copy

Copy

1

分析:

1,一个无向图的题,去掉构成闭环的边,最少只用去掉几条。这就是题目的大意。

2,我刚开始看见这个题目我就知道肯定要使用到并查集,所以我就先把并查集写了出来,经过几番试验,发现:只要用输入的边的总数减去并查集的数组中不为0的数的个数,就是去掉的边的个数。

3,这就是说我本来有n条边,但是我只需要m条边,那么我去掉的边就是n-m,至于m怎么求,那就是使用并查集求。

代码如下:

#include
#include
#include

int pre[200005];

int find(int x)
{
	return pre[x]==0?x:(pre[x]=find(pre[x]));
}

void join(int x, int y) 
{
	int fx = find(x);
	int fy = find(y);
	if (fx!=fy){
		pre[fx] = fy;
	}
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i

 

总结:
打了几场cf的比赛之后,看见题目的思维发散了一些,不会再死死的盯着一个点死磕,考虑问题也周全了一些,看来真的是学多少都不如做多少。

你可能感兴趣的:(c语言,算法,图论)