当前位置:网站首页>2022.8.8 Exam area link (district) questions
2022.8.8 Exam area link (district) questions
2022-08-10 03:20:00 【bj_hacker】
题目
4、regional link(district)–1200
时间限制: | 空间限制:
题目描述:
有 个点,第 The color of a dot is .
你要添加 A bidirectional edge connects all points.The two endpoints of an edge cannot have the same color.
Give any constructive scheme that meets the conditions,Or judge that there is no eligible scheme.共 组数据.
输入格式:
第一行仅有一个正整数 ( ),表示测试数据的组数.
每组测试数据包括两行:
第一行一个正整数 ( ,所有数据中 之和不超过 ),表示点的个数;
第二行 个正整数 ( ),表示每个点的颜色.
输出格式:
对于每组测试数据,If there is no matching scheme output ;
否则,第一行输出 ,接下来的 The line outputs the endpoints of each edge in your construction scheme.
思路
将nThe records of all legal unvisited points that can be connected to one point will continue to traverse the remaining points
重点
Time complexity is only supportedO(n^2),合理控制
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+10;
int t,n;
int a[maxn];
int mp[maxn][2];
bool vis[maxn];
inline bool Prim(){
memset(vis,false,sizeof(vis));
memset(mp,0,sizeof(mp));
vis[1]=true;
int cnt=0;
for(int i=1;i<=n;i++){
if(!vis[i])continue;
for(int j=1;j<=n;j++){
if(vis[j])continue;
if(a[i]==a[j])continue;
mp[++cnt][0]=i;
mp[cnt][1]=j;
vis[j]=true;
}
}
bool flag=true;
for(int i=1;i<=n;i++){
if(!vis[i]){
flag=false;
break;
}
}
if(flag)return true;
return false;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
if(Prim()){
printf("YES\n");
for(int i=1;i<=n-1;i++)printf("%d %d\n",mp[i][0],mp[i][1]);
}
else printf("NO\n");
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
FusionConpute虚拟机的发放与管理
自动化测试中,测试数据与脚本分离以及参数化方法
UXDB现在支持函数索引吗?
one of the variables needed for gradient computation has been modified by an inplace
网络爬虫错误
Janus actual production case
FusionCompute产品介绍
Open3D 中点细分(网格细分)
C# winform 单选框
openpose脚部标注问题梳理
Nacos源码分析专题(五)-Nacos小结
Janus实际生产案例
QT模态对话框及非模态对话框学习
Difference Between Data Mining and Data Warehousing
OpenCV图像处理学习二,图像掩膜处理
Shell编程--awk
深度学习(五) CNN卷积神经网络
微生物是如何影响身体健康的
微透镜阵列后光传播的研究
免费文档翻译软件电脑版软件









