CF 427C. Checkposts Strongly connected components

本文介绍了一种使用Tarjan算法求解最强连通分支的方法,并通过一个具体实例展示了如何利用该算法来解决问题。文章首先定义了所需的数据结构和变量,接着详细解释了Tarjan算法的具体实现过程,包括如何寻找强连通分量、确定每个分量中的最小值及数量等步骤。

思路: 根据tarjan算法求最强连通分支,然后找出每个联通分支中的最小值,并统计其数量,乘法定理解决方法数。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;

#define MEM(a,b) memset(a, b, sizeof(a))
#define _FOR_I_(_offx, _offy, _offz) for(i=_offx; i<=_offy; i+=_offz)
#define INT64 __int64

const int N = 100100;
const int E = 300300;
const int INF = 0x7fffffff;
const int MODNUM = 1000000007;

struct Node{
    int m, num;
};

int n, val[N];
vector<int> v[N];

int scc_cnt, dfs_clock;
int DFN[N], cmp[N], low[N];
stack<int> s;

Node res[N];

void tarjan(int u){
    DFN[u] = low[u] = ++dfs_clock;
    s.push(u);

    for(int i = 0; i < v[u].size(); ++ i){
        if(!DFN[ v[u][i] ]) {
            tarjan(v[u][i]);
            low[u] = min(low[u], low[ v[u][i] ]);
        }
        else if(!cmp[ v[u][i] ]){
            low[u] = min(low[u], DFN[ v[u][i] ]);
        }
    }

    if(low[u] == DFN[u]){
        ++ scc_cnt;
        for(;;){
            int x = s.top();
            s.pop();
            cmp[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

void scc(){
    dfs_clock = scc_cnt = 0;
    MEM(DFN, 0);
    MEM(cmp, 0);

    int i;
    _FOR_I_(1, n, 1)
    {
        if(!DFN[i]) tarjan(i);
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
        scanf("%d", &val[i]);
    int m;
    scanf("%d", &m);

    while(m --)
    {
        int f, t;
        scanf("%d%d", &f, &t);
        v[f].push_back(t);
    }

    // tarjan method to find strongly connected components
    scc();

    int i;
    _FOR_I_(1, scc_cnt, 1)
        res[i].m = INF;

    // find minimum money for each strongly connected components
    _FOR_I_(1, n, 1){
        if(res[ cmp[i] ].m == val[i]) ++res[ cmp[i] ].num;
        if(res[ cmp[i] ].m > val[i]){
            res[ cmp[i] ].m = val[i];
            res[ cmp[i] ].num = 1;
        }
    }

    // count the value
    INT64 sum_val = 0, sum = 1;
    _FOR_I_(1, scc_cnt, 1){
        sum_val += res[i].m;
        sum = sum * res[i].num % MODNUM;
    }

    printf("%I64d %I64d\n", sum_val, sum);

    return 0;
}


root@hobot:/map/gus/Release-J6-20251117-71cf856a7/log/dlt# ../../MemoryMappingService [libprotobuf FATAL google/protobuf/generated_message_util.cc:811] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): terminate called after throwing an instance of 'google::protobuf::FatalException' what(): CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): Aborted (core dumped) root@hobot:/map/gus/Release-J6-20251117-71cf856a7/log/dlt# gdb ../../MemoryMappingService GNU gdb (Debian 13.1-3) 13.1 Copyright (C) 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "aarch64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ../../MemoryMappingService... (gdb) r Starting program: /map/gus/Release-J6-20251117-71cf856a7/MemoryMappingService [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/aarch64-linux-gnu/libthread_db.so.1". [New Thread 0xffffe91bcc40 (LWP 68227)] [New Thread 0xffffe51acc40 (LWP 68228)] [New Thread 0xffffdf19cc40 (LWP 68229)] [New Thread 0xffffda18cc40 (LWP 68230)] [New Thread 0xffffd617cc40 (LWP 68231)] [libprotobuf FATAL google/protobuf/generated_message_util.cc:811] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): terminate called after throwing an instance of 'google::protobuf::FatalException' what(): CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): Thread 1 "MemoryMappingSe" received signal SIGABRT, Aborted. __pthread_kill_implementation (threadid=281474593062944, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 44 ./nptl/pthread_kill.c: No such file or directory. (gdb) where #0 __pthread_kill_implementation (threadid=281474593062944, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 #1 0x0000ffffeece0a64 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78 #2 0x0000ffffeec9a76c in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x0000ffffeec874bc in __GI_abort () at ./stdlib/abort.c:79 #4 0x0000ffffeef949c8 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #5 0x0000ffffeef923bc in ?? () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #6 0x0000ffffeef92420 in std::terminate() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #7 0x0000ffffeef92704 in __cxa_throw () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #8 0x0000fffff2a14e6c in google::protobuf::internal::LogMessage::Finish() () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #9 0x0000fffff2a14ea8 in google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #10 0x0000fffff2a49bb0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #11 0x0000fffff56a50cc in phoenix::msg::routing::RoutingNotifyHMI::RoutingNotifyHMI(google::protobuf::Arena*) () from ../../ALGORITHM/lib/libcru_routing_j5.so #12 0x0000fffff069a8d0 in phoenix::msg::routing::RoutingNotifyHMI::RoutingNotifyHMI ( this=0xfffff5889d60 <phoenix::msg::routing::_RoutingNotifyHMI_default_instance_>) at /workspace/proto/msg_series_c/routing_notify.pb.h:627 #13 0x0000fffff0695a74 in InitDefaultsscc_info_RoutingNotifyHMI_routing_5fnotify_2eproto () at /workspace/proto/msg_series_c/routing_notify.pb.cc:75 #14 0x0000fffff2a4998c in google::protobuf::internal::(anonymous namespace)::InitSCC_DFS(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #15 0x0000fffff2a49bf0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #16 0x0000fffff718a654 in google::protobuf::internal::InitSCC(google::protobuf::internal::SCCInfoBase*) () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #17 0x0000fffff2b62420 in google::protobuf::(anonymous namespace)::AddDescriptorsImpl(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #18 0x0000fffff2b62570 in google::protobuf::internal::AddDescriptors(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #19 0x0000fffff069a588 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /workspace/proto/msg_series_c/routing_notify.pb.cc:197 #20 0x0000fffff069a5b8 in _GLOBAL__sub_I_routing_notify.pb.cc(void) () at /deps/include/google/protobuf/port_undef.inc:107 #21 0x0000fffff7fb431c in call_init (env=0xffffffffece8, argv=0xffffffffecd8, argc=1, l=<optimized out>) at ./elf/dl-init.c:90 #22 call_init (l=<optimized out>, argc=1, argv=0xffffffffecd8, env=0xffffffffece8) at ./elf/dl-init.c:27 #23 0x0000fffff7fb4424 in _dl_init (main_map=0xfffff7ff1380, argc=1, argv=0xffffffffecd8, env=0xffffffffece8) at ./elf/dl-init.c:137 #24 0x0000fffff7fca9b8 in _start () at ../sysdeps/aarch64/dl-start.S:46 (gdb) #0 __pthread_kill_implementation (threadid=281474593062944, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 #1 0x0000ffffeece0a64 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78 #2 0x0000ffffeec9a76c in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x0000ffffeec874bc in __GI_abort () at ./stdlib/abort.c:79 #4 0x0000ffffeef949c8 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #5 0x0000ffffeef923bc in ?? () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #6 0x0000ffffeef92420 in std::terminate() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #7 0x0000ffffeef92704 in __cxa_throw () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #8 0x0000fffff2a14e6c in google::protobuf::internal::LogMessage::Finish() () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #9 0x0000fffff2a14ea8 in google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #10 0x0000fffff2a49bb0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #11 0x0000fffff56a50cc in phoenix::msg::routing::RoutingNotifyHMI::RoutingNotifyHMI(google::protobuf::Arena*) () from ../../ALGORITHM/lib/libcru_routing_j5.so #12 0x0000fffff069a8d0 in phoenix::msg::routing::RoutingNotifyHMI::RoutingNotifyHMI ( this=0xfffff5889d60 <phoenix::msg::routing::_RoutingNotifyHMI_default_instance_>) at /workspace/proto/msg_series_c/routing_notify.pb.h:627 #13 0x0000fffff0695a74 in InitDefaultsscc_info_RoutingNotifyHMI_routing_5fnotify_2eproto () at /workspace/proto/msg_series_c/routing_notify.pb.cc:75 #14 0x0000fffff2a4998c in google::protobuf::internal::(anonymous namespace)::InitSCC_DFS(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #15 0x0000fffff2a49bf0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #16 0x0000fffff718a654 in google::protobuf::internal::InitSCC(google::protobuf::internal::SCCInfoBase*) () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #17 0x0000fffff2b62420 in google::protobuf::(anonymous namespace)::AddDescriptorsImpl(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #18 0x0000fffff2b62570 in google::protobuf::internal::AddDescriptors(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #19 0x0000fffff069a588 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /workspace/proto/msg_series_c/routing_notify.pb.cc:197 #20 0x0000fffff069a5b8 in _GLOBAL__sub_I_routing_notify.pb.cc(void) () at /deps/include/google/protobuf/port_undef.inc:107 #21 0x0000fffff7fb431c in call_init (env=0xffffffffece8, argv=0xffffffffecd8, argc=1, l=<optimized out>) at ./elf/dl-init.c:90 #22 call_init (l=<optimized out>, argc=1, argv=0xffffffffecd8, env=0xffffffffece8) at ./elf/dl-init.c:27 #23 0x0000fffff7fb4424 in _dl_init (main_map=0xfffff7ff1380, argc=1, argv=0xffffffffecd8, env=0xffffffffece8) at ./elf/dl-init.c:137 #24 0x0000fffff7fca9b8 in _start () at ../sysdeps/aarch64/dl-start.S:46 (gdb) q A debugging session is active. Inferior 1 [process 67966] will be killed.
11-18
(gdb) r Starting program: /map/gus/Release-J6-20251117-71cf856a7/MemoryCruiseService [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/aarch64-linux-gnu/libthread_db.so.1". [New Thread 0xffffe3eacc40 (LWP 72484)] [New Thread 0xffffdfe9cc40 (LWP 72485)] [New Thread 0xffffd9e8cc40 (LWP 72486)] [New Thread 0xffffd5e7cc40 (LWP 72487)] [New Thread 0xffffd0e6cc40 (LWP 72488)] [libprotobuf FATAL google/protobuf/generated_message_util.cc:811] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): terminate called after throwing an instance of 'google::protobuf::FatalException' what(): CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): Thread 1 "MemoryCruiseSer" received signal SIGABRT, Aborted. __pthread_kill_implementation (threadid=281474505965600, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 44 ./nptl/pthread_kill.c: No such file or directory. (gdb) where #0 __pthread_kill_implementation (threadid=281474505965600, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 #1 0x0000ffffe9a10a64 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78 #2 0x0000ffffe99ca76c in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x0000ffffe99b74bc in __GI_abort () at ./stdlib/abort.c:79 #4 0x0000ffffe9cc49c8 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #5 0x0000ffffe9cc23bc in ?? () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #6 0x0000ffffe9cc2420 in std::terminate() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #7 0x0000ffffe9cc2704 in __cxa_throw () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #8 0x0000ffffed6d4e6c in google::protobuf::internal::LogMessage::Finish() () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #9 0x0000ffffed6d4ea8 in google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #10 0x0000ffffed709bb0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #11 0x0000fffff718a654 in google::protobuf::internal::InitSCC(google::protobuf::internal::SCCInfoBase*) () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #12 0x0000fffff71a341c in apollo::common::StatusPb::SharedCtor() () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #13 0x0000fffff71a1ea8 in apollo::common::StatusPb::StatusPb(google::protobuf::Arena*) () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #14 0x0000fffff71a30d8 in apollo::common::StatusPb::StatusPb() () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #15 0x0000fffff11f1a90 in InitDefaultsscc_info_StatusPb_map_5fapollo_2fcommon_5fmsgs_2fbasic_5fmsgs_2ferror_5fcode_2eproto() () from ../../ALGORITHM/lib/libcru_planning_j5.so #16 0x0000ffffed70998c in google::protobuf::internal::(anonymous namespace)::InitSCC_DFS(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #17 0x0000ffffed7098e0 in google::protobuf::internal::(anonymous namespace)::InitSCC_DFS(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #18 0x0000ffffed7098e0 in google::protobuf::internal::(anonymous namespace)::InitSCC_DFS(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #19 0x0000ffffed709bf0 in google::protobuf::internal::InitSCCImpl(google::protobuf::internal::SCCInfoBase*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #20 0x0000fffff718a654 in google::protobuf::internal::InitSCC(google::protobuf::internal::SCCInfoBase*) () from ../../ALGORITHM/lib/libcruiseplanning_j5.so #21 0x0000ffffed822420 in google::protobuf::(anonymous namespace)::AddDescriptorsImpl(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #22 0x0000ffffed822570 in google::protobuf::internal::AddDescriptors(google::protobuf::internal::DescriptorTable const*) () from /map/kotei/OSS/lib/j5/protobuf/libprotobuf.so.24 #23 0x0000ffffeb3809d8 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /workspace/proto/msg_series_c/map_msg_apollo.pb.cc:100 #24 0x0000ffffeb380a08 in _GLOBAL__sub_I_map_msg_apollo.pb.cc(void) () at /deps/include/google/protobuf/port_undef.inc:107 #25 0x0000fffff7fb431c in call_init (env=0xffffffffecf8, argv=0xffffffffece8, argc=1, l=<optimized out>) at ./elf/dl-init.c:90 #26 call_init (l=<optimized out>, argc=1, argv=0xffffffffece8, env=0xffffffffecf8) at ./elf/dl-init.c:27 #27 0x0000fffff7fb4424 in _dl_init (main_map=0xfffff7ff1380, argc=1, argv=0xffffffffece8, env=0xffffffffecf8) at ./elf/dl-init.c:137 #28 0x0000fffff7fca9b8 in _start () at ../sysdeps/aarch64/dl-start.S:46 (gdb) q A debugging session is active. Inferior 1 [process 72233] will be killed.
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值