#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. python小白的爬虫之旅

    1.爬www.haha56.net/main/youmo网站的内容 ieimport requests import re response=requests.get("http://www ...

  2. spring框架相关概念

    软件行业的二八法则?技术中只有20%是最常用和最关键的,决定你的基础,后面的80%决定你的潜能! 概念: 1,轻量级框架,用户需要什么功能就自己添加相应的功能模块,不像重量级框架,一旦用,所有功能都添 ...

  3. CSS选择器效率问题

    今天学习了CSS各类选择器,对其效率问题有些疑问,故总结了一些学习笔记 有很多人都忘记了,或在简单的说没有意识到,CSS在我们手中,既能很高效,也可以变得很低能.这很容易被忘记,尤其是当你意识到你会的 ...

  4. phpstorm问题

    主题 PreferencesPreferencesPreferences 重要的事情说3边,而不是default setting Preferences->Appearance & Be ...

  5. oracle查询表统计行数与注释

    SELECT TABLE_NAME,NUM_ROWS,(select COMMENTS from user_tab_comments WHERE TABLE_NAME=C.TABLE_NAME) FR ...

  6. Android 调起系统相机拍照

    概述 最近在看 nanChen 写的图片选择器 ImagePicker,感觉写得很不错,也打算把从中学到的东西写下来.很多时候,遇到一个好的框架能够降低开发成本这是好事.但是也要去了解其内部具体实现逻 ...

  7. Javascript——(1)

    1.Javascript有两种解释表示形式:1)在html的<header>中写<script><script/>,另一种是将另一个文件保存为xxx.js文档,然后 ...

  8. Python3.5学习之旅——day6

    面向对象编程的学习 一.定义 首先跟大家介绍一位资深的程序员前辈说过的编程心得: 1.写重复代码是非常不好且低级的行为 2.完成的代码需要经常变更 所以根据以上两个心得,我们可以知道写的代码一定要遵循 ...

  9. Ubuntu各个版本的镜像下载地址

    http://mirrors.melbourne.co.uk/ubuntu-releases/

  10. Passive Client Feature

    Q. How is the passive client feature used on Wireless LAN Controllers? A. Passive clients are wirele ...