当前位置:网站首页>D. Districts Connection
D. Districts Connection
2022-08-08 16:37:00 【秦小咩】
D. Districts Connection
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn districts in the town, the ii-th district belongs to the aiai-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n−1n−1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n−1n−1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n−1n−1 roads to satisfy all the conditions.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤5001≤t≤500) — the number of test cases. Then tt test cases follow.
The first line of the test case contains one integer nn (2≤n≤50002≤n≤5000) — the number of districts. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the gang the ii-th district belongs to.
It is guaranteed that the sum of nn does not exceed 50005000 (∑n≤5000∑n≤5000).
Output
For each test case, print:
- NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
- YES on the first line and n−1n−1 roads on the next n−1n−1 lines. Each road should be presented as a pair of integers xixi and yiyi (1≤xi,yi≤n;xi≠yi1≤xi,yi≤n;xi≠yi), where xixi and yiyi are two districts the ii-th road connects.
For each road ii, the condition a[xi]≠a[yi]a[xi]≠a[yi] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
input
Copy
4 5 1 2 2 1 3 3 1 1 1 4 1 1000 101 1000 4 1 2 3 4
output
Copy
YES 1 3 3 5 5 4 1 2 NO YES 1 2 2 3 3 4 YES 1 2 1 3 1 4
=========================================================================
可以说CF上ABCD里面遇见的所谓图论树连边问题,都是抓住一个点进行连接。本题要求不能把相同的连接到一起。那么我们就选两个不同的点,a,b 把b全部连到a,再把剩下的b之外的全部连到b
#include<iostream>
# include<algorithm>
using namespace std;
typedef long long int ll;
int a[5010];
bool book[5010];
int main ()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
book[i]=0;
}
int root1=0,root2=0;
int pos1,pos2;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(!root1)
{
root1=a[i];
pos1=i;
}
else if(root1&&a[i]!=root1)
{
root2=a[i];
pos2=i;
}
}
if(root2==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
{
if(a[i]==root2)
{
cout<<pos1<<" "<<i<<endl;
book[i]=1;
}
}
for(int i=1;i<=n;i++)
{
if(i!=pos1&&!book[i])
{
cout<<pos2<<" "<<i<<endl;
}
}
}
}
return 0;
}
边栏推荐
猜你喜欢
10分钟快速入门RDS【华为云至简致远】
Patience sorting - specializing in quickly solving the longest increasing subarray
OpenAI怎么写作「谷歌小发猫写作」
七、jmeter发出请求的逻辑
jupyter notebook 隐藏&显示全部输出内容
[uniapp applet] view container cover-view
【LeetCode】试题总结:深度优先搜索 (DFS)
英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%
jupyter notebook hide & show all output
MySQL database
随机推荐
mysql进阶(二十九)常用函数汇总
IDEA2020安装教程
laravel-practice
最高法院关于婚姻案件诉讼程序的一些解答
4、S32K14X学习笔记:S32 Design Studio 新建和导入工程
jupyter notebook hide & show all output
json根据条件存入数据库
英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%
bzoj1269 [AHOI2006]文本编辑器editor
Grafana配置LDAP认证
股票开户中金公司好不好,安全吗
耐心排序——专门快速解决最长递增子数组
Charles MOCK 数据 htpps代理
Understanding of redis slice cluster
通过jenkins交付微服务到kubernetes
3 个开源项目,让你感受程序员的浪漫!
Lecture 207, Class Schedule
Beetl使用记录
元宇宙医疗或将改变医疗格局
Using PyGame's Bubble Sort Visualizer