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;
}
Codeforces Round #586 (Div. 1 + Div. 2) E. Tourism的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- PTA(Advanced Level)1041.Be Unique
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- spring security中Authority、Role的区别
最近在研究spring security时,大概研究了一下Authority.Role之间到底有什么本质的区别. 如果你使用的是hasRole方法来判断你的登录用户是否有权限访问某个接口,那么你初始化 ...
- PHP学习之PHP trait解析
自PHP5.4.0起,PHP实现了一种代码复用的方法,称为trait. 众所周知,PHP中是单继承的,trait是为类似PHP的单继承语言而准备得一种代码复用机制.trait为了减少单继承语言的限制, ...
- Python 基础(十六)--随机数模块
random随机数模块 random.randint(1,10):随机1-10包括10 random.randrange(1,10,2):在1.3.5.7.9中随机,类似切片,不包括10 random ...
- 怎样获取不同环境下的document对象
1. 在一般的网页文档中, 可通过: document 或 window.document 获取; 2. 在iframe框架中, 可通过iframe节点的属性: contentDocument 获取; ...
- python 的常见排序算法实现
python 的常见排序算法实现 参考以下链接:https://www.cnblogs.com/shiluoliming/p/6740585.html 算法(Algorithm)是指解题方案的准确而完 ...
- ajax调用c# webservice 如何传递实体参数
后端: 前端: 运行结果: 感觉还是比较简单,只是好久没有写webservice 了,上网找了一下写法
- ASE19团队项目alpha阶段model组 scrum11 记录
本次会议于11月15日,19时整在微软北京西二号楼sky garden召开,持续5分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- 随机模块 random 函数的调用
随机模块 random 作用: 用于模拟或生成随机输出的模块. 用法示意: import random as R 函数名 描述 R.random() 返回一个[0, 1) 之间的随机实数 R.unif ...
- java 使用POI导出百万级数据
先看结果吧,这只是测试其中有很多因数影响了性能. 表总数为:7千多万,测试导出100万 表字段有17个字段 最终excel大小有60多兆 总耗时:126165毫秒 差不多2分多钟 其核心简单来说就是分 ...