链接:

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

最新文章

  1. Hadoop入门学习笔记---part4
  2. python列表模拟堆栈和队列
  3. 05.virsh命令的常用操作(kvm)
  4. jQuery触发a标签点击事件-为什么不跳转
  5. Android开发学习笔记:浅谈显示Intent和隐式Intent
  6. CentOS 下JDK安装
  7. 2012 #1 Saving Princess claire_
  8. 关于webapi 返回的类型的笔记
  9. Android设备定制为永不锁屏
  10. 保存mysql用户的登录信息到~.my.cnf文件;用于方便登录操作。
  11. jupyter nootbook本地使用指南
  12. 安装Visual Studio 语言包时出现windows 程序兼容模式已打开.请将其关闭
  13. MVC+EF 多条件查询
  14. 架构(四)Git简介,安装以及相关命令SourceTree
  15. 3ds max学习笔记-- 动画
  16. Oracle11默认用户名和密码
  17. 如何打印一棵树(Java)
  18. spring注解之@Lazy
  19. EJB2.0 ejb-jar.xml配置文件详解
  20. 【WIN10】WIN2D——基本圖形的繪製

热门文章

  1. kafka server.properties 配置文件详解(二)
  2. [转]Mybatis之TypeHandler使用教程
  3. 安装 Dashboard 插件
  4. 记笔记的软件(vnote)
  5. Entity的约束
  6. spring利用xml配置定时任务
  7. idea内存溢出解决方法
  8. 深入JavaScript对象(Object)与类(class),详细了解类、原型
  9. fragment概念理解
  10. oracle索引查询