zoukankan      html  css  js  c++  java
  • [树直径] [POJ] CowMarathon

    时间复杂度超高

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
     
    const int MAXN = 1e6 + 10;
     
    bool used[MAXN] = {0};
     
    vector < pair <int, int> > arr[MAXN];
     
    int lma, lmp, lb = 0, beg;
     
    void findpoint(int now, int step)
    {
        if(!used[now])
            used[now] = true;
        else
            return;   
        if(step > lma)
            lma = step, lmp = now;
        for(unsigned int i = 0; i < arr[now].size(); i++)
        {
            findpoint(arr[now][i].first, step + arr[now][i].second);
        }
    }
     
    void largeway(int now, int step)
    {
        if(!used[now])
            used[now] = true;
        else
            return ;
        if(step > lb)
            lb = step;
        for(unsigned int i = 0; i < arr[now].size(); i++)
        {
            largeway(arr[now][i].first, step + arr[now][i].second);
        }
    }
    
    void init(int x, int y, int z)
    {
        beg = x;
        arr[x].push_back(make_pair(y, z)), arr[y].push_back(make_pair(x, z));
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
     
        cin.tie(0);     cout.tie(0);
     
        int N, M;
     
        cin>>M>>N;
     
        while(N--)
        {
            int x, y, z;
            char c;
            cin>>x>>y>>z>>c;
            init(x, y, z);
        }
    
        memset(used, 0, sizeof(used));
        
        lma = 0, lmp = beg;
        
        findpoint(beg, 0);
        
        memset(used, 0, sizeof(used));
        
        largeway(lmp, 0);
     
        cout<<lb<<endl;
     
        return 0;
    }
  • 相关阅读:
    【学习笔记 2】单调队列 & 单调栈
    【学习笔记 1】快速幂
    题解P1151
    题解 P6161【[Cnoi2020]高维】
    不知道叫啥的题目1
    神秘题目1
    5.30 模拟赛赛后总结
    矩阵乘法加速图上问题专题总结
    点分治&点分树 复习
    5.26赛后总结
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270450.html
Copyright © 2011-2022 走看看