毛毛虫
題目描述
對于一棵樹,我們可以將某條鏈和與該鏈相連的邊抽出來,看上去就象成一個毛毛蟲,點數越多,毛毛蟲就越大。例如下圖左邊的樹(圖 1 )抽出一部分就變成了右邊的一個毛毛蟲了(圖 2 )。
輸入輸出格式
輸入格式:
?
在文本文件 worm.in 中第一行兩個整數 N , M ,分別表示樹中結點個數和樹的邊數。
接下來 M 行,每行兩個整數 a, b 表示點 a 和點 b 有邊連接( a, b ≤ N )。你可以假定沒有一對相同的 (a, b) 會出現一次以上。
?
輸出格式:
?
在文本文件 worm.out 中寫入一個整數 , 表示最大的毛毛蟲的大小。
?
輸入輸出樣例
輸入樣例#1:?復制 13 12 1 2 1 5 1 6 3 2 4 2 5 7 5 8 7 9 7 10 7 11 8 12 8 13 輸出樣例#1:?復制 11說明
40% 的數據, N ≤ 50000
100% 的數據, N ≤ 300000
#include<bits/stdc++.h> #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) using namespace std; const int maxn=3e5+5; template <class T> inline void rd(T &ret){char c;ret = 0;while ((c = getchar()) < '0' || c > '9');while (c >= '0' && c <= '9'){ret = ret * 10 + (c - '0'), c = getchar();} } struct node{int to,nx;}p[maxn<<1]; int n,m,head[maxn],a,b,tot,ans,al[maxn],dp[maxn]; void addedge(int u,int v){p[++tot].to=v,p[tot].nx=head[u],head[u]=tot; } void dfs(int s,int fa){for(int i=head[s];i;i=p[i].nx){if(p[i].to!=fa)al[s]++;}for(int i=head[s];i;i=p[i].nx){int to=p[i].to;if(to==fa)continue;dfs(to,s);ans=max(ans,dp[s]+dp[to]+1+(fa>0));dp[s]=max(dp[s],dp[to]+al[s]);} } int main() {rd(n),rd(m);REP(i,1,m){rd(a),rd(b);addedge(a,b),addedge(b,a);}dfs(1,0);cout<<ans<<endl;return 0; }?
轉載于:https://www.cnblogs.com/czy-power/p/10440915.html
總結
- 上一篇: 你知道css单位fr吗?
- 下一篇: node.js 使用----相关常用命令