【Algorithm】GPLT L2-038 病毒溯源

该篇博客介绍了一种通过BFS寻找病毒源头及其变异路径的方法。利用入度为零的病毒作为起点,对病毒编号进行排序,然后遍历所有病毒,找出最少的变异次数和对应的病毒序列。最终,通过回溯pre数组重构完整的变异链。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

L2-038 病毒溯源 

v[ i ] 中保存第 i 种病毒可能产生的变异病毒编号,in[ i ] 中保存编号 i 的入度,入度为零的病毒为源头病毒,pre[ i ] 中保存病毒由哪种病毒变异而来(上级病毒)。为了保证序列最小,在输入 v[ i ]后可对其进行一次排序,然后通过 BFS 找到变异次数,过程中由 res 和 idx 标记最多变异次数量以及对应的病毒编号,最后通过pre找回完整变异链。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e4 + 10;

int n, in[N], pre[N];
int s; // 源头
vector<int> v[N], ans;
queue<PII> q;

int main()
{
    scanf("%d", &n);
    
    for (int i = 0; i < n; i++) {
        int k;
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            int x;
            scanf("%d", &x);
            in[x] ++;
            pre[x] = i;
            v[i].push_back(x);
        }
        sort(v[i].begin(), v[i].end());
    }
    
    for (int i = 0; i < n; i++) if (!in[i]) s = i;
    
    int res = 0, idx;
    q.push({s, 1});
    while (!q.empty()) {
        auto t = q.front();
        int a = t.first, b = t.second;
        q.pop();
        
        if (b > res) {
            res = b;
            idx = a;
        }
        
        for (auto it : v[a]) q.push({it, b + 1});
    }
    
    printf("%d\n", res);
    
    while (idx != s) {
        ans.push_back(idx);
        idx = pre[idx];
    }
    ans.push_back(s);
    
    for (int i = ans.size() - 1; i > 0; i--)
        printf("%d ", ans[i]);
    printf("%d\n", ans[0]);
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值