Book(拓扑排序,dp,优先队列)

C. Book

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.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤2⋅104).

The first line of each test case contains a single integer n (1≤n≤2⋅105) — number of chapters.

Then n lines follow. The i-th line begins with an integer ki (0≤ki≤n−1) — number of chapters required to understand the i-th chapter. Then ki integers ai,1,ai,2,…,ai,ki (1≤ai,j≤n,ai,j≠i,ai,j≠ai,l for j≠l) follow — the chapters required to understand the i-th chapter.

It is guaranteed that the sum of n and sum of ki over all testcases do not exceed 2⋅105.

Output
For each test case, if the entire book can be understood, print how many times you will read it, otherwise print −1.

  • 输入
    5
    4
    1 2
    0
    2 1 4
    1 2
    5
    1 5
    1 1
    1 2
    1 3
    1 4
    5
    0
    0
    2 1 2
    1 2
    2 2 1
    4
    2 2 3
    0
    0
    2 3 2
    5
    1 2
    1 3
    1 4
    1 5
    0

  • 输出
    2
    -1
    1
    2
    5

  • 举例

  1. In the first example, we will understand chapters {2,4} in the first reading and chapters {1,3} in the second reading of the book.
  2. In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book.
  3. In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go.
  4. In the fourth example, we will understand chapters {2,3,4} in the first reading and chapter 1 in the second reading of the book.
  5. In the fifth example, we will understand one chapter in every reading from 5 to 1.

Codeforces Round #743 (Div. 2) C. Book

//拓扑排序+优先队列
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N = 2e5 + 10;

vector<int> g[N];  
//g数组用来存储每一个点的出边(存在先a才可以b g[a].push_back(b))
int d[N];  //d[i]存储i点有多少边进入 d[i]==0说明可以做到
typedef pair<int, int> PII;
//第一位存储是第几轮被做到,第二位存储是这个点的编号

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            g[i].clear();
            d[i] = 0;
        }
        for (int i = 1; i <= n; i++)
        {
            int t;
            scanf("%d", &t);
            while (t--)
            {
                int a;
                scanf("%d", &a);
                g[a].push_back(i);
                d[i]++;
            }
        }

        priority_queue<PII, vector<PII>, greater<PII>> que;

        for (int i = 1; i <= n; i++)
        {
            if (!d[i])  
            //在一开始先把d[i]==0点入队 轮数是1 这是第一轮
                que.push(make_pair(1, i));
        }

        int res = 0;
        int ans = 0;
        while (que.size())
        {
            PII head = que.top();
            ans = head.x;
            que.pop();
            int bh = head.y;
            res++;  //这是被做到的章节
            int n = g[bh].size(); 
         //当bh被做到以后 与它相关联的所有章节都被删除一个入点
            for (int i = 0; i < n; i++)
            {
                int to = g[bh][i];
                d[to]--;
                if (!d[to])
                {
                    if (to < bh)
                        que.push(make_pair(head.x + 1, to));
                    else
                        que.push(make_pair(head.x, to));
                }
            }
        }

        if (res == n)
            printf("%d\n", ans);
        else
            printf("-1\n");
    }
}

dp的思路其实和上一种方法有着较大的相同之处,但是有着dp状态转移方程的考虑,主要是思考

  • 当可以入队的节点(x)在前置节点(y)的前面时候
    dp[x]=max(dp[x],dp[y]+1)
  • 当可以入队的节点(x)在前置节点(y)的后面的时候
    dp[x]=max(dp[x],dp[y])

其实就是简单的思考了是否会再重新搞一遍的情况发生

//dp思路 大体的框架差不多
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;

vector<int> g[N];
int d[N];
int dp[N];

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        queue<int> que;

        int n;
        cin >> n;

        for (int i = 0; i <= n; i++)
        {
            g[i].clear();
            d[i] = 0;
            dp[i] = 1;
        }

        for (int i = 1; i <= n; i++)
        {
            int t;
            cin >> t;
            while (t--)
            {
                int a;
                cin >> a;
                g[a].push_back(i);
                d[i]++;
            }
        }

        for (int i = 1; i <= n; i++)
            if (!d[i])
                que.push(i);

        int res = 0;
        int ans = 0;
        while (que.size())
        {
            int head = que.front();
            que.pop();

            res++;

            ans = max(ans, dp[head]);

            int n = g[head].size();
            for (int i = 0; i < n; i++)
            {
                d[g[head][i]]--;
                if (g[head][i] < head)
                    dp[g[head][i]] = max(dp[g[head][i]], dp[head] + 1);
                else
                    dp[g[head][i]] = max(dp[g[head][i]], dp[head]);

                if (!d[g[head][i]])
                    que.push(g[head][i]);

            }
        }
        if (res == n)
            cout << ans << endl;
        else
            cout << -1 << endl;
    }
}
上一篇:chapter 1 顺序结构


下一篇:Chapter 2 - Sockets and Patterns【选译,哈哈】 Part 9 High-Water Marks