#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. 试题编号: 201903-3 试题名称: 损坏的RAID5

    这题的数据未免也太水了,题目的意思好像默认是每块磁盘装载数据的长度是相等的.我写了判断每次取数据是否会超过每块磁盘存的数据的长度,然而并没有什么卵用.交上去20分,写了个数据测了下,如果要求的块太大的 ...

  2. Java将数据进行分组处理

    将传人的数据进行分组,使用map保存每组的数据. /** * 将取出的数据进行分组 * @param list * @return */ public Map<Integer,Object> ...

  3. 【原】docker-compose 管理docker的多容器配置

    docker-compose管理docker的多容器配置,实现docker的自动化. version: '3.4' x-defaults: &defaults restart: unless- ...

  4. dp(完全背包)

    有 NN 种物品和一个容量是 VV 的背包,每种物品都有无限件可用. 第 ii 种物品的体积是 vivi,价值是 wiwi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大. ...

  5. C语言:找出一个大于给定整数m且紧随m的素数,-求出能整除x且不是偶数的数的个数,

    //函数fun功能:找出一个大于给定整数m且紧随m的素数,并作为函数值返回. #include <stdlib.h> #include <conio.h> #include & ...

  6. 最长递增子序列-Hdu 1257

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. Spring Boot Web 开发@Controller @RestController 使用教程

    在 Spring Boot 中,@Controller 注解是专门用于处理 Http 请求处理的,是以 MVC 为核心的设计思想的控制层.@RestController 则是 @Controller ...

  8. Java面向对象编程 -1.5

    对象引用传递分析 类本身属于引用传递类型,既然是引用传递类型,那么就牵扯到内存的引用传递 所谓的引用传递的本质:同一块堆内存空间可以被不同的栈内存所指向,也可以更换指向. class Person{ ...

  9. ➡️➡️➡️IELTS speaking by simon

    目录 p1 课程概述 p2 speaking part1, intro, warm up introduction questions then 4 questions about one topic ...

  10. Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)

    记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...