T268749题解

· · 个人记录

这道题:luogu大水题

luogu传送门

本题重点是“建模理解”,“去重”,以及“最少多少天才能满足愿望”。

本题一个思路:每天推荐的题都做了。(不做会亏)。然后注意一点:少数服从多数,推荐的时候我们取得是最后做的题,也就是最长路。

最长路的做法和最短路差不多,只不过要判断去重。

去重

去重本质上只是判断能否做第K题。 当下列情况发生任意一种时则不能做出:

  1. 在推荐K题的路线中有环
  2. 中途有题不会被推荐且不是第一天推荐的

如何判断环?

建立一个数组 fa,然后用函数搜索判断是否存在某个父节点。

int fa[1005][1005] = {}; // fa[i][j]表示点i是点j的直系父节点
bool isfa(int f, int s)
{
    if (f == s) // 如果要求的父节点和自己一样
    {
        return true;
    }
    if (fa[f][s]) // 如果是直系
    {
        return true;
    }
    mark[s] = true; // 去重
    for (int i = 1; i <= N; i++) // 从直系父节点中找
    {
        if (!mark[i])
        {
            if (fa[i][s])
            {
                if (isfa(f, i))
                {
                    fa[f][s] = true; // 其实也相当于“直系”了
                    mark[s] = false;
                    return true;
                }
            }
        }
    }
    mark[s] = false;
    return false;
}

则使用

isfa(son, father);

可以判断连接后是否回成为环。

最后附上有注释的代码

#include <iostream>
#include <queue>

using namespace std;

#define END             \
    cout << -1 << endl; \
    return 0;

// 这个宏只是为了好看

struct Edge
{
    int to;
    int next;
} edges[1000007]; // 一千的平方
int head[1005] = {};

int rec[1005][1005] = {}; // 推荐信息,rec[i][j]表示要推荐i题必须先做第rec[i][j]题,rec[i][0]表示要做多少题才能推荐
int need[1005] = {};      // 是否需要做
int dist[1005] = {};      // 天数+1
int mark[1005] = {};
int cnt = 0;

int N, K;

int fa[1005][1005] = {}; // 这里讲过了
bool isfa(int f, int s)
{
    if (f == s)
    {
        return true;
    }
    if (fa[f][s])
    {
        return true;
    }
    mark[s] = true;
    for (int i = 1; i <= N; i++)
    {
        if (!mark[i])
        {
            if (fa[i][s])
            {
                if (isfa(f, i))
                {
                    fa[f][s] = true;
                    mark[s] = false;
                    return true;
                }
            }
        }
    }
    mark[s] = false;
    return false;
}

bool add(int from, int to) // 添加边,返回是否会成为环
{
    if (isfa(from, to)) // 已经连过了
    {
        return false;
    }
    if (dist[from] != 1)
    {
        if (isfa(to, from)) // 会连成环
        {
            return true;
        }
    }
    cnt++;
    edges[cnt].to = to;
    edges[cnt].next = head[from];
    head[from] = cnt;
    fa[from][to] = true;
    return false;
}

int main()
{
    int p;
    cin >> N >> K >> p;
    int cur;
    for (int i = 1; i <= p; i++)
    {
        cin >> cur;
        dist[cur] = 1; // 第一天做的题
    }
    int R, rp, r, pi;
    cin >> R;
    for (int i = 1; i <= R; i++) // 输入部分。变量名的对应的意义和在题目里出现的顺序的意义一样。
    {
        cin >> rp >> r;
        rec[rp][0] = r;
        for (int j = 1; j <= r; j++)
        {
            cin >> pi;
            rec[rp][j] = pi;
        }
    }
    queue<int> que1; // 一队两用,广搜+堆优化
    que1.push(K);
    need[K] = 1;
    while (!que1.empty())
    {
        int t = que1.front();
        que1.pop();
        if (dist[t] == 1)
        {
            continue;
        }
        if (rec[t][0] == 0)
        {
            END;
        }
        for (int i = 1; i <= rec[t][0]; i++)
        {
            if (add(rec[t][i], t)) // 会成环
            {
                END;
            }
            need[rec[t][i]] = 1;
            que1.push(rec[t][i]);
        }
    }
    priority_queue<int, vector<int>, less<int> /**/> que;
    for (int i = 1; i <= N; i++)
    {
        if (dist[i] == 1 && need[i] == 1)
        {
            que.push(i);
        }
    }
    while (!que.empty()) // 已经没有回路了,所以可以放心用最短路
    {
        int t = que.top();
        que.pop();
        for (int i = head[t]; i != 0; i = edges[i].next)
        {
            if (dist[edges[i].to] != 1 && need[edges[i].to] == 1)
            {
                if (dist[t] + 1 > dist[edges[i].to]) // <改>就可以了
                {
                    dist[edges[i].to] = dist[t] + 1;
                    que.push(edges[i].to);
                }
            }
        }
    }
    cout << (dist[K] - 1) << endl; // 别忘了-1,如果达不到就是0-1=-1,正好
    return 0;
}

第一次写题解,不喜勿喷