1583B - Omkar and Heavenly Tree - CodeForces Solution


constructive algorithms trees *1200

Please click on ads to support us..

Python Code:

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        n, m = readIntArr()
        inmiddle = [0] * (n + 1)
        for __ in range(m):
            a, b, c = readIntArr()
            inmiddle[b] = 1
                central = -1
        for u in range(1, n + 1):
            if inmiddle[u] == 0:
                central = u
                break
        assert central != -1
        for v in range(1, n + 1):
            if u == v:
                continue
            allans.append((u, v))
    multiLineArrayOfArraysPrint(allans)
    
    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>
using namespace std;
#define fopen freopen("E:/in.txt", "r", stdin); 
#define ios                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);\
    cout.tie(0);
#define vint vector<int>
#define pii pair<int,int>
#define vnode vector<node>
#define lowbit(x) (x&-x)
#define ll long long 
#define ull unsigned ll


void solve() {
	int n, m;
	cin >> n >> m;
	vint vis(n + 1);
	for (int i = 1; i <= m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		vis[b] = 1;
	}
	int rt = 0;
	for (int i = 1; i <= n; i++)
		if (!vis[i]) rt = i;
	for (int i = 1; i <= n; i++)
		if (i != rt) cout << i << ' ' << rt << '\n';
}

signed main() {
	ios;
	//fopen;

	int t = 1;
	cin >> t;
	srand(time(0));
	while (t--)
		solve();

	return 0;
}


Comments

Submit
0 Comments
More Questions

1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection