Codeforces Round #677 D. Districts Connection

题目

题意:给出n个点,对n个点连n-1条边要求构成一棵树,其中对于树中属于同一帮派的点不能相互连边。则我们只需对所有的点进行连边尝试,直到连了n-1条边即可。遇到同一个帮派的点或已经连接的点直接跳过即可。(题目数据不大直接暴力就过了)

AC code

    #include<iostream>
    #include<string>
    #include<map>
    #include<algorithm>
    #include<memory.h>
    #include<cmath>
    #define pii pair<int,int>
    #define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    using namespace std;
    typedef long long ll;
    const int Max = 1e6 + 5;
    int lst[Max];
    int ls[Max][2];
     
    int main()
    {
   
    	FAST;
    	int t;cin >>t;
    	while (t--)
    	{
   
    		map<int, int> ma;//ma用来标记哪些点已经连接
    		int n;cin >> n;
    		for (int i = 1;i <= n;i++)cin >> lst[i];
    		int g = 0;
    		for (int i = 1;i <= n;i++)
    		{
   
    			for (int j = 1;j <= n;j++)
    			{
   
    				if (g == n - 1)break;//已连n-1条边
    				if (ma[i]&&ma[j])//两点已在
    				{
   
    					continue;
    				}
    				else
    				{
   
    					if (lst[i] == lst[j])continue;//相同帮派跳过
    					else
    					{
   
    						ma[i]++,ma[j]++;
    						ls[++g][0] = i, ls[g][1] = j;
    					}
    				}
    			}
    		}
    		if (g != n - 1)
    			cout << "NO" << endl;
    		else
    		{
   
    			cout << "YES" << endl;
    			for (int i = 1;i <= g;i++)cout << ls[i][0] << " " << ls[i][1] << endl;
    		}
    		
    	}
    }
全部评论

相关推荐

03-04 19:02
云南大学 Java
Yki_:没挂,只是没人捞,该干啥干啥,等着就好了
点赞 评论 收藏
分享
02-24 17:39
门头沟学院 Java
神哥不得了:神哥来啦~专业技能的话建议不要前面空那么多,八股的话建议先把高频top 50的八股多巩固几遍,千万不要看那些假高频八股。项目的话,建议换两个高质量的项目上去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务