链接:

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;
}

Codeforces Round #586 (Div. 1 + Div. 2) E. Tourism的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. Centos7下关闭Firewalls配置iptables

    在网上搜索了很多这种资料,现在总结一下以备后用. 1.关闭防火墙:sudo systemctl stop firewalld.service 2.关闭开机启动:sudo systemctl disab ...

  2. ubuntu下安装navicat

    1.去官网下载 https://www.navicat.com/en/download/navicat-premium 2.命令行输入(解压命令) tar -zxvf xxxxx.tar.gz 3.移 ...

  3. 剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

    1 题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 2. 思路和方法 利用辅助栈来存储现有栈的最小值.在入栈和出栈的时候将现有栈和最小 ...

  4. codeforces 1244E Minimizing Difference (贪心)

    (点击此处查看原题) 题意分析 给出n个数,a1,a2...an,现在可以进行最多k次操作,每次操纵可以使得任意一个数自增或者自减,问经过最多k次操作后,n个数中的最大值-最小值最小为多少? 解题思路 ...

  5. kubernetes kubeadm安装v1.14

    1.我们这里准备两台Centos7的主机用于安装,后续节点可以根究需要添加即可:master node01两台都得改:cat /etc/hosts192.168.71.134 master192.16 ...

  6. js实现div转图片并保存

    最近工作中遇到的需求,将div转成图片并保存. 1.准备需要用到的js插件jquery-1.8.2.js,html2canvas.min.js(将div转换为canvas),bluebird.js(用 ...

  7. 阿里云=>RHSA-2019:1884-中危: libssh2 安全更新

    由于项目构建时间比较长,近期安全检查发现openssh有漏洞.所以要升级openssh到7.9p1版本.由于ssh用于远程连接,所以要谨慎操作. 建议生成环境要先做测试,之后再在生产环境升级. 1 前 ...

  8. 深入理解Java自动装箱拆箱机制

    1.自动装箱与拆箱的定义 装箱就是自动将基本数据类型转换为包装器类型(int-->Integer): 拆箱就是自动将包装器类型转换为基本数据类型(Integer-->int). Java中 ...

  9. ::selection伪元素改变文本选中颜色

    在看张鑫旭大神的文章,刚刚发现了这个,感觉应该后面有用,先存起来. 效果如下: 代码如下: <!DOCTYPE html> <html> <head> <me ...

  10. 让theano在windows下能进行GPU并行的配置步骤

    最近在implement DeepLearning.net上面的程序.对于开源的python,最头疼的就是各种package和各种configuration. 而且还是在windows下. 想要让th ...