1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| #include<stdio.h>
#include<string.h>
int n,m;
int t,res;
int head[100200];
int headcnt;
struct List {
int u,v,w;
int next;
} edge[100200];
void dfs(int u,int v,int w) {
if(w>res) {
res=w;
t=u;
}
for(int i=head[u]; i!=-1; i=edge[i].next) {
if(edge[i].v!=v) {
dfs(edge[i].v,u,w+edge[i].w);
}
}
}
void add(int u,int v,int w) {
edge[headcnt].u=u;
edge[headcnt].v=v;
edge[headcnt].w=w;
edge[headcnt].next=head[u];
head[u]=headcnt++;
}
int main() {
while(scanf("%d %d",&n,&m)!=EOF) {
headcnt=0;
memset(head,-1,sizeof(head));
while(m--) {
int u,v,w;
scanf("%d %d %d %*c",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
res=0;
dfs(1,0,0);
dfs(t,0,0);
printf("%d\n",res);
}
return 0;
}
|