C++程序找出i的最大值

首页 编程分享 PHP丨JAVA丨OTHER 正文

PHPz 转载 编程分享 2023-09-15 21:20:32

简介 假设我们有一个整数排列'seq'和一个大小为m的整数对数组'pairs',其中包含整数0到n-1。现在,我们尽可能多地对seq执行以下操作,以使seq[i]=i(0≤i


假设我们有一个整数排列 'seq' 和一个大小为 m 的整数对数组 'pairs',其中包含整数 0 到 n - 1。现在,我们尽可能多地对 seq 执行以下操作,以使 seq[i] = i (0 ≤ i

  • 我们必须选择一个整数 j,其中 0

我们必须找出 i 的最大值,使得在多次执行操作后 seq[i] = i。

因此,如果输入是 n = 4,m = 2,seq = {0, 3, 2, 1},pairs = {{0, 1}, {2, 3}},那么输出将是 2。

最大可能的值是 2。

为了解决这个问题,我们将按照以下步骤进行:

N := 100
Define an array tp of size: N.
Define arrays vtmp, vis of size N.
Define a function dfs(), this will take j, k,
tp[j] := k
insert j at the end of vtmp[k]
for each value b in vis[j], do:
   if tp[b] is not equal to 0, then:
      Ignore following part, skip to the next iteration
   dfs(b, k)
res := 0
for initialize i := 0, when i 

Example

让我们看下面的实现以更好地理解−

#include 
using namespace std;
const int INF = 1e9;
#define N 100
int tp[N];
vector vtmp[N], vis[N];
void dfs(int j, int k){
   tp[j] = k;
   vtmp[k].push_back(j);
   for(auto b : vis[j]) {
      if(tp[b] != 0)
         continue;
      dfs(b, k);
   }
}
void solve(int n, int m, int seq[], vector> pairs) {
   int res = 0;
   for(int i = 0; i > pairs = {{0, 1}, {2, 3}};
   solve(n, m, seq, pairs);
   return 0;
}

输入

4, 2, {0, 3, 2, 1}, {{0, 1}, {2, 3}}

输出

2

以上就是C++程序找出i的最大值的详细内容,更多请关注php中文网其它相关文章!

转载链接:https://www.php.cn/faq/609106.html


Tags:


本篇评论 —— 揽流光,涤眉霜,清露烈酒一口话苍茫。


    声明:参照站内规则,不文明言论将会删除,谢谢合作。


      最新评论




ABOUT ME

Blogger:袅袅牧童 | Arkin

Ido:PHP攻城狮

WeChat:nnmutong

Email:nnmutong@icloud.com

标签云