传送门

这是一道英语题,首先要读懂题目:

$\text{Alex believes that his trip will be interesting only if he will not use any road twice in a row.}$

这句话意思是不会连续走一条路,但是同一条路是可以走多次的

所以对于一个边双联通分量,是可以全部走一遍并可以从联通分量里的任意一个点离开的

所以就可以直接缩点,然后就变成树上问题

发现对于树上的点,如果它的大小为 $1$ 并且它的儿子都没法返回 ,那么到达它以后就没法返回,反之一定可以返回(自己内部绕一圈或者走到儿子再回来即可)

考虑树上搞搞 $dp$,设 $f[x]$ 表示走完 $x$ 的子树以后并能返回 $x$ 的父亲时得到的最大价值,$g[x]$ 表示走完 $x$ 的子树不返回的最大价值

那么对于一个可以返回父亲的的子节点 $v$ ,有转移:$f[x]+=f[v]$

对于 $g[x]$ ,比较复杂,首先 $g[x]$ 可以是 $x$ 走完所有可以返回的儿子,最后再走到一个不能返回的儿子:$g[x]=f[x]+g[v]$

也可以是走到某个虽然可以返回,但是没必要返回的儿子里面不返回(因为儿子再往下走到不能返回的节点最终价值可能更大):$g[x]=g[x]-f[v]+g[v]$,这里减去 $f[v]$ 是因为之前加上了

然后最后答案就是 $g[root]$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=4e5+;
int n,m,a[N];
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int dfn[N],low[N],bel[N],st[N],sz[N],Top,cnt,tot;
void Tarjan(int x,int fa)
{
dfn[x]=low[x]=++cnt; st[++Top]=x;
for(int i=fir[x];i;i=from[i])
{
int &v=to[i]; if(v==fa) continue;
if(!dfn[v]) Tarjan(v,x),low[x]=min(low[x],low[v]);
else if(!bel[v]) low[x]=min(low[x],dfn[v]);
}
if(low[x]!=dfn[x]) return;
tot++; while(st[Top]!=x) bel[st[Top--]]=tot;
bel[st[Top--]]=tot;
}
struct edge {
int u,v;
edge (int _u=,int _v=) { u=_u,v=_v; }
inline bool operator < (const edge &tmp) const {
return u!=tmp.u ? u<tmp.u : v<tmp.v;
}
inline bool operator == (const edge &tmp) const {
return u==tmp.u&&v==tmp.v;
}
};
vector <edge> tmp;
vector <int> V[N];
ll val[N];
void build()
{
for(int i=;i<=n;i++) val[bel[i]]+=a[i],sz[bel[i]]++;
for(int i=;i<=n;i++)
for(int j=fir[i];j;j=from[j])
{
int &v=to[j]; if(bel[i]==bel[v]) continue;
tmp.push_back(edge(bel[i],bel[v]));
}
sort(tmp.begin(),tmp.end());
tmp.resize( unique(tmp.begin(), tmp.end()) - tmp.begin() );
for(auto E: tmp) V[E.u].push_back(E.v);
}
ll f[N],g[N];
bool ret[N];
void dfs(int x,int fa)
{
f[x]=val[x]; if(sz[x]>) ret[x]=;
ll mx=;
for(auto v:V[x])
{
if(v==fa) continue;
dfs(v,x);
if(ret[v]) f[x]+=f[v],ret[x]=;
else mx=max(mx,g[v]);
}
g[x]=f[x]+mx;
for(auto v:V[x])
if(v!=fa&&ret[v])
g[x]=max(g[x], f[x]-f[v]+g[v] );
}
int main()
{
n=read(),m=read(); int x,y;
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=m;i++)
{
x=read(),y=read();
add(x,y); add(y,x);
}
int s=read();
for(int i=;i<=n;i++) if(!dfn[i]) Tarjan(i,);
build();
dfs(bel[s],);
printf("%lld\n",g[bel[s]]);
return ;
}

Codeforces 1220E. Tourism的更多相关文章

  1. CF-diary

    (做题方式:瞟题解然后码) 1238E. Keyboard Purchase \(\texttt{Difficulty:2200}\) 题意 给你一个长度为 \(n\) 的由前 \(m\) 个小写字母 ...

  2. 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 th ...

  3. Codeforces 1220 E Tourism

    题面 可以发现一个边双必然是可以随意走的,所以我们就把原图求割边然后把边双缩成一个点,然后就是一个树上dp了. #include<bits/stdc++.h> #define ll lon ...

  4. Codeforces 杂题集 2.0

      记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序   1326D2 - Prefix-Suffix Palindrome (Hard version) ...

  5. Codeforces Round #635 (Div. 2) 题解

    渭城朝雨浥轻尘,客舍青青柳色新. 劝君更尽一杯酒,西出阳关无故人.--王维 A. Ichihime and Triangle 网址:https://codeforces.com/contest/133 ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. Unity常用API备忘录

    UnityEditor 复制文本到剪切板  GUIUtility.systemCopyBuffer 获取资源路径 AssetDatabase.GetAssetPath 选择节点 Selection.a ...

  2. 【转】Microsoft SQL Server 2008 R2 官方简体中文正式版下载(附激活序列号密钥)

    原文: https://www.bensblog.cn/1238.html

  3. MySQL5.7快速修改表中字段长度

    在mysql 5.5版本时,商用环境升级,有一个表存在六千多万数据,升级时需要修改这个表其中一个varchar类型字段的长度,当时用了大概4个多小时,还没有结束,之后我们系统mysql升级到5.7版本 ...

  4. [Java]算术表达式组建二叉树,再由二叉树得到算式的后序和中序表达式

    Entry类: package com.hy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  5. group_concat() 函数 拼接字符串长度有限制

    最近,在做一个行转列的存储过程,遇到一个问题,问题如下: 我用group_concat()函数 来整合一个月每天的操作量,并将每天的操作量用CONCAT()函数拼接成 “MAX(IF(t.a = '2 ...

  6. LC 890. Find and Replace Pattern

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  7. PHP中获取当前页面的完整URL、PHP URL处理、获取不带扩展名的文件名

    javascript实现: top.location.href 顶级窗口的地址this.location.href 当前窗口的地址 PHP实现 #测试网址: http://localhost/blog ...

  8. ubuntu源与常用python配置pip源(win)、pip常用命令

    pip常用命令 ubuntu更新系统源 首先备份/etc/apt/sources.list mv /etc/apt/sources.list /etc/apt/sources.list.bak 然后下 ...

  9. JPA 或者Hibernate 实体类说明

    这里简单介绍Hibernate的Annotation注解 一.声明实体 @Entity对实体注释.任何Hibernate映射对象都要有这个注释@Table声明此对象映射到数据库的数据表,通过它可以为实 ...

  10. dbgrid中移动焦点到指定的行和

    dbgrid是从TCustomGrid继承下来的,它有col与row属性,只不过是protected的,不能直接访问,要处理一下,可以这样: TDrawGrid(dbgrid1).row:=row;  ...