Uvalive 4848 tour Belt

Source: Internet
Author: User
F-tour Belt Time limit:3000 Ms Memory limit:0 KB 64bit Io format:% LLD & % llusubmit status practice uvalive 4848

Description

Korea has implements tourist attractions. one of them is an archipelago (dadohae in Korean), a cluster of small islands scattered in the southern and western coasts of Korea. the Korea Tourism Organization (Kto) plans to promote a new tour program on these islands. for this, the Kto wants to designate two or more islands of the archipelago as a tour belt.

There areNIslands in the archipelago. The Kto focuses on the synergy effect created when some islands are selected for a tour belt. synergy effects are assigned to several pairs of islands. A synergy effectSe(U,V) OrSe(V,U) Between two islandsUAndVIs a positive integer which represents a value anticipated when bothUAndVAre supported ded in a tour belt. The Kto wants to select two or more islands for the tour belt so that the economic benefit of the tour belt is as high as possible.

To be precise, we define a connected graphG= (V,E), WhereVIs a setNVertices andEIs a setMEdges. Each vertexVRepresents an island in the archipelago, and an edge (U,V)EExists if a synergy effectSe(U,V) Is defined between two distinct vertices (Islands)UAndVOfV. LetABe a subset consisting of at least two vertices inV. An edge (U,V) Is an inside edgeAIf bothUAndVAre inA. An edge (U,V) Is a border edgeAIf oneUAndVIs inAAnd the other is not inA.

A vertex setBOf a connected subgraphGWith 2 |B|NIs called a candidate for the tour belt if the synergy effect of every inside edgeBIs larger than the synergy effect of any border edgeB. A candiate will be chosen as the final tour belt by the Kto. There can be either possible candidates inG. Note thatVItself is a candidate because there are no border edges. the graph in figure 1 (a) has three candidates {1, 2}, {3, 4} and {1, 2, 3}, but {2, 3, 3, 4} is not a candidate because there are inside edges whose synergy effects are not larger than those of some border edges. the graph in figure 1 (B) contains six candidates, {}, {3, 4}, {5, 6}, {7, 8}, {3, 4, 5, 6} and {1, 2, 3, 4, 5, 6, 7, 8 }. but {1, 2, 7, 8} is not a candidate because it does not form a connected subgraphG, I. e., there are no edges connecting {1, 2} and {7, 8 }.

 

 

Figure 1. Graphs and their good subsets marked by gray ellipses.

The Kto will decide one candidate inGAs the final tour belt. For this, the Kto asks you want to find all candidates inG. You write a program to print the sum of the sizes of all candidates in a given graphG. For example, the graph in figure 1 (a) contains three candidates and the sum of their sizes is 2 + 2 + 4 = 8, and the graph in figure 1 (B) contains six candidates and the sum of their sizes is 2 + 2 + 2 + 2 + 4 + 8 = 20.

 

Input

Your program is to read input from standard input. The input consistsTTest cases. The number of test casesTIs given in the first line of the Input. Each test case starts with a line containing two integersN(2N5,000) andM(1M), WhereNRepresents the number of vertices (Islands) andMRepresents the number of edges of a connected graphG. Islands are numbered from 1N. In the followingMLines, the synergy effects assignedMEdges are given; each line contains three integers,U,V, AndK(1UVN, 1K105), whereKIs the synergy effect between two distinct islandsUAndV, I. e .,Se(U,V) =Se(U,V) =K.

 

Output

Your program is to write to standard output. Print exactly one line for each test case. Print the sum of the sizes of all candidates for a test case.

The following shows sample input and output for two test cases.

 

Sample Input

 

24 61 2 32 3 24 3 41 4 12 4 21 3 28 71 2 22 3 13 4 44 5 35 6 46 7 17 8 2

 

Sample output

 

820

Find a connected subgraph in a connected undirected graph. If the minimum edge weight in the connected subgraph is greater
The maximum number of connected edges of subgraphs and non-subgraphs. ans + = number of connected subgraphs. Output ans.
Train of Thought: sort by edge from large to small, and then add them one by one. If the two vertices of the original edge are connected, ignore it.
If it is not connected, determine whether the conditions are met (purely violent ..)

#include<cstdio>#include<string>#include<cstring>#include<vector>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<algorithm>#define INF 0x3f3f3f3f#define FINF 1e9#define ll long long#define BUG printf("hehe!~\n")using namespace std;const int N=5010;const int M=22501000;struct Edge{    int u,v,w;}edge[M];bool cmp(Edge A,Edge B){    return A.w>B.w;}int cur[N+N];int n,m;int fa[N];int findp(int x) {    return fa[x]==x?x:fa[x]=findp(fa[x]);}void init(){    for(int i=0;i<=n;++i) fa[i]=i,cur[i]=1;}int connect(int fu,int fv){    cur[fu]+=cur[fv];    return cur[fu];}int main(){    int _;    cin>>_;    int u,v,w;    int x,y,num;    bool flag;    int ans,cnt;    while(_--) {        scanf("%d%d",&n,&m);        init();        ans=0;        for(int i=0;i<m;++i) {            scanf("%d%d%d",&u,&v,&w);            edge[i].u=u, edge[i].v=v, edge[i].w=w;        }        sort(edge,edge+m,cmp);        num=n;        for(int i=0;i<m;++i) {            u=edge[i].u,v=edge[i].v,w=edge[i].w;            int fu=findp(u);            int fv=findp(v);            if(fu!=fv) {                num++;                fa[fv]=num;                fa[fu]=num;                fa[num]=num;                flag=true;                cur[num]=cur[fv]+cur[fu];                cnt=cur[num];                int mn=INF,mx=-INF;                for(int j=0;j<m;++j) {                    Edge& e=edge[j];                    x=findp(e.u);y=findp(e.v);                    if(x==y&&x==num) mn=min(mn,e.w);                    else if(x==num||y==num) {                        mx=max(mx,e.w);                    }                }                if(mn>mx) {                    ans+=cnt;                }            }        }        printf("%d\n",ans);    }    return 0;}
View code

 

Uvalive 4848 tour Belt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.