CF 699D 并查集

D. Fix a Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A tree is an undirected connected graph without cycles.

Let’s consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, …, pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).

For this rooted tree the array p is [2, 3, 3, 2].
Given a sequence p1, p2, …, pn, one is able to restore a tree:

There must be exactly one index r that pr = r. A vertex r is a root of the tree.
For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.
A sequence p1, p2, …, pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.

You are given a sequence a1, a2, …, an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.

Input
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ n).

Output
In the first line print the minimum number of elements to change, in order to get a valid sequence.

In the second line, print any valid sequence possible to get from (a1, a2, …, an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.

题意:用一个数组可以表示一棵树,其中Ai表示i的father,根节点的father是本身。但不是所有数组都表示一棵树,现在给你一个数组,用最少的修改次数将之改成一棵树,并输出修改次数和修改后的数组。

做法:用并查集,扫一遍,每次将自己和父亲节点合并。过程中,假如父亲节点的根节点是本身,表示出现了环,标记tag=1,假如父亲节点是本身,表示是根节点,标记tag=0;假如有非环根节点,那将所有其他独立根节点和环都连到这个根节点上,ans=cnt0+cnt1-1;假如全是环,将一个环上的点置为根节点,其他的环连到这个根节点上,ans=cnt1;

zz处:第一发提交WA在把一个fst==-1写成了fst=-1,改之,pp了。然而fst=-1有两处我只改了一处,所以真的fst了,QAQ。之后改了秒过,唉。以后要注意查到错时看看是否有相同的错,全部改了。
然后我很好的一次过4题的机会就这样GG辣,桑心,还掉了分。

代码:

#include <bits/stdc++.h>
using namespace std;
int rec[200500];
int u[200500];
int tag[200500];
bool vis[200500];
int n;
int find(int x)
{
    if(x==u[x])return x;
    else return u[x]=find(u[x]);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&rec[i]);
            u[i]=i;
            tag[i]=0;
        }
        for(int i=1;i<=n;i++)
        {   
            if(i==rec[i])continue;
            int tmp=find(rec[i]);
            if(tmp==i){
                tag[i]=1;
                rec[i]=i;
            }
            else{
                u[i]=tmp;
            }
        }
        int cnt0=0;
        int cnt1=0;
        int fst=-1;
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)   
        {
            int tmp=find(i);
            if(!vis[tmp]){
                vis[tmp]=true;
                if(fst==-1&&tag[tmp]==0)
                {
                    fst=tmp;
                    break;
                }
            }
        }
        memset(vis,false,sizeof(vis));
        int ans;
        if(fst==-1)
        {
            for(int i=1;i<=n;i++)   
            {
                int tmp=find(i);
                if(!vis[tmp]){
                    vis[tmp]=true;
                    if(fst==-1)
                    {
                        fst=tmp;
                        cnt1++;
                    }
                    else{
                        rec[tmp]=fst;
                        cnt1++;
                    }
                }
            }
            ans=cnt1;
        }
        else{
            for(int i=1;i<=n;i++)   
            {
                int tmp=find(i);
                if(!vis[tmp]){
                    vis[tmp]=true;
                    if(tag[tmp]==0)
                    {
                        cnt0++;
                        rec[tmp]=fst;
                    }
                    else {
                        cnt1++;
                        rec[tmp]=fst;
                    }
                }
            }
            ans=cnt0+cnt1-1;
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)
        {
            printf("%d%c",rec[i],i==n?'\n':' ');
        }
    }
    return 0;
}
当您在微信小程序中调用云函数并收到类似如下的响应信息时: ```json { "errMsg": "cloud.callFunction:ok", "result": null, "requestID": "b41f899d-cf64-4032-8bab-170464172e64" } ``` 这意味着您的云函数确实被成功调用了,并且回传了 `"cloud.callFunction:ok"` 的状态码,但是 `result` 字段却为空 (`null`) 或者没有任何数据。这种情况一般可能是由于以下几个原因导致的: ### 可能的原因及解决方案 #### 1. **云函数未返回有效值** 如果云函数内部没有显式地使用 `return` 返回任何内容给前端,那么默认情况下会接收到一个空的结果集。需要确保云函数有明确的数据输出。 **解决方案:** 修改云函数代码,使其能够返回预期的数据结构。例如: ```javascript exports.main = async (event, context) => { // 您的业务逻辑处理... return { message: 'Hello World', data: [] }; // 确保这里有一个合理的返回体 }; ``` #### 2. **异步操作未能完成即刻返回** 有时候,如果您在云函数内进行了某些异步任务(比如数据库查询、网络请求等),但在这些任务结束前就提前结束了函数执行流,也会造成同样的现象——因为此时还没有生成有效的返回值就被发送回去了。 **解决方案:** 使用合适的等待机制让程序等到所有的异步操作完成后才继续下一步。可以借助于 JavaScript Promise 对象或者 await 关键字来简化这一过程。示例: ```javascript const cloud = require('wx-server-sdk'); cloud.init(); async function getData() { let res; try { res = await db.collection('your_collection').get(); } catch (err) { console.error(err); // 错误日志记录以便调试 } if (!res || !Array.isArray(res.data)) { throw new Error('获取不到正确的数据'); } return res; // 正常情况应该在这里返回所需的数据 } exports.main = async (event, context) => { const result = await getData(); return result; }; ``` #### 3. **权限配置问题** 另外一种可能性是权限设置不当造成的访问限制,使得虽然 API 请求本身是可以到达服务器端点并且被执行了,但由于缺少必要的鉴权步骤或者其他安全策略的影响,最终还是无法获得期望之外的实际数据回应。 **解决方案:** 核对一下相关的认证凭证和服务授权范围是否准确无误。特别是涉及到敏感资源读取的地方更要注意这一点。 综上所述,针对以上三种常见的场景,请逐一排查对应的疑点区域直至找到根本所在为止。希望这些建议可以帮助到您解决问题! --
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值