Hdoj 4514 JIU JIU series story--Design the scenery line and search the set + non-connected graph to find the maximum diameter of many trees __ and check the collection

Source: Internet
Author: User
The story of JIU JIU series--Design scenery lineTime limit:6000/3000 MS (java/others) Memory limit:65535/32768 K (java/others) Total submission (s): 3482 accepted Submission (s): 610

Problem Description with the popularity of Hangzhou West Lake, landscape planning expert JIU JIU hope to design a new classic sightseeing line, according to the boss Ma Xiaoteng instructions, the new scenic line is best to build a ring, if there is no condition to build a ring, the longer the better.
It has now been explored to determine the n positions that can be used for construction, and the exploration between them also determines the route to be designed by the M strip as well as their length. I wonder if I can build a circular scenery line. If not, how much the landscape can reach.
Among them, the route which can be built is two-way, the length between them is more than 0.

Input test data has multiple groups, the first row of each set of test data has two numbers n, M, its meaning refer to the topic description;
Then the M line, 3 digits per line, U v W, represents the starting point, end point, and length of the line respectively.

   [Technical Specification]
1. n<=100000
2. M <= 1000000
3.1<= u, v <= n
4. W <= 1000

Output for each set of test data, if you can build a ring (do not need to connect to all the scenic spots), then outputs Yes, otherwise output the longest length, each set of data output line.

Sample Input
3 3 1 2 1 2 3 1 3 1 1
Sample Output
YES
The diameter of the tree is not to be asked here.
Train of thought: Judge whether the ring is not said, and check the set directly can be achieved. The difficulty is to find the longest length between any two points, which is that the graph given by the topic may be a non connected graph (that is, not a tree). We can't fool around two times BFS directly to find the diameter of the tree, because the result may not be the largest of all trees. Therefore, we need to enumerate all the trees and find out their diameters.
Note: The topic n Maximum can reach 10W, we can not find each point of the tree's diameter, so the time complexity is too high, will be timed out. Since multiple points may belong to the same tree, we can use an array to mark the diameter of each tree, indicating that the point is in the previous tree and the tree has already asked for the corresponding diameter, thus greatly reducing the complexity of the time.

AC Code: 858MS (the topic data is not super int, I was just in case ...) )

#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define LL long-long #d
Efine MAXN 100000+10 #define MAXM 2000000+100 using namespace std;
    struct Edge {int from, to;
    LL Val;
int next;
};
Edge EDGE[MAXM];
int HEAD[MAXN], edgenum;
LL DIST[MAXN];
BOOL VIS[MAXN];
BOOL used[maxn];//mark the current point has not appeared int PRE[MAXN] in the previous BFS query;
int N, M;
    int NODE;//S-T Endpoint ll temp;//each lookup of the longest long LL ans;//final result bool flag;//to determine whether the ring int find (int p) {int t;
    int child = P;
    while (P!= pre[p]) p = pre[p];
        while (child!= p) {t = Pre[child];
        Pre[child] = p;
    Child = t;
} return p;
    void merge (int x, int y) {int fx = find (x);
    int fy = find (y);
    if (FX!= fy) pre[fx] = FY;
    else flag = true;//cyclization} void init () {for (int i = 1; I <= N; i++) pre[i] = i;
    Edgenum = 0;
Memset (Head,-1, sizeof (head)); } void Addedge (int u, int v, LL w) {Edge E = {u, V, W, Head[u]};
    Edge[edgenum] = E;
Head[u] = edgenum++;
    } void Getmap () {int A, B;
    LL C;
    Flag = false;
        for (int i = 1; I <= M i++) {scanf ("%d%d%lld", &a, &b, &c);
        if (flag) continue;//has been judged cyclization Addedge (A, B, c);
        Addedge (b, A, c);
    Merge (A, b);
    } void SPFA (int sx) {queue<int> Q;
    memset (Dist, 0, sizeof (Dist));
    Memset (Vis, false, sizeof (VIS));
    VIS[SX] = true;
    temp = 0;
    node = SX;
    Q.push (SX); while (!
        Q.empty ()) {int u = q.front ();
        Q.pop (); Used[u] = true;//already appears in the previous BFS query for (int i = head[u]; I!=-1; i = Edge[i].next) {Edge E = Edge
            [i];
                if (!vis[e.to]) {dist[e.to] = Dist[u] + e.val;
                if (dist[e.to] > Temp) temp = dist[e.to], node = e.to;
                Vis[e.to] = true;
            Q.push (e.to);
   }} void Solve () { if (flag)//cyclization {printf ("yes\n");
    return;
    Ans = 0;
    Memset (used, false, sizeof (used)); for (int i = 1; I <= N; i++)//may be a non-connected graph {if (Used[i]) continue;//has appeared in the previous BFS query SPFA (i);//Each node is looking for the corresponding s-t end
    Point SPFA (node);//Find the longest road ans = max (ans, temp);
printf ("%lld\n", ans);
        int main () {while (scanf ("%d%d", &n, &m)!= EOF) {init ();
        Getmap ();
    Solve ();
return 0;
 }



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.