uva 11504 - Dominos (scc)

该博客介绍了UVA 11504编程题——Dominos,主要内容是如何通过求解强连通分量(scc)和拓扑排序来确定最少需要手动推倒的多米诺骨牌数量。

Problem D: Dominos

Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line. However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again.

Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.

Input Specification

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing two integers, each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n. Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well.

Sample Input

1
3 2
1 2
2 3

Output Specification

For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.

Output for Sample Input

1

Ondřej Lhoták

先求scc,然后按照拓扑序dfs。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 100000 + 5;
const int INF = 1000000000;

int V;
vector<int> G[maxn];
vector<int> rG[maxn];
vector<int> vs;
bool used[maxn];
int cmp[maxn];

void add_edge(int from, int to){
    G[from].push_back(to);
    rG[to].push_back(from);
}

void dfs(int v){
    used[v] = true;
    for(int i = 0;i < G[v].size();i++){
        if(!used[G[v][i]]) dfs(G[v][i]);
    }
    vs.push_back(v);
}

void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for(int i = 0;i < rG[v].size();i++){
        if(!used[rG[v][i]]) rdfs(rG[v][i], k);
    }
}

int scc(){
    memset(used, 0, sizeof(used));
    vs.clear();
    for(int i = 0;i < V;i++){
        if(!used[i]) dfs(i);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for(int i = vs.size()-1;i >= 0;i--){
        if(!used[vs[i]]) rdfs(vs[i], k++);
    }
    return k;
}

P p[maxn];

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        int m;
        scanf("%d%d", &V, &m);
        for(int i = 0;i < maxn;i++){
            G[i].clear();
            rG[i].clear();
        }
        while(m--){
            int x, y;
            scanf("%d%d", &x, &y);
            x--;y--;
            add_edge(x, y);
        }

        int k = scc();

        for(int i = 0;i < V;i++){
            p[i] = P(cmp[i], i);
        }
        sort(p, p+V);
        memset(used, 0, sizeof(used));
        int ans = 0;
        for(int i = 0;i < V;i++){
            int u = p[i].second;
            if(!used[u]){
                dfs(u);
                ans++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


2025-10-24 21:51:32,325 INFO akka.event.slf4j.Slf4jLogger [] - Slf4jLogger started 2025-10-24 21:51:32,351 INFO akka.remote.RemoteActorRefProvider [] - Akka Cluster not in use - enabling unsafe features anyway because `akka.remote.use-unsafe-remote-features-outside-cluster` has been enabled. 2025-10-24 21:51:32,352 INFO akka.remote.Remoting [] - Starting remoting 2025-10-24 21:51:32,473 INFO akka.remote.Remoting [] - Remoting started; listening on addresses :[akka.tcp://flink@dc3-dominos-usdp-fun01:41361] 2025-10-24 21:51:32,626 INFO org.apache.flink.runtime.rpc.akka.AkkaRpcServiceUtils [] - Actor system started at akka.tcp://flink@dc3-dominos-usdp-fun01:41361 2025-10-24 21:51:32,656 INFO org.apache.flink.runtime.blob.BlobServer [] - Created BLOB server storage directory /data/tmp/usercache/root/appcache/application_1755681731461_9029/jm_7d81ae143bee2871308112b19784294f/blobStorage 2025-10-24 21:51:32,659 INFO org.apache.flink.runtime.blob.BlobServer [] - Started BLOB server at 0.0.0.0:39765 - max concurrent requests: 50 - max backlog: 1000 2025-10-24 21:51:32,663 INFO org.apache.flink.runtime.security.token.KerberosDelegationTokenManagerFactory [] - Cannot use kerberos delegation token manager no valid kerberos credentials provided. 2025-10-24 21:51:32,671 INFO org.apache.flink.runtime.metrics.MetricRegistryImpl [] - No metrics reporter configured, no metrics will be exposed/reported. 2025-10-24 21:51:32,674 INFO org.apache.flink.runtime.rpc.akka.AkkaRpcServiceUtils [] - Trying to start actor system, external address dc3-dominos-usdp-fun01:0, bind address 0.0.0.0:0. 2025-10-24 21:51:32,688 INFO akka.event.slf4j.Slf4jLogger [] - Slf4jLogger started 2025-10-24 21:51:32,693 INFO akka.remote.RemoteActorRefProvider [] - Akka Cluster not in use - enabling unsafe features anyway because `akka.remote.use-unsafe-remote-features-outside-cluster` has been enabled. 2025-10-24 21:51:32,693 INFO akka.remote.Remoting [] - Starting remoting 2025-10-24 21:51:32,700 INFO akka.remote.Remoting [] - Remoting started; listening on addresses :[akka.tcp://flink-metrics@dc3-dominos-usdp-fun01:44491] 2025-10-24 21:51:32,710 INFO org.apache.flink.runtime.rpc.akka.AkkaRpcServiceUtils [] - Actor system started at akka.tcp://flink-metrics@dc3-dominos-usdp-fun01:44491 2025-10-24 21:51:32,721 INFO org.apache.flink.runtime.rpc.akka.AkkaRpcService [] - Starting RPC endpoint for org.apache.flink.runtime.metrics.dump.MetricQueryService at akka://flink-metrics/user/rpc/MetricQueryService . 2025-10-24 21:51:32,778 INFO org.apache.flink.configuration.Configuration [] - Config uses fallback configuration key 'rest.port' instead of key 'rest.bind-port' 我看flink的报错信息里,还是有akka啊?
11-13
六自由度机械臂ANN人工神经网络设计:正向逆向运动学求解、正向动力学控制、拉格朗日-欧拉法推导逆向动力学方程(Matlab代码实现)内容概要:本文档围绕六自由度机械臂的ANN人工神经网络设计展开,详细介绍了正向与逆向运动学求解、正向动力学控制以及基于拉格朗日-欧拉法推导逆向动力学方程的理论与Matlab代码实现过程。文档还涵盖了PINN物理信息神经网络在微分方程求解、主动噪声控制、天线分析、电动汽车调度、储能优化等多个工程与科研领域的应用案例,并提供了丰富的Matlab/Simulink仿真资源和技术支持方向,体现了其在多学科交叉仿真与优化中的综合性价值。; 适合人群:具备一定Matlab编程基础,从事机器人控制、自动化、智能制造、电力系统或相关工程领域研究的科研人员、研究生及工程师。; 使用场景及目标:①掌握六自由度机械臂的运动学与动力学建模方法;②学习人工神经网络在复杂非线性系统控制中的应用;③借助Matlab实现动力学方程推导与仿真验证;④拓展至路径规划、优化调度、信号处理等相关课题的研究与复现。; 阅读建议:建议按目录顺序系统学习,重点关注机械臂建模与神经网络控制部分的代码实现,结合提供的网盘资源进行实践操作,并参考文中列举的优化算法与仿真方法拓展自身研究思路。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值