当前位置:网站首页>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;
}
边栏推荐
- Redis - Basic operations and usage scenarios of String|Hash|List|Set|Zset data types
- LeetCode 每日一题——1413. 逐步求和得到正数的最小值
- Algorithm and voice dialogue direction interview question bank
- 如何让数据库中的数据同步
- 【QT】QT项目:自制Wireshark
- openpose脚部标注问题梳理
- 【机器学习】随机森林、AdaBoost、GBDT、XGBoost从零开始理解
- 剑指offer专项突击版第25天
- 翻译软件免费版下载-免费版翻译软件下载
- RESOURCE_EXHAUSTED: etcdserver: mvcc: database space exceeded
猜你喜欢
随机推荐
如何让数据库中的数据同步
2022杭电多校联赛第七场 题解
C# 正则表达式分组查询
【Kali安全渗透测试实践教程】第7章 权限提升
【二叉树-中等】1261. 在受污染的二叉树中查找元素
官宣出自己的博客啦
手把手教你搭建ELK-新手必看-第一章:什么是ELK?
状态压缩小经验
官宣出自己的博客了
小程序开发的报价为什么有差别?需要多少钱?
数据库治理利器:动态读写分离
在蓝图中给组件动态加子Actor组件
OpenCV图像处理学习二,图像掩膜处理
进程管理和任务管理
[网鼎杯 2020 青龙组]AreUSerialz
Janus actual production case
[Syntax sugar] About the mapping of category strings to category numeric ids
算法与语音对话方向面试题库
【Kali安全渗透测试实践教程】第9章 无线网络渗透
2022.8.8考试摄像师老马(photographer)题解