当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
SQLserver加个判断
官宣出自己的博客啦
2022年8月8日-2022年8月15日,ue4视频教程+插件源码()
【二叉树-中等】1104. 二叉树寻路
【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
one of the variables needed for gradient computation has been modified by an inplace
Redis - Basic operations and usage scenarios of String|Hash|List|Set|Zset data types
《GB39732-2020》PDF下载
[Swoole Series 3.5] Process Pool and Process Manager
openpose脚部标注问题梳理
C# winform 单选框
FusionCompute产品介绍
781. 森林中的兔子
谷歌翻译器-谷歌翻译器软件批量自动翻译
2022.8.9 Remainder of Exam Balance--1000 Question Solutions
【二叉树-中等】1261. 在受污染的二叉树中查找元素
MySQL:日志系统介绍 | 错误日志 | 查询日志 | 二进制日志:bin-log数据恢复实践 | 慢日志查询
Deep Learning (5) CNN Convolutional Neural Network
Redis - String|Hash|List|Set|Zset数据类型的基本操作和使用场景
sqlmap dolog外带数据