/* time:O(unknown) space:O(n*n) knowledge:树的直径 step:dfs*2 */ #include<bits/stdc++.h> using namespace std; int n; vector<int> t[105]; bool vis[105]; int s,len; void dfs(int x,int d){ vis[x]=true; if(len<d){ s=x; len=d; } for(int i=0;i<t[x].size();i++){ int o=t[x][i]; if(vis[o]==false){ dfs(o,d+1); } } } int main(){ cin>>n; for(int i=1;i<n;i++){ int x,y; cin>>x>>y; t[x].push_back(y); t[y].push_back(x); } memset(vis,0,sizeof(vis)); dfs(1,0); memset(vis,0,sizeof(vis)); dfs(s,0); cout<<len; return 0; } /* 提交时间:202311242117 解题时间:202311242045-202311242117 测试数据: in1: 6 1 2 3 2 5 6 2 4 5 2 out1: 3 in2: 6 1 2 1 3 1 4 1 5 1 6 out2: 2 in3: 6 1 2 1 3 2 4 2 5 4 6 out3: 4 in4: 6 2 1 1 3 2 5 2 6 2 4 out4: 3 in5: 6 1 2 2 3 3 4 4 5 5 6 out5: 5 */
标签:20231124,int,memset,back,dfs,vis From: https://www.cnblogs.com/zangqy/p/17854795.html