当前位置:网站首页>2022.8.8考试区域链接(district)题解
2022.8.8考试区域链接(district)题解
2022-08-10 01:55:00 【bj_hacker】
题目
4、区域链接(district)–1200
时间限制: | 空间限制:
题目描述:
有 个点,第 个点的颜色为 。
你要添加 条双向边使所有点连通。一条边的两个端点颜色不能相同。
给出任意一种符合条件的构造方案,或者判断不存在符合条件的方案。共 组数据。
输入格式:
第一行仅有一个正整数 ( ),表示测试数据的组数。
每组测试数据包括两行:
第一行一个正整数 ( ,所有数据中 之和不超过 ),表示点的个数;
第二行 个正整数 ( ),表示每个点的颜色。
输出格式:
对于每组测试数据,若不存在符合条件的方案输出 ;
否则,第一行输出 ,接下来的 行输出你的构造方案中每条边的端点。
思路
将n个点能连出去的所有合法未访问过的点记录将剩下的点继续遍历
重点
时间复杂度仅支持O(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;
}
边栏推荐
- mysql -sql编程
- FILE结构体在stdio.h头文件源码里的详细代码
- ImportError: Unable to import required dependencies: numpy
- .Net面试经验总结
- 数组(一)
- Unity reports Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Pla
- 高压之下,必有懦夫
- 已备案域名用国外服务器会不会掉备案?
- [论文阅读] Diverse Image-to-Image Translation via Disentangled Representations
- Database management tool: dynamic read-write separation
猜你喜欢

自动化测试中,测试数据与脚本分离以及参数化方法

Unity image is blurry after using long image

基于FTP协议实现文件上传与下载
![[网鼎杯 2020 青龙组]AreUSerialz](/img/33/a237185ffe0c5780432c242c36cbdc.png)
[网鼎杯 2020 青龙组]AreUSerialz

小程序开发的报价为什么有差别?需要多少钱?

web crawler error

Janus actual production case

中文NER的SOTA:RICON

In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?

【干货】集成学习原理总结
随机推荐
[论文阅读] Diverse Image-to-Image Translation via Disentangled Representations
阿里云OSS文件上传
首次在我们的centos上安装MySQL
Janus actual production case
控制台中查看莫格命令的详细信息
ECCV 2022 Oral | CCPL: 一种通用的关联性保留损失函数实现通用风格迁移
浅写一个下拉刷新组件
OpenCV图像处理学习一,加载显示修改保存图像相关函数
UXDB现在支持函数索引吗?
空间复杂度为O(1)的归并排序
数据库治理利器:动态读写分离
别再用 offset 和 limit 分页了,性能太差!
微透镜阵列后光传播的研究
[网鼎杯 2020 青龙组]AreUSerialz
odoo公用变量或数组的使用
2022年8月8日-2022年8月15日,ue4视频教程+插件源码()
16. 最接近的三数之和
LeetCode每日两题02:两数之和 II - 输入有序数组 (均1200道)
web crawler error
Deep Learning (5) CNN Convolutional Neural Network