CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-B. Omkar and Heavenly Tree-题解

本文主要是介绍CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-B. Omkar and Heavenly Tree-题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

    • Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - B. Omkar and Heavenly Tree
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Onput
      • Note
  • 题目大意
  • 解题思路
  • AC代码

Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - B. Omkar and Heavenly Tree

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

Lord Omkar would like to have a tree with n n n nodes ( 3 ≤ n ≤ 1 0 5 ) (3≤n≤10^5) (3n105) and has asked his disciples to construct the tree. However, Lord Omkar has created m ( 1 ≤ m < n ) m (1≤m<n) m(1m<n) restrictions to ensure that the tree will be as heavenly as possible.

A tree with n n n nodes is an connected undirected graph with n n n nodes and n − 1 n−1 n1 edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once.

Here is an example of a tree:

树

Input

Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104). Description of the test cases follows.

The first line of each test case contains two integers, n n n and m ( 3 ≤ n ≤ 1 0 5 , 1 ≤ m < n ) m (3≤n≤10^5, 1≤m<n) m(3n105,1m<n), representing the size of the tree and the number of restrictions.

The i-th of the next m lines contains three integers a i , b i , c i a_i, b_i, c_i ai,bi,ci ( 1 ≤ a i , b i , c i ≤ n , a , b , c 1≤a_i,b_i,c_i≤n, a, b, c 1ai,bi,cin,a,b,c are distinct), signifying that node b i b_i bi cannot lie on the simple path between nodes a i a_i ai and c i c_i ci.

It is guaranteed that the sum of n n n across all test cases will not exceed 1 0 5 10^5 105.

Output

For each test case, output n − 1 n−1 n1 lines representing the n − 1 n−1 n1 edges in the tree. On each line, output two integers u u u and v ( 1 ≤ u , v ≤ n , u ≠ v ) v (1≤u,v≤n, u≠v) v(1u,vn,u=v) signifying that there is an edge between nodes u u u and v v v. Given edges have to form a tree that satisfies Omkar’s restrictions.

Sample Input

2
7 4
1 2 3
3 4 5
5 6 7
6 5 4
5 3
1 2 3
2 3 4
3 4 5

Sample Onput

1 2
1 3
3 5
3 4
2 7
7 6
5 1
1 3
3 2
2 4

Note

The output of the first sample case corresponds to the following tree:

样例1

For the first restriction, the simple path between 1 1 1 and 3 3 3 is 1 1 1, 3 3 3, which doesn’t contain 2 2 2. The simple path between 3 3 3 and 5 5 5 is 3 , 5 3, 5 3,5, which doesn’t contain 4 4 4. The simple path between 5 5 5 and 7 7 7 is 5 , 3 , 1 , 2 , 7 5,3,1,2,7 5,3,1,2,7, which doesn’t contain 6 6 6. The simple path between 6 6 6 and 4 4 4 is 6 , 7 , 2 , 1 , 3 , 4 6,7,2,1,3,4 6,7,2,1,3,4, which doesn’t contain 5 5 5. Thus, this tree meets all of the restrictions.

The output of the second sample case corresponds to the following tree:

在这里插入图片描述


题目大意

给你 n n n个节点的树和最多 n − 1 n-1 n1个约束,其中每条约数包括三个数 a , b , c a,b,c a,b,c,需要满足 b b b不在 a , c a,c a,c的最短路径上。

让你找到一种满足上述条件的树

解题思路

说白了就是 b b b 不在 a a a c c c 的中间。

既然给你了 n − 1 n-1 n1 个约束条件,那么必有一点没当过 b b b,也就是说这个点可以在任何两点之间。

于是我们就可以以这个点为根,其他每个节点都连接到这个节点上面去。

也就是说假如 点 A 点A A 没有被限制在另外两点之间,那么就以 A A A 为中心,其他点都连接到这个点上。

在这里插入图片描述


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
bool visited[100010]; // visited[i]代表点i是否当过b
int main()
{int N;cin>>N;while(N--) {int n, m;cd(n), cd(m); // scanf n, mfor (int i = 1; i <= n; i++) {visited[i] = false; // 初始值这n个点都没当过b}for (int i = 0; i < m; i++) {int a,b,c;scanf("%d%d%d",&a,&b,&c);visited[b] = true; // b这个点当过b了}int notV = 0; // 没有当过b的点for (int i = 1; i <= n; i++) {if (!visited[i]) { // 找到了notV = i; // 赋值break; // 退出}}for (int i = 1; i <= n; i++) {if (i != notV) { // 除了这个点自己以外,所有点都连接到这个点上printf("%d %d\n", notV, i);}}}return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/120835589

这篇关于CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-B. Omkar and Heavenly Tree-题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/893884

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?