Codeforces Round #586 (Div. 1 + Div. 2) E. Tourism

链接:

https://codeforces.com/contest/1220/problem/E

题意:

Alex decided to go on a touristic trip over the country.

For simplicity let's assume that the country has n cities and m bidirectional roads connecting them. Alex lives in city s and initially located in it. To compare different cities Alex assigned each city a score wi which is as high as interesting city seems to Alex.

Alex believes that his trip will be interesting only if he will not use any road twice in a row. That is if Alex came to city v from city u, he may choose as the next city in the trip any city connected with v by the road, except for the city u.

Your task is to help Alex plan his city in a way that maximizes total score over all cities he visited. Note that for each city its score is counted at most once, even if Alex been there several times during his trip.

思路:

题意是不能立即原路返回, 而是绕一下可以原路返回..以为是每条边只能走一次.
这样的话看别人代码, 考虑Dfs, 记录存不存在一个链最底下有没有环存在, 如果有就可以返回上来, 吧点值累计到答案.
如果不行吧最长不能返回的链值单独记录, 然后不停地往上去更新, 更新出一个值最大的链即可.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;

vector<int> G[MAXN];
LL Other[MAXN], Val[MAXN], res = 0;
int Vis[MAXN];
int n, m, s;

bool Dfs(int u, int fa)
{
    Vis[u] = 1;
    bool flag = false;
    for (int i = 0;i < G[u].size();i++)
    {
        int node = G[u][i];
        if (node == fa)
            continue;
        if (Vis[node] == 1)
        {
            flag = true;
            continue;
        }
        bool Tmp = Dfs(node, u);
        if (Tmp)
            flag = true;
        Other[u] = max(Other[u], Other[node]);
    }
    if (flag)
    {
        res += Val[u];
        return true;
    }
    else
    {
        Other[u] += Val[u];
        return false;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= n;i++)
        cin >> Val[i];
    int u, v;
    for (int i = 1;i <= m;i++)
    {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    cin >> s;
    Dfs(s, 0);
    cout << res+Other[s] << endl;

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11627536.html