随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好。 
  现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度。请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少? 
  其中,可以兴建的路线均是双向的,他们之间的长度均大于0。 

Input  测试数据有多组,每组测试数据的第一行有两个数字n, m,其含义参见题目描述; 
  接下去m行,每行3个数字u v w,分别代表这条线路的起点,终点和长度。 

   TechnicalSpecification 
  1. n<=100000 
  2. m <= 1000000 
  3. 1<= u, v <= n 
  4. w <= 1000 
Output  对于每组测试数据,如果能够建成环形(并不需要连接上去全部的风景点),那么输出YES,否则输出最长的长度,每组数据输出一行。 
Sample Input

3 3
1 2 1
2 3 1
3 1 1

Sample Output

YES

 

 

求树的直径,用再次bfs。证明见:树的直径(最长路) 的详细证明(转)

首先先判环,如果有环直接输出YES,用并查集就好。如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存,

还有用C++交用的少一些,我用G++交的卡在32764K,限制是32768K。。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <cstdio>
  3 #include <string>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <iostream>
  7 #include <cstring>
  8 #include <set>
  9 #include <queue>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <map>
 13 #include <cctype>
 14 #include <cmath>
 15 #include <stack>
 16 //#include <tr1/unordered_map>
 17 #define freopenr freopen("in.txt", "r", stdin)
 18 #define freopenw freopen("out.txt", "w", stdout)
 19 using namespace std;
 20 //using namespace std :: tr1;
 21  
 22 typedef long long LL;
 23 typedef pair<int, int> P;
 24 const int INF = 0x3f3f3f3f;
 25 const double inf = 0x3f3f3f3f3f3f;
 26 const LL LNF = 0x3f3f3f3f3f3f;
 27 const double PI = acos(-1.0);
 28 const double eps = 1e-8;
 29 const int maxn = 1e5 + 5;
 30 const LL mod = 10000000000007;
 31 const int N = 1e6 + 5;
 32 const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
 33 const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
 34 const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
 35 inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
 36 int n, m;
 37 const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 38 const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 39 inline int Min(int a, int b){ return a < b ? a : b; }
 40 inline int Max(int a, int b){ return a > b ? a : b; }
 41 inline LL Min(LL a, LL b){ return a < b ? a : b; }
 42 inline LL Max(LL a, LL b){ return a > b ? a : b; }
 43 inline bool is_in(int r, int c){
 44     return r >= 0 && r < n && c >= 0 && c < m;
 45 }
 46 vector<P> G[maxn];
 47 int p[maxn], dp[maxn];
 48 bool vis[maxn], viss[maxn];
 49  
 50 int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]);  }
 51  
 52 int bfs(int root){
 53     memset(vis, false, sizeof vis);
 54     memset(dp, 0, sizeof dp);
 55     queue<int> q;
 56     q.push(root);
 57     vis[root] = viss[root] = true;
 58     int ans = root, maxx = 0;
 59  
 60     while(!q.empty()){
 61         int u = q.front();  q.pop();
 62         for(int i = 0; i < G[u].size(); ++i){
 63             P p = G[u][i];
 64             int v = p.first;
 65             int w = p.second;
 66             if(vis[v])  continue;
 67             vis[v] = viss[v] = true;
 68             dp[v] = dp[u] + w;
 69             if(maxx < dp[v]){
 70                 maxx = dp[v];
 71                 ans = v;
 72             }
 73             q.push(v);
 74         }
 75     }
 76     return ans;
 77 }
 78  
 79 int solve(int root){
 80     int u = bfs(root);
 81     int v = bfs(u);
 82     return dp[v];
 83 }
 84  
 85 int main(){
 86     while(scanf("%d %d", &n, &m) == 2){
 87         int u, v, c;
 88         for(int i = 1; i <= n; ++i)  G[i].clear(), p[i] = i;
 89         bool ok = false;
 90         for(int i = 0; i < m; ++i){
 91             scanf("%d %d %d", &u, &v, &c);
 92             int x = Find(u);
 93             int y = Find(v);
 94             if(x != y)  p[y] = x;
 95             else ok = true;
 96             G[u].push_back(P(v, c));
 97             G[v].push_back(P(u, c));
 98         }
 99         if(ok){  puts("YES");  continue; }
100  
101         memset(viss, false, sizeof viss);
102         int ans = 0;
103         for(int i = 1; i <= n; ++i)
104             if(!viss[i])  ans = Max(ans , solve(i));
105         printf("%d\n", ans);
106     }
107     return 0;
108 }
View Code

相关文章:

  • 2022-12-23
  • 2022-02-26
  • 2021-06-08
  • 2022-02-19
  • 2021-06-02
  • 2021-12-14
  • 2022-03-02
  • 2021-05-17
猜你喜欢
  • 2021-07-30
  • 2022-12-23
  • 2021-08-08
  • 2021-08-04
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
相关资源
相似解决方案