题目
题解思路
将1号与第一个帮派不同的全部连起来,再将与1号相同的连接再任意1号已经连接的区域。这样出来的还是N-1条边,总以为不是,不敢往这方向想。
这样建成了一棵树就满足题目要求了。
思维思维。
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
struct bian
{
double w;
int z;
bool operator < (const bian &other) const
{
return w > other.w;
}
};
int read() {
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
int w[5010];
int main ()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n, book = -1;
cin>>n;
for (int i = 1; i <= n ; i++ )
{
int t;
cin>>t;
w[i] = t;
}
vector <pair <int ,int > > ans ;
for (int k = 2 ; k <= n ; k++ )
{
if ( w[1]!=w[k])
{
book = k;
ans.push_back({1,k});
}
}
if (book == -1 )
{
cout<<"NO\n";
continue;
}
for (int k = 2 ; k <= n ; k++ )
{
if(w[k] == w[1])
{
ans.push_back({book , k });
}
}
cout<<"YES\n";
for (int i = 0 ; i < ans.size() ; i++ )
{
cout<<ans[i].first<<" "<<ans[i].second<<"\n";
}
}
return 0 ;
}