D. Districts Connection(思维)Codeforces Round #677 (Div. 3)

原题链接: https://codeforces.com/contest/1433/problem/D

在这里插入图片描述
测试样例

input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4

题意: n n n个区,每个区都属于一个帮,现在你需要用 n − 1 n-1 n1条道路连连接这些区,要求所连的直接区不能属于一个帮,可以使用中间点来连接。

解题思路: 哇哇哇,这道题一言难尽,想到了就真的秒过的那种。OK,我们来看看吧,解决此题的关键就是要知道一个区可以连接多个区,那么我们发现只要有一个区和其他的是不同帮的,那么就可以实现连接。 因为用这个区连接所有的不同帮的区,再用不同帮的区连接和我之前选择参考的区的帮的区。(好绕呀,禁止套娃)哈哈,仔细理解。那么这样我们解决这道问题确实很简单,具体看代码体会。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int a[maxn];
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n;
			rep(i,1,n){
    
    
				cin>>a[i];
			}
			bool flag=false;
			rep(i,1,n-1){
    
    
				if(a[i]!=a[i+1]){
    
    
					flag=true;
					break;
				}
			}
			if(!flag){
    
    
				cout<<"NO"<<endl;
			}
			else{
    
    
				cout<<"YES"<<endl;
				//以1为中间点,连接所有与这个不同区的点。
				int temp;//temp用来记录不同点来连接与1相同的点。
				rep(i,2,n){
    
    
					if(a[i]!=a[1]){
    
    
						temp=i;
						cout<<i<<" "<<1<<endl;
					}
				}
				rep(i,2,n){
    
    
					if(a[i]==a[1]){
    
    
						cout<<temp<<" "<<i<<endl;
					}
				}
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109193239
今日推荐