#include<bits/stdc++.h>
using namespace std;
int n,m,s;
vector<int>edge[200007];
queue<int>leaf;
int weight[200007],degree[200007];
long long wei[200007];//当前点深搜下去的链的总重(不包括当前结点,当前结点权重通过weight已经计算在内)
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=1;i<=n;++i)
cin>>weight[i];
for(int i=1;i<=m;++i){
int u,v;
cin>>u>>v;
edge[u].push_back(v);
edge[v].push_back(u);
++degree[u];
++degree[v];
}
cin>>s;
for(int i=1;i<=n;++i)
if(degree[i]==1&&i!=s)//如果s也是叶子,这条链的重量会计算在环内
leaf.push(i);
while(!leaf.empty()){
int i=leaf.front();
leaf.pop();
degree[i]=-1;//避免重复访问
for(int j=0;j<edge[i].size();++j){
if(degree[edge[i][j]]<0)//孤立结点
continue;
--degree[edge[i][j]];//访问过后拆边
wei[edge[i][j]]=max(wei[edge[i][j]],wei[i]+weight[i]);//更新当前结点可选择的链的最大值
if(degree[edge[i][j]]==1&&edge[i][j]!=s)
leaf.push(edge[i][j]);
}
}
long long ans=0;//环的总重
long long mx=0;//最大值的链的重量
for(int i=1;i<=n;++i){
if(degree[i]<0)
continue;
ans+=weight[i];
mx=max(mx,wei[i]);
}
cout<<ans+mx;
return 0;
}

Codeforces Round #586 (Div. 1 + Div. 2)E(拓扑排序,思维)的更多相关文章

  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. weinre 真机调试

    1.在任意文件夹 安装weinre cnpm -g install weinre 2.启动 weinre --httpPort 8009 --boundHost -all- 3.在你的页面中加入 本机 ...

  2. STM32F103之I2C学习记录

    26.3.1  模式选择 该外设可以在以下四种模式之一 1)从机发送模式 2)从机接收模式 3)主机发送模式 4)主机接收模式 IIC协议时序 MSB:Most Significant Bit(最高有 ...

  3. Bugku-CTF加密篇之贝斯家族(@iH<,{bdR2H;i6*Tm,Wx2izpx2!)

    贝斯家族 @iH<,{bdR2H;i6*Tm,Wx2izpx2!  

  4. MyBatis XML常用配置

    1.属性(properties) 可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递. 可以外部定义好properties文件通过 ...

  5. WLC配置LAG

    在一般的网络环境中,为了让网络更加的可靠,冗余性更好,会对WLC和对端的设备配置LAG. 如下是一个基本的示例topo(VSS的情况下建议的连接方式): 一般情况下的连接方式: 一般来说配置比较简单, ...

  6. 【JavaWeb】Spring相关错误记录

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...

  7. Spring Boot中Restful Api的异常统一处理

    我们在用Spring Boot去向前端提供Restful Api接口时,经常会遇到接口处理异常的情况,产生异常的可能原因是参数错误,空指针异常,SQL执行错误等等. 当发生这些异常时,Spring B ...

  8. 跨域-CORS

    跨域:是浏览器为了安全而做出的限制策略 浏览器请求必须遵循同源策略:同域名,同端口,同协议 cors跨域- 服务器端设置,前端直接调用 说明:后台允许前端某个站点进行访问 后台设置 Access-Co ...

  9. C++中宏的定义与用法(现已被内联函数所代替)

    在noip中,宏还是被经常采用,所以这里讲一下,C++中宏的定义与用法 第一种用法——配合条件编译:#define DEBUG 定义一个叫DEBUG的标识符.它应该与#ifdef或#ifndef配合使 ...

  10. php中截取中文不乱吗

    php截取中文的使用是随处可见的,譬如,博客首页显示简介,可能会用到,或一些相册简介会用到,以前不知道,还傻傻的自己去写函数用来做“智能截取”,效果还不十分好,幸运的是,今天因为一位同学做项目,让我一 ...