2092 Joyful repair of high-speed rail

Pleasant Goat was caught by Big Big Wolf!

However, considering the thousands of episodes, Big Big Wolf decided to give Pleasant Goat a chance—to build a high-speed rail for Qingqing Grassland!

There are n tribes on the Qingqing Grassland, and Big Big Wolf asks to choose m to repair the high-speed rail for the tribe, and requires that the high-speed rail repaired should not be wasted. What is waste? If there is a high-speed rail between tribe A and B, and a high-speed rail between tribe B and C, but there is also a high-speed rail between tribe A and C, then one of these three high-speed rails is wasted, because there is more than one high-speed rail from A to C The way to arrive by high-speed rail.

Now Pleasant Goat has repaired m high-speed rail, now Pleasant Goat invites you to help him check the following, whether there is a waste of high-speed rail.

Please do this task well, otherwise we will not see the new cartoon of Pleasant Goat and Big Big Wolf, because Pleasant Goat will be eaten by Big Big Wolf.

Input
an integer case in the first line, indicating that there is an array of case groups.
For each set of data,
there are two integers n and m in the first line, indicating that there are n tribes on the Qingqing grassland. There are m pairs of tribes that have built high-speed rail
. Two integers x, y in each line, indicating that a high-speed
rail has been built between tribe x and tribe y. Output the
case line, and each line outputs a string "Yes" or "No", indicating whether there is waste of high-speed rail (if there is waste, output Yes, otherwise output No)
Data range
For 20% of the data: 1<=n,m<=100
For 50% of the data: 1<=n,m<=1000
For 100% of the data: 1<=n,m<=100000, 1<=cas<=10
Input example
Input example 1:
1
5 3
1 2
2 3
3 4
Input example 2:
1
5 5
1 2
2 3
3 4
4 5
5 1
Input example 3:
2
5 3
1 2
2 3
3 4
5 5
1 2
2 3
3 4
4 5
5 1
Output example
Output example 1:
No
Output example 2:
Yes
Output example 3:
No
Yes

The idea of ​​solving this problem is also very simple: we only need to connect every two points according to the line he gave, and mark him after the connection. If we find that these two points can be connected before connecting two points Then mark the wasted route. If the last marked variable does not change, it means that there is no repeated route. Output according to the corresponding requirements, then the code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long

using namespace std;

const int maxn = 0x3f3f3f;
int t,n,m,u,v;
int fa[maxn];//记录每个点的父亲是谁

int fund(int i){
    
    //查找每个节点归属于哪个节点
	if(fa[i] == i) 
		return i;
	else
		return fa[i] = fund(fa[i]);
}

void init(int n){
    
    //初始化
	for(ll i = 1;i <= n;i++)
		fa[i] = i;
}

void U(int n,int m){
    
    //将两个点连接到一起
	fa[fund(n)] = fund(fa[m]);
}

int main () {
    
    
	scanf("%d",&t);
	while(t--) {
    
    
		scanf("%d%d",&n,&m);
		init(n);
		bool f = 0;
		for(int i = 1;i <= m;i++){
    
    
			scanf("%d%d",&v,&u);
			if(fund(u)==fund(v))//如果这两个点之前已经连通那么就是存在浪费
				f = 1;
			U(u,v);
		}
		if(f)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_48627750/article/details/119737891