题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N = 100010, M = 2 * N;
int n;
int h[N], e[M], ne[M], idx;
bool st[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int dfs(int root, int father)
{
int res = 1;
for (int i = h[root]; ~i; i = ne[i])
{
int j = e[i];
if (j != father) res += dfs(j, root);
}
return res;
}
int bfs(int root)
{
int res = 0;
queue<int> q;
q.push(root);
st[root] = true;
while(q.size())
{
auto t = q.front(); q.pop();
res ++;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (!st[j])//入队一次;
{
q.push(j);
st[j] = true;
}
}
}
return res;
}
int main()
{
memset(h, -1, sizeof h);
cin >> n;
for (int i = 0; i < n - 1; i ++)
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
int res = 0;
st[1] = true;
for (int i = h[1]; i != -1; i = ne[i])
{
int j = e[i];
//res = max(res, helper(j, 1));
res = max(res, bfs(j));
}
cout << res;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla