1433D - Districts Connection - CodeForces Solution


constructive algorithms dfs and similar *1200

Please click on ads to support us..

Python Code:

I=lambda: map(int, input().split())
for _ in [0]*int(input()):
    n=int(input())
    a=list(I())
    c=list(set(a))
    if len(set(a))==1:
        print('NO')
    else:
        x=c[0]
        y=c[1]
        indx=a.index(x)+1
        indy=a.index(y)+1
        print('YES')
        print(indx,indy)
        i=1
        for v in a:
            if i!=indx and i!=indy:
                if v!=x:
                    print(indx,i)
                else:
                    print(indy,i)
            i=i+1

C++ Code:

#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
using LL = long long;
const int mod = 1e9+7;
const int M = 500000;
template<class T>
inline void out(T s )
{
	cout<<s<<endl;
}
inline void out(bool f)
{
	if(f)cout<<"YES\n";
	else cout<<"NO\n";
}
template<typename T>
T Max(T &&a,T &&b)
{
	return a>b?a:b;
}
template<typename T,typename... Args>
T Max(T &&a, Args... arg)
{
	T tmp= Max(arg...); 
	return Max(a,tmp);
}

int dis(int x,int y,int n,int m)
{
	return abs(x-n)+abs(y-m);
}
void fun(int x,int y, int p)
{
	
}
void ots(string s)
{
	bool flag = 1;
	for(int i=0;i<s.length();i++)
	{
		if(flag && s[i] == '0')continue;
		cout<<s[i];
		flag = 0;
	}
	if(flag)cout<<"0";
}
const LL INF  = 0x3f3f3f3f;
int pre[M];
int fin(int x)
{
	return x==pre[x]? x :pre[x] = fin(pre[x]);
}
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)pre[i] = i;
	vector<int>a(n+1);
	vector<pair<int,int>>ans;
	a[0] = INF;
	int num = 0;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<n;i++)
		for(int j=i+1;j<=n;j++)
		{
			if(num == n-1)break;
			if(a[i] != a[j])
			{
				int x =fin(i),y = fin(j);
				if(x!=y)
				{
					pre[x] = y;
					num++;
					ans.push_back({i,j});
				}
			}
		}
	out( num == n-1);
	if(num == n-1)
	for(auto c:ans)
	cout<<c.first<<" "<<c.second<<endl;
}
int main()
{
	int tt = 1;
	cin >> tt;
	while(tt--)
	solve();
	return 0;
}
	 	  		   				 	 	   					  	 	


Comments

Submit
0 Comments
More Questions

Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately