Entering edit mode

**Overview**

You are given a book with n chapters.

Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list.

Currently, you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book.

**Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter.**Determine

**how many times you will read the book to understand every chapter**, or**determine that you will never understand every chapter no matter how many times you read the book**.

**Solution**

- We will construct a graph, where there is a directed edge from a to b if chapter a is needed to understand chapter b. This edge has weight 0 if b>a and 1 otherwise.
- The answer is the length of the longest weighted path in this graph incremented by 1. If there exists a cycle we should output −1.
- If the graph is a DAG, we can use topological sort and a simple DP to calculate the answer.

**Code**

```
void dfs(ll v)
{
for (auto it : adj[v])
{
if (color[it.ff] == 0)
{
color[it.ff] = 1; // Set value as 1
dfs(it.ff);
}
else if (color[it.ff] == 1) // Means a cycle a present in the graph
{
flag = 1; // Make flag 1
}
}
color[v] = 2; // Set value of color to 2
}
void solve()
{
ll n;
cin >> n;
flag = 0;
for (ll i = 1; i <= n; i++)
{
ll x;
cin >> x;
for (ll j = 0; j < x; j++)
{
ll y;
cin >> y;
if (i > y)
{
adj[y].pb({i, 0});
}
else
{
adj[y].pb({i, 1});
}
ind[i]++; // increase dependency by 1
}
}
for (ll i = 1; i <= n; i++)
{
if (color[i] == 0)
{
color[i] = 1;
dfs(i);
}
}
if (flag)
{
for (ll i = 1; i <= n; i++) // reset all values
{
color[i] = 0;
d[i] = 0;
ind[i] = 0;
adj[i].clear();
}
cout << -1 << endl; // If cycle is present then its never possible to read all the chapters
return;
}
queue<ll> q;
for (ll i = 1; i <= n; i++)
color[i] = 0;
for (ll i = 1; i <= n; i++)
{
if (ind[i] == 0) // If a particular chapter has no dependency
{
color[i] = 1;
q.push(i); // insert it in a queue
d[i] = 0;
}
}
ll ans = 0;
while (!q.empty())
{
ll v = q.front();
color[v] = 1;
q.pop(); // Take out the element
for (auto it : adj[v])
{
ind[it.ff]--; // decrese the dependency by 1
if (ind[it.ff] == 0)
{
q.push(it.ff);
}
d[it.ff] = max(d[it.ff], d[v] + it.ss); // increase the distance accordingly
}
}
for (ll i = 1; i <= n; i++)
{
ans = max(ans, d[i] + 1); // answer would be maximum of all possible distances + 1(for starting)
color[i] = 0;
d[i] = 0;
ind[i] = 0;
adj[i].clear();
}
cout << ans << endl;
return;
}
```

Entering edit mode

Q2)

- First of all let's analyze when we can convert
**a[i] to X it can be done iff (a[i] - p * b[i])==X, where p is number of steps we have used for i_th integer** - So now we know when we can convert
**any a[i] to X**, so let's iterate over all possible values of X**0 < =X < =max_element of array**, because we cannot make**X > max_element of array**, due to the fact we can only reduce number in any step**not increase them**,, and since we can only apply operation when**a[i]>=b[i]**, and all**a[i]'s are >=1 initially**, so X**cannot be less than 0** - Now for number of steps, we need to check the following for converting
**a[i] to X****a[i]>=X****(a[i]-X) should be multiple of b[i]**, because**a[i] - X = p * b[i]**- Number of steps would be p,
**p = (a[i]-X) / b[i]**

```
Iterate over values of X in [0....max_element of array]
possible = 1
steps=0
for ( int i=0;i < n;i++)
if(a[i] > =X and ((a[i]-X) is divisible by b[i]) )
steps= steps + (a[i]-X)/b[i]
else
possible=0
if(possible)
answer=min(answer,steps)
```

**If you do not get any valid possible than output -1**

Loading Similar Posts