vector40ptsMLE求调
查看原帖
vector40ptsMLE求调
1028047
ZinfI_Sh楼主2024/8/9 19:36

看了一些同情况帖子还是不会

#include <bits/stdc++.h>
#define int long long
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
#define lowbit(x) (x & -x)
using namespace std;
const int DM[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
const int HASHMOD[2] = {9223372036854775783, 212370440130137957};
const int HASHBASE[2] = {131, 83};
const int HASHITEM = 999997;
inline void read(int &res)
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    res = x * f;
}
const int N = 2100005;
vector<int> e[N], af[N];
map<pair<int, int>, int> hav;
int cx[N], cy[N];
int dfn[N], low[N], col[N], cnt[N], tick, tot, poi[N];
stack<int> stak;
void tarjan(int u)
{
    dfn[u] = low[u] = ++tick;
    stak.push(u);
    for (auto v : e[u])
    {
        if (!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (!col[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u])
    {
        col[u] = ++tot;
        cnt[tot] = poi[u];
        while (stak.top() != u)
        {
            col[stak.top()] = tot;
            cnt[tot] += poi[stak.top()];
            stak.pop();
        }
        stak.pop();
    }
}
queue<int> q;
int ru[N], dp[N];
signed main()
{
    int n, r, c;
    cin >> n >> r >> c;
    for (int i = 1; i <= n; i++)
    {
        poi[r + c + i] = 1;
        int x, y, t;
        cin >> x >> y >> t;
        e[x].push_back(r + c + i);
        e[y + r].push_back(r + c + i);
        if (t == 1)
        {
            e[r + c + i].push_back(x);
        }
        if (t == 2)
        {
            e[r + c + i].push_back(y + r);
        }
        if (t == 3)
        {
            cx[i] = x;
            cy[i] = y;
        }
        hav[make_pair(x, y)] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        if (cx[i])
        {
            for (int j = 0; j < 8; j++)
            {
                int dx = cx[i] + DM[j][0], dy = cy[i] + DM[j][1];
                if (hav[make_pair(dx, dy)])
                {
                    e[r + c + i].push_back(hav[make_pair(dx, dy)] + r + c);
                }
            }
        }
    }
    for (int i = 1; i <= r + c + n; i++)
    {
        if (!dfn[i])
        {
            tarjan(i);
        }
    }
    for (int i = 1; i <= r + c + n; i++)
    {
        for (auto v : e[i])
        {
            if (col[i] != col[v])
            {
                af[col[i]].push_back(col[v]);
                ru[col[v]]++;
            }
        }
    }
    for (int i = 1; i <= tot; i++)
    {
        if (!ru[i])
        {
            dp[i] = cnt[i];
            q.push(i);
        }
    }
    while (q.size())
    {
        int u = q.front();
        q.pop();
        for (auto v : af[u])
        {
            dp[v] = max(dp[v], dp[u] + cnt[v]);
            if (--ru[v] == 0)
            {
                q.push(v);
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= tot; i++)
    {
        ans = max(ans, dp[i]);
    }
    cout << ans;
    return 0;
}
2024/8/9 19:36
加载中...