• D. Districts Connection


    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

    1. #include
    2. # include
    3. using namespace std;
    4. typedef long long int ll;
    5. int a[5010];
    6. bool book[5010];
    7. int main ()
    8. {
    9. int t;
    10. cin>>t;
    11. while(t--)
    12. {
    13. int n;
    14. cin>>n;
    15. for(int i=1;i<=n;i++)
    16. {
    17. book[i]=0;
    18. }
    19. int root1=0,root2=0;
    20. int pos1,pos2;
    21. for(int i=1;i<=n;i++)
    22. {
    23. cin>>a[i];
    24. if(!root1)
    25. {
    26. root1=a[i];
    27. pos1=i;
    28. }
    29. else if(root1&&a[i]!=root1)
    30. {
    31. root2=a[i];
    32. pos2=i;
    33. }
    34. }
    35. if(root2==0)
    36. {
    37. cout<<"NO"<
    38. }
    39. else
    40. {
    41. cout<<"YES"<
    42. for(int i=1;i<=n;i++)
    43. {
    44. if(a[i]==root2)
    45. {
    46. cout<" "<
    47. book[i]=1;
    48. }
    49. }
    50. for(int i=1;i<=n;i++)
    51. {
    52. if(i!=pos1&&!book[i])
    53. {
    54. cout<" "<
    55. }
    56. }
    57. }
    58. }
    59. return 0;
    60. }

  • 相关阅读:
    2023NOIP A层联测10 子序列
    免安装版MySQL(解压版)安装详细教程及注意事项
    【基础】填涂颜色
    Debezium系列之:将Debezium的info日志、warn日志、error日志拆分到不同文件中
    自定义功能区
    22. 概率与统计 - 贝叶斯统计&机器学习分类指标
    实验六 移位寄存器的设计【Verilog】
    Kubernetes - 一键安装部署 K8S(附:Kubernetes Dashboard)
    Visual Studio部署C++矩阵库Armadillo的方法
    Pytorch模型model&data.to(device) | .cuda | .cpu()
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126222654