Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ program to find out the maximum value of i
Suppose, we have a permutation of integers 'seq' and an array of integer pairs 'pairs' of size m that contains integers 0 to n - 1. Now, we perform the following operation on seq as many times as possible so that seq[i] = i (0 ≤ i
We have to choose an integer j where 0
We have to find out the maximum value of i so that seq[i] = i after performing the operation multiple times.
So, if the input is like n = 4, m = 2, seq = {0, 3, 2, 1}, pairs = {{0, 1}, {2, 3}}, then the output will be 2.
The maximum possible value is 2.
To solve this, we will follow these steps −
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 iExample
Let us see the following implementation to get better understanding −
#includeusing 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; } Input
4, 2, {0, 3, 2, 1}, {{0, 1}, {2, 3}}Output
2
Advertisements
