QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#506395#6421. Degree of Spanning TreezmrzmrRE 1ms5660kbC++201.4kb2024-08-05 17:09:072024-08-05 17:09:07

Judging History

你现在查看的是最新测评结果

  • [2024-08-05 17:09:07]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5660kb
  • [2024-08-05 17:09:07]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 5010;
const int M = 2e5+10;
int n,m;
int ans = 0 ;
int cnt = 0;
int cnt_tree = 0 ;
int inde[M];
int flag = 0;
struct edge{
	int x,y;
//	int w;
}a[M];
struct edge_tree{
	int x,y;
//	int w;
}e[M];
/*
bool cmp(edge a,edge b)		//排序 
{
	return a.w<b.w;
}
*/
int f[M];
int find(int x)		//并查集 
{
	if(f[x]==x) return x;
	return f[x] = find(f[x]);
}
void kru()
{
//	sort(a+1,a+m+1,cmp);
	for(int i = 1; i <= m ; i++)
	{
		int eu = find(a[i].x);
		int ev = find(a[i].y);
		if(eu == ev) continue;
		e[++cnt_tree].x = a[i].x;
		e[cnt_tree].y = a[i].y;
		f[ev] = eu;
		if(++cnt==n-1)
		{
			break;
		}
	}
}
void slove()
{
		cin>>n>>m;
	for(int i = 1 ; i <= m ; i++ )
	{
		int x,y;//,z;
		cin>>x>>y;//>>z;
		a[i].x=x;
		a[i].y=y;   
	//	a[i].w=z;      
	}
	for(int i = 1 ; i <= n ; i++ )		//并查集初始化 
	{
		f[i] = i ;   
	}
	kru();
	for(int i = 1 ; i <= cnt_tree ; i++ )
	{
		inde[e[i].x]++;
		inde[e[i].y]++;
//		cout<<e[i].x<<" "<<e[i].y<<endl;
	}
	for(int i = 1 ; i <= cnt_tree ; i++ )
	{
		if( inde[i] > n/2 )
		{
			flag = 1 ;
		}
	}
	
	if(flag == 0 )
	{
		cout<<"Yes"<<endl;
		for(int i = 1 ; i <= cnt_tree ; i++ )
			cout<<e[i].x<<" "<<e[i].y<<endl;
		return ;
	}
	cout<<"No"<<endl;
}
int main()
{

	int tcase;
	cin>>tcase;
	while(tcase--)
	{
		slove();
	}
	return 0;
	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5660kb

input:

2
6 9
1 2
1 3
1 4
2 3
2 4
3 4
4 5
4 6
4 6
3 4
1 3
2 3
3 3
1 2

output:

Yes
1 2
1 3
1 4
4 5
4 6
No

result:

ok 2 cases

Test #2:

score: -100
Runtime Error

input:

11140
10 15
9 6
5 7
6 5
2 3
7 5
7 5
3 10
9 7
5 5
9 1
7 5
2 8
7 5
4 3
6 2
9 19
3 7
3 9
2 8
2 8
3 6
5 1
1 8
8 9
8 3
4 8
5 5
3 1
4 3
1 3
8 6
1 3
7 4
4 3
8 8
12 20
10 2
5 5
2 4
3 3
3 3
5 11
9 2
5 5
7 12
11 3
3 3
3 5
5 3
3 1
4 6
7 11
6 8
4 5
6 12
6 5
8 18
4 2
4 3
2 4
2 4
4 3
4 8
2 2
6 7
2 4
6 2
1 4
8 7
4...

output:

Yes
9 6
5 7
6 5
2 3
3 10
9 1
2 8
4 3
6 2
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
N...

result: