【算法】树的直径
memset(a,0,sizeof(a))
#include#include #include using namespace std;const int maxn=10010;struct edge{ int from,v,w;}e[maxn*2];int tot,X,n,head,tail,q[maxn],d[maxn],first[maxn];bool v[maxn];void insert(int u,int v,int w){tot++;e[tot].v=v;e[tot].w=w;e[tot].from=first[u];first[u]=tot;}void bfs(int s){ X=0; memset(d,0,sizeof(d)); memset(v,0,sizeof(v));//数组,值,范围 head=1;tail=1;q[1]=s;v[s]=1; while(head<=tail) { int u=q[head++]; if(d[u]>d[X])X=u; for(int i=first[u];i;i=e[i].from) if(!v[e[i].v]) { v[e[i].v]=1; d[e[i].v]=d[u]+e[i].w; q[++tail]=e[i].v; } }}int main(){ scanf("%d",&n); for(int i=1;i