简单的dfs 70分,求助大佬!!!
查看原帖
简单的dfs 70分,求助大佬!!!
914079
ylh1230i楼主2023/2/6 22:52

#include <bits/stdc++.h>
using namespace std;
#if 1
#define int LL
#endif
using LL = long long;
using DB = double;
using PI = pair<int, int>;
using PL = pair<LL, LL>;
template<typename T> using v = vector<T>;
constexpr auto INF = 0X3F3F3F3F;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;
#define ic std::ios::sync_with_stdio(false);std::cin.tie(nullptr)
template <typename ConTainermap> void dbgumap(ConTainermap c);	//output umap
template <typename _Ty> void dbg(_Ty arr[], int n, int m = -1);
inline int read();			//fast input
inline void write(int x);	//fast output

//TODO: Write code here
int n, m;
const int N = 1e5 + 10;
int nums[N], wsum[N], ans;
v<int> vec[N];
bool vis[N];
bool top[N];
void dfs(int root, int fa)
{
    if (!vis[fa])
    {
        wsum[root] = nums[root];
    }
    for (int i = 0; i < vec[root].size(); i++)
    {
        int son = vec[root][i];
        if (son != fa)
        {
            int sumL = wsum[root], sumR = wsum[root];
            if (!vis[fa])//如果说root的父亲不参加,则root参加
            {
                vis[root] = true;//根参加
                dfs(son, root);//递归到孩子,孩子们全部不参加
                if (wsum[son] > 0) sumL += wsum[son];
            }
            else//如果root的父亲参加,则root不参加,root的孩子们可以参加
            {
                dfs(son, root);
                if (wsum[son] > 0) sumR += wsum[son];
            }
            wsum[root] = max(sumL, sumR);
        }
    }
    if (wsum[root] > ans) ans = wsum[root];
}
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> nums[i];
    for (int i = 1; i <= n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        vec[u].push_back(v);
        vec[v].push_back(u);
        top[u] = true;    //v是u的上司
    }
    for (int i = 1; i <= n; i++)
    {
        memset(vis,false,sizeof(vis));
        if (!top[i]) dfs(i, 0);
    }
    cout << ans;
#define one 1
    return 0;
}

template <typename _Ty>
void dbg(_Ty arr[], int n, int m)
{
#if one
    for (int i = 1; i <= n; i++)
    {
        cout << arr[i] << ' ';
    }
#else
    if (m == -1) { cout << "please input m! "; return; }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << *(arr[i] + j) << ' ';
        }
        cout << endl;
    }
#endif
    cout << endl;
}
template <typename ConTainermap>
void dbgumap(ConTainermap c)
{
    for (auto& x : c)
    {
        cout << "key:" << x.first << "  val:" << x.second << endl;
    }
}
inline int read()
{
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9')
    {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * w;
}
inline void write(int x)
{
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48);
}
2023/2/6 22:52
加载中...