Topic link: http://acm.hdu.edu.cn/showproblem.php?pid=4514
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Problem Description
With the further improvement of the popularity of Hangzhou West Lake, Qiuqiu, a garden planning expert, hopes to design a new classic sightseeing route. According to the instructions of the boss Ma Xiaoteng, the new scenic route should preferably be built in a circle. If there is no condition to build a circle, then it will be built The longer the better.
Now that the survey has determined n locations that can be used for construction, and between them, m possible routes and their lengths have been surveyed and determined. Is it possible to build a circular landscape? If not, how long can the landscape be the longest?
Among them, the routes that can be constructed are all bidirectional, and the length between them is greater than 0.
Now that the survey has determined n locations that can be used for construction, and between them, m possible routes and their lengths have been surveyed and determined. Is it possible to build a circular landscape? If not, how long can the landscape be the longest?
Among them, the routes that can be constructed are all bidirectional, and the length between them is greater than 0.
Input
There are multiple sets of test data, and the first line of each set of test data has two numbers n, m, the meaning of which is shown in the title description;
the next m lines, each line has three numbers uvw, representing the starting point, end point and length.
[Technical Specification]
1. n<=100000
2. m <= 1000000
3. 1<= u, v <= n
4. w <= 1000
the next m lines, each line has three numbers uvw, representing the starting point, end point and length.
[Technical Specification]
1. n<=100000
2. m <= 1000000
3. 1<= u, v <= n
4. w <= 1000
Output
For each set of test data, if a ring can be built (it is not necessary to connect all the scenic spots), then output YES, otherwise output the longest length, and output one line for each set of data.
Sample Input
3 3
1 2 1
2 3 1
3 1 1
Sample Output
YES
answer:
And check the undirected graph ring of the set judgment, this is a very simple application of the union search template;
Just need to pay attention, if there is a ring, stop adding edges to the adjacency list, otherwise it will be MLE;
Tree DP finds the diameter of the tree, this is also a template application,
The only caveat is that it is possible to have more than one connected component.
AC code:
#include<bits/stdc++.h> using namespace std; const int maxn=100000+10; int n,m; struct Edge{ int u,v,w; Edge(int u,int v,int w){this->u=u,this->v=v,this->w=w;} }; vector<Edge> E; int E_size; vector<int> G[maxn]; void adjListInit(int l,int r) { E.clear(); E_size=0; for(int i=l;i<=r;i++) G[i].clear(); } void addEdge(int u,int v,int w) { E.push_back(Edge(u,v,w)); E_size++; E.push_back(Edge(u,v,w)); E_size++; G[u].push_back(E_size-2); G[v].push_back(E_size-1); } int par[maxn]; void UFSinit(int l,int r){for(int i=l;i<=r;i++) par[i]=i;} int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} void unite(int x,int y) { x=find(x),y=find(y); if(x==y) return; par[y] = x; } inline bool isSame(int x,int y){return find(x)==find(y);} int diameter,dp[maxn][2]; bool vis[maxn]; void dfs(int now,int par) { vis[now]=1; for(int i=0;i<G[now].size();i++) { Edge &e=E[G[now][i]]; int nxt=e.v; if(vis[nxt]) continue; dfs(nxt,now); if (dp[now][ 0 ] < dp[nxt][ 0 ]+ew) // ( "the largest of its children" + "the distance from its children" ) > "largest" > "second largest" { dp[now][1] = dp[now][0]; dp[now][0] = dp[nxt][0] + e.w; } else if (dp[now][ 1 ] < dp[nxt][ 0 ]+ew) // "largest" > ( "the largest of one of its children" + "the distance from its child" ) > "the next largest" { dp[now][1] = dp[nxt][0]+e.w; } } if(diameter<dp[now][0]+dp[now][1]) diameter=dp[now][0]+dp[now][1]; } int years; intmain () { while(scanf("%d%d",&n,&m)!=EOF) { bool haveRing=0; adjListInit(1,n); UFSinit(1,n); for(int i=1,u,v,w;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); if(haveRing) continue; addEdge(u,v,w); if(!isSame(u,v)) unite(u,v); else haveRing=1; } if(haveRing) { printf("YES\n"); continue; } memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); ans=0; for(int i=1;i<=n;i++) { if(vis[i]) continue; diameter=0; dfs(i,0); years = max(diameter,years); } printf("%d\n",ans); } }