Pseudo
Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1705 accepted submission (s): 653
Problem description
In graph theory, a pseudo forest is an undirected graph in which every connected component has at most one cycle. the maximal pseudo doforests of G are the pseudo forest subgraphs of G that are not contained within any larger pseudo doforest of G. A pesudoforest is larger than another if and only if the total value of the edges is greater than another one's.
Input
The input consists of multiple test cases. the first line of each test case contains two integers, n (0 <n <= 10000), m (0 <= m <= 100000 ), which are the number of the vertexes and the number of the edges. the next M lines, each line consists of three integers, U, V, C, which means there is an edge with value C (0 <C <= 10000) between u and v. you can assume that there are no loop and no multiple edges.
The last test case is followed by a line containing two zeros, which means the end of the input.
Output
Output the sum of the value of the edges of the maximum pesudoforest.
Sample Input
3 30 1 11 2 12 0 14 50 1 11 2 12 3 13 0 10 2 20 0
Sample output
35
Question: n vertices, give you n edges and require you to connect some edges. The sum of edge weights is the largest (the entire graph can be unconnected, and each connected subgraph can have only one ring)
: According to the maximum spanning tree method, a connection can be established as long as the two nodes connecting to the same connected subgraph determine whether the subgraph has a ring.
The two nodes connecting different connected subgraphs are used to determine whether the sum of the rings of the two connected subgraphs is less than 2. The connected subgraphs can be connected when the conditions are met.
view code#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;#define REP(i,n) for(int i=0; i<(n); i++)const int N = 10010;int n, m, fa[N], num[N];struct edge{ int u, v, w; bool operator < (const edge &o) const { return w>o.w; }}e[N*10];int find(int x){ return x==fa[x]?x:(fa[x]=find(fa[x]));}void solve(){ REP(i,n) fa[i] = i, num[i] = 0; REP(i,m) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); sort(e, e+m); int ans = 0; REP(i, m) { int u = find(e[i].u), v =find(e[i].v); if(u==v && num[v]==0) ans += e[i].w, num[v]++; else if(u!=v && num[v]+num[u]<2) fa[u] = v, num[v]+= num[u], ans += e[i].w; } printf("%d\n", ans);}int main(){// freopen("in.txt", "r", stdin); while(cin>>n>>m &&(n|m) ) solve(); return 0;}