标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 533 Accepted Submission(s): 175
#include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <queue> using namespace std; int p, m; const int M = 100005; const int N = 10005; typedef long long ll; struct edge { int v, to; edge() { }; edge(int v, int to): v(v), to(to) {}; } e[M]; int head[N], flag[N], val[N], in[N], vis[N], tot; queue<int> q; void init() { memset(head, -1, sizeof head); memset(flag, 0, sizeof flag); memset(vis, 0, sizeof vis); memset(in, 0, sizeof in); tot = 0; while(!q.empty()) q.pop(); } void addedge(int u, int v) { e[tot] = edge(v, head[u]); head[u] = tot++; } void dfs(int u) { for(int i = head[u]; i != -1; i = e[i].to) { int v = e[i].v; if(in[v] == 0) continue; if(flag[v]) continue; in[v]--; in[u]--; if(in[v] == 1) { q.push(v); flag[v] = 1; } } } void pre() { for(int i = 1; i <= p; ++i) { if(in[i] == 1) { flag[i] = 1; q.push(i); } } while(!q.empty()) { int f = q.front(); q.pop(); dfs(f); } } int cnt; ll sum; void calc(int u) { cnt++; vis[u] = 1; sum += val[u]; for(int i = head[u]; i != -1; i = e[i].to) { int v = e[i].v; if(vis[v]) continue; if(flag[v]) continue; calc(v); } } int main() { int _; scanf("%d", &_); while(_ --) { scanf("%d%d", &p, &m); int u, v; for(int i = 1; i <= p; ++i) scanf("%d", &val[i]); init(); for(int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); in[u]++; in[v]++; addedge(u, v); addedge(v, u); } pre(); ll ans = 0; for(int i = 1; i <= p; ++i) if(!flag[i] && !vis[i]) { cnt = 0; sum = 0; calc(i); if((cnt & 1) && cnt !=1) ans += sum; } printf("%lld\n", ans); } return 0; }
hdu5438 Ponds dfs 2015changchun网络赛
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4805793.html