计算树的直径
树为无向树,树的直径即为树的最长简单路。
- 随便找一个源点,bfs求出每个点相对源点的距离
- 找出所有点中距离的最大值,并记录最大值所在点
- 以距离最大的点为起点第二次bfs
- 求出第二次bfs后的最大距离和这个点,即为答案。
复杂度:2*O(V+E)
代码
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int MV=1005;
const int ME=200005;
/*
* 空间复杂度比vector实现大
* 链式前向星存图,基于广度优先遍历
* 无向无权图
* 增加顶点的结构体存每个顶点的深度和parent等信息
* parent用于构造bfs树
*/
struct EDGE{
int next, to, from;
};//d为深度,w为权重
int tot = 1; //为了双向边异或顺利进行,边的下标从0开始
EDGE edge[ME]; //edge代表边集
struct VERT{
int h, d, p;
};//h为起始边,d为点的深度,p为bfs树中的父节点
VERT vert[MV]; //head代表点集
bool vis[MV];
int V, E;
/*
*无权无向图,每次调用生成两条边
*/
void add(int u, int v)
{
edge[++tot].next = vert[u].h;//tot从1开始,故若某next==0,可认为到终点了
edge[tot].to = v;
edge[tot].from = u;
vert[u].h = tot;
edge[++tot].next = vert[v].h;//tot从1开始,故若某next==0,可认为到终点了
edge[tot].to = u;
edge[tot].from = v;
vert[v].h = tot;
}
/*
*计算树的直径
*/
bool bfs(int s)
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vert[s].d = 0;
vis[s] = true;
int v,u;
while(!Q.empty()) {
u = Q.front(); Q.pop();
for(int i = vert[u].h; i; i = edge[i].next){
v = edge[i].to;
if(!vis[v]) {
vis[v] = true;
vert[v].p = u; //方便输出路径
vert[v].d = vert[u].d + 1;
Q.push(v);
}
}
}
}
int main()
{
cin >> V >> E;
int a, b;
for(int i = 1; i <= E; i++) {
cin >> a >> b;
add(a,b);
}
bfs(1); //为了统计最长路的端点,随便选一个源
int max = -INF, k1, k2;
for(int i = 1; i <= V; i++) {
if(vert[i].d > max) {
max = vert[i].d;
k1 = i;
}
}
bfs(k1);
max = -INF;
for(int i = 1; i <= V; i++) {
if(vert[i].d > max) {
max = vert[i].d;
k2 = i;
}
}
cout << "The tree's diameter is from " <<k1 << " to " << k2 << ":" << max << endl;
}
判断是否为二分图
代码
/*
* 链式前向星存图
*/
const int MV=10005;
const int ME=10005;
struct EDGE{
int next, to, from;
};//d为深度,w为权重
int tot = 1; //为了双向边异或顺利进行,边的下标从0开始
EDGE edge[ME]; //edge代表边集
struct VERT{
int h, d, p;
};//h为起始边,d为点的深度
VERT vert[MV]; //head代表点集
bool vis[MV];
int V,E;
void add(int u, int v)
{
edge[++tot].next = vert[u].h;//tot从1开始,故若某next==0,可认为到终点了
edge[tot].to = v;
edge[tot].from = u;
vert[u].h = tot;
edge[++tot].next = vert[v].h;//tot从1开始,故若某next==0,可认为到终点了
edge[tot].to = u;
edge[tot].from = v;
vert[v].h = tot;
}
/*
* 判断是否为二分图
* s为bfs的始点
*/
bool is_binary_graph(int s)
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vert[s].d = 0;
vis[s] = true;
int v,u;
while(!Q.empty()) {
u = Q.front(); Q.pop();
for(int i = vert[u].h; i; i = edge[i].next){
v = edge[i].to;
if(!vis[v]) {
vis[v] = true;
vert[v].p = u; //方便输出路径
vert[v].d = vert[u].d^1;
Q.push(v);
}
}
}
for (int i = 2; i <= tot; i++) {
if(vert[edge[i].from].d^vert[edge[i].to].d == 0)
return false;
}
return true;
}
int main()
{
cin>>V>>E;
int a,b;
for(int i=1;i<=E;i++){
cin>>a>>b;
add(a,b);
}
cout << is_binary_graph(1)<<endl;
}
输出BFS树
以上判定二分图主要使用了bfs的思想,因此可以根据过程中储存的vert[i].p
来输出BFS树中每个节点的路径。
递归输出代码如下:
void print_path(int s, int v, int t)
{
if(s == v)
cout << s << "->";
else if(vert[v].p == 0)
cout << "No path from " << s << " to " << v << endl;
else{
print_path(s, vert[v].p, t);
if(v == t)
cout << v << endl;
else
cout << v << "->";
}
}
int main()
{
cin>>V>>E;
int a,b;
for(int i=1;i<=E;i++){
cin>>a>>b;
add(a,b);
}
cout << is_binary_graph(1)<<endl;
for(int i = 2; i <= V; i++){
cout << "path " <<i <<endl;
print_path(1, i, i);
//cout << endl;
}
}