求助 思路是对的但是不会写
查看原帖
求助 思路是对的但是不会写
508624
Chronomia楼主2022/10/30 09:29
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main();
int read()
{
    int xsn = 1, xnb = 0;
    char c = getchar();
    while (c != 45 && (c <= 47 || c >= 58))
        c = getchar();
    if (c == 45)
        xsn = -1, c = getchar();
    while (c >= 48 && c <= 57)
        xnb = (xnb << 1) + (xnb << 3) + (c ^ 48), c = getchar();
    xnb *= xsn;
    return xnb;
}
void print(int x)
{
    if (x < 0)
        putchar(45), x = -x;
    if (x >= 10)
        print(x / 10);
    putchar(x % 10 ^ 48);
}
int n, m, k;
struct node
{
    bitset<2550> con;
    vector<int> cn;
    int val;
    vector<pair<int, int>> twice;
    vector<int> chk;
};
node v[2550];
bitset<2550> vis;
vector<int> bfs1;
bool differ(int a, int b, int c, int d)
{
    return a != b && b != c && c != d && a != c && b != d && a != d;
}
signed main()
{
    // freopen("holiday.in","r",stdin);
    // freopen("holiday.out","w",stdout);
    //	freopen("holiday3.in","r",stdin);
    //	freopen("test.out","w",stdout);
    n = read(), m = read(), k = read();
    for (int i = 2; i <= n; ++i)
        cin >> v[i].val;
    while (m--)
    {
        int a, b;
        a = read(), b = read();
        v[a].con.set(b);
        v[b].con.set(a);
        v[a].cn.push_back(b);
        v[b].cn.push_back(a);
    }
    queue<pair<int, int>> q;
    q.push({1, 0});
    vis[1] = 1;
    while (!q.empty())
    {
        auto x = q.front();
        q.pop();
        if (x.second == k + 1)
            continue;
        for (auto i : v[x.first].cn)
        {
            if (!vis[i])
            {
                vis.set(i);
                q.push({i, x.second + 1});
            }
        }
    }
    for (int i = 2; i <= n; ++i)
    {
        bitset<2550> vs;
        queue<pair<int, int>> qq;
        qq.push({i, 0});
        vs[i] = 1;
        while (!qq.empty())
        {
            auto x = qq.front();
            qq.pop();
            if (x.second == k + 1)
                continue;
            for (auto j : v[x.first].cn)
            {
                if (!vs[j])
                {
                    vs.set(j);
                    if (vis[i])
                        v[j].twice.push_back(make_pair(i, v[i].val + v[j].val));
                    v[i].chk.push_back(j);
                    qq.push({j, x.second + 1});
                }
            }
        }
    }
    int ans = -1;
    for (int i = 2; i <= n; ++i)
    {
        for (auto j : v[i].chk)
        {
            for (auto R1 : v[j].twice)
            {
                for (auto R2 : v[i].twice)
                {
                    if (differ(i, j, R1.first, R2.first))
                    {
                        //						cout<<i<<" "<<j<<" "<<R1.second+R2.second<<endl;
                        ans = max(ans, R1.second + R2.second);
                    }
                }
            }
        }
    }
    print(ans);
    //	freopen("test.log","w",stdout);
    //	system("fc test.out holiday3.ans");
    return 0;
}

求大佬帮忙改一下

2022/10/30 09:29
加载中...