Codeforces Round # 586 (Div. 1 + Div. 2) E (topological sort, thinking)

#include <bits / STDC ++ H.>
the using namespace STD;
int n-, m, S;
Vector <int> Edge [200007];
Queue <int> Leaf;
int weight [200007], Degree [200007];
Long Long Wei [ 200 007]; // current point total search deep down heavy chain (excluding the current node, the current node has a weight by weight into account)
int main () {
iOS :: sync_with_stdio (to 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] == I. 1 &&!= s) // if s is a leaf, the weight of this chain ring will be calculated on the
leaf.push (i);
the while (! leaf.empty ()) {
int I = leaf.front ();
leaf.pop ();
Degree [I] = -. 1; // to avoid visiting
for (int j = 0; j <edge [i ] .size (); J ++) {
IF (Degree [Edge [I] [J]] <0) // isolated node
Continue;
--degree [Edge [I] [J]]; // access after demolition edge
wei [edge [i] [j ]] = max (wei [edge [i] [j]], wei [i] + weight [i]); // update the current node maximum selectable chain
IF (Degree [Edge [I] [J]] ==. 1 && Edge [I] [J] = S!)
leaf.push (Edge [I] [J]);
}
}
Long Long ANS = 0; // ring the total weight of
long long mx = 0; // chain maximum weight
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;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11573804.html