Codeforces Round #749 A Windblume 、B. Omkar and Heavenly Tree题解 (Div. 1 + Div. 2)

A. Windblume Ode

You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of n (n≥3) positive distinct integers (i.e. different, no duplicates are allowed).

Find the largest subset (i.e. having the maximum number of elements) of this array such that its sum is a composite number. A positive integer x is called composite if there exists a positive integer y such that 1<y<x and x is divisible by y.

If there are multiple subsets with this largest size with the composite sum, you can output any of them. It can be proven that under the constraints of the problem such a non-empty subset always exists.

Input
Each test consists of multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.

The first line of each test case contains an integer n (3≤n≤100) — the length of the array.

The second line of each test case contains n distinct integers a1,a2,…,an (1≤ai≤200) — the elements of the array.

Output
Each test case should have two lines of output.

The first line should contain a single integer x: the size of the largest subset with composite sum. The next line should contain x space separated integers representing the indices of the subset of the initial array.

题意分析
此题给一个集合,需要求最大子集,并且这个子集每个数加起来的和需要是一个合数,合数就是除了质数以外的数,题目中数字都大于0。
题解思路
因为集合的个数 n >= 3,所以我们考虑,如果集合的和刚开始就是一个合数,那么我们直接输出这个集合就可以,如果和为质数,那么我们考虑只需要减去集合中的一个数就可以让集合的和为合数。为什么呢?
证明:已知一个自然数不是偶数就是奇数,质数一定是一个奇数,那么假设集合中元素有奇数和偶数,奇数 + 偶数 = 奇数,奇数 + 奇数 + 奇数 = 奇数,不论什么情况,只要去掉一个奇数,这个集合的和就变成了偶数,那么就是一个合数了。所以我们遍历一下集合,找到去除的那个数就可以了。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 205;
int a[N];
//判断是否是质数,因为这个题n很小,所以我没用筛法筛素数
bool isprime(int n)
{
    for(int i=2; i<=n/i; ++i){
        if(n%i==0){
            return false;
        }
    }
    return true;
}
int main()
{
    int n;
    int t;
    cin>>t;

    while(t--){
        cin>>n;
        int sum = 0;
        for(int i=0; i<n; ++i){
            cin>>a[i];
            sum += a[i];
        }

        int tmp = 0,pos = -1;
        for(int i=0; i<n; ++i){
        //如果不是质数就是合数
            if(isprime(sum)==false){
                break;
            }
            //如果去除这个数是合数,就记录一下这个数的坐标
            if(isprime(sum-a[i])==false){
                sum -= a[i];
                tmp = 1;
                pos = i;
            }
        }
        if(tmp == 1){
            cout<<n - 1<<endl;
            for(int i=0; i<n; ++i){
                if(pos==i){
                    continue;
                }
                cout<<i+1<<' ';
            }
        }
        else{
             cout<<n<<endl;
            for(int i=0; i<n; ++i){
                cout<<i+1<<' ';
            }
        }
        cout<<endl;
    }
    return 0;
}

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

A tree with n nodes is an connected undirected graph with n nodes and n−1 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:

A restriction consists of 3 pairwise distinct integers, a, b, and c (1≤a,b,c≤n). It signifies that node b cannot lie on the simple path between node a and node c.

Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

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

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

It is guaranteed that the sum of n across all test cases will not exceed 105.

Output
For each test case, output n−1 lines representing the n−1 edges in the tree. On each line, output two integers u and v (1≤u,v≤n, u≠v) signifying that there is an edge between nodes u and v. Given edges have to form a tree that satisfies Omkar’s restrictions.
题意分析
题目给出一些限制条件,要求满足限制条件的情况下,输出一个满足条件的数
限制条件 a , b , c 要求,b不能在a->c的路径上,而且要求树中每个点两两之间有满足限制条件可达的路径,并且不能一个点重复走。
题解思路
有个取巧的方法,找到一个不是 给定限制条件中是 b 的一个节点,把他当作根节点,
然后这样构造树就行了
在这里插入图片描述
如图,任意两个点之间经过根节点到达另一个点,不会重复,不会因为限制条件出错,根节点因为不是 b[i] 数组中的一个值,所以可以是a[i] -> c[i] 路径上的一个点
此题得解。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

bool vis[100005];
int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(vis,0,sizeof(vis));
        int n,m;
        cin>>n>>m;
        for(int i=0; i<m; ++i){
            int a,b,c;
            cin>>a>>b>>c;
            vis[b] = true;
        }
        int t;
        for(int i=1; i<=n; ++i){
            if(!vis[i]){
                t = i;
                break;
            }
        }
        for(int i=1; i<=n; ++i){
            if(i==t) continue;
            else{
                cout<<t<<' '<<i<<endl;
            }
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值