标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2358 | Accepted: 1156 |
Description
Input
Output
Sample Input
5 1 6 1 4 5 6 3 9 2 6 8 6 1 7
Sample Output
22
裸树的直径有木有!!!
#include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue> #define maxn 40000+10 #define maxm 80000+10 using namespace std; int ed, sum; int dist[maxn], vis[maxn]; int head[maxn], cnt; struct node { int u, v, w, next; }; node edge[maxm]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w){ edge[cnt] = {u, v, w, head[u]}; head[u] = cnt++; edge[cnt] = {v, u, w, head[v]}; head[v] = cnt++; } void bfs(int sx){ queue<int>q; memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); ed = sx; sum = 0; vis[sx] = 1; q.push(sx); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; if(!vis[v]){ dist[v] = dist[u] + edge[i].w; if(sum < dist[v]){ ed = v; sum = dist[v]; } vis[v] = 1; q.push(v); } } } } int main (){ int a, b, c; init(); while(scanf("%d%d%d", &a, &b, &c) != EOF) add(a, b, c); bfs(1); bfs(ed); printf("%d\n", sum); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2631 -- Roads in the North【树的直径 && 裸题】
标签:
原文地址:http://blog.csdn.net/hpuhjh/article/details/47728449