传送门
题意
分析
如果
n
n
n是偶数的话,因为是一个连通图,所以我可以一个边都不删,最大权值就是所有点的权值和
如果
n
n
n是奇数的话,就需要删除一个点了,但需要注意的话,删除一个点的话,需要把和这条边链接的所有边全部删除,所有如果是割点的话,就会把这张连通图分割成几个部分,所以我们要判断一个割点,并在
d
f
s
dfs
dfs的过程中维护一下子树大小
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N], ne[N * 2], e[N * 2], idx;
ll a[N];
int n, m;
int dfn[N], low[N], ti;
int root;
ll ans[N];
ll sz[N];
bool ok[N];
bool st[N];
void init() {
for (int i = 1; i <= n; i++) h[i] = -1, dfn[i] = low[i] = 0, st[i] = 0, ok[i] = 0;
idx = ti = 0;
}
void add(int x, int y) {
ne[idx] = h[x], e[idx] = y, h[x] = idx++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++ti;
int cnt = 0;
sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!dfn[j]) {
tarjan(j);
sz[u] += sz[j];
low[u] = min(low[u], low[j]);
if (low[j] >= dfn[u]) {
cnt++;
if (u != root || cnt > 1) st[u] = true;
if (sz[j] & 1) ok[u] = 1;
}
}
else low[u] = min(low[u], dfn[j]);
}
}
void solve() {
read(n), read(m);
init();
ll sum = 0;
for (int i = 1; i <= n; i++) read(a[i]), sum += a[i];
while (m--) {
int x, y;
read(x), read(y);
add(x, y), add(y, x);
}
if ((n & 1) == 0) {
dl(sum);
return;
}
for (root = 1; root <= n; root++) {
if (!dfn[root])
tarjan(root);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (!st[i]) ans = max(ans, sum - 2 * a[i]);
else if (!ok[i]) ans = max(ans, sum - 2 * a[i]);
}
dl(ans);
}
int main() {
int T;
read(T);
while (T--) {
solve();
}
return 0;
}