HihoCoder 1322 Tree or Not
啰啰嗦嗦写在前面的话:
这学期有算法分析与设计课,每周的作业是在Vjudge上刷相应的题目,为了方便以后回顾我就把代码搬到这里来了 。也希望能帮助其他刷题的朋友,代码不要完完全全复制粘贴(强调强调),重要的是看思路,不懂的大家可以一起交流!米娜桑,一起加油哇!
翠花儿,上题!
Given an undirected graph G which has N vertice and M edges, determine whether G is a tree.
Input
The first line contains an integer T, the number of test cases. (1 ≤ T ≤ 10) For each test case the first line contains 2 integers N and M. (2 ≤ N ≤ 500, 1 ≤ M ≤ 100000)
The following M lines each contain 2 integers a and b representing that there is an edge between a and b. (1 ≤ a, b ≤ N)
Output
For each test case output "YES" or "NO" indicating whether G is a tree or not.
Sample Input
2 3 2 3 1 3 2 5 5 3 1 3 2 4 5 1 2 4 1
Sample Output
YES NO
思路:
首先,*树是一个连通且无环的无向图。在判断连通性时我是用深度优先搜索进行判断的,无环我使用了|E| =|V|-1 来判断的。
在构建图的时候我自己写了一个链表,后来觉得这样做很傻 - - 明明有vector可以用,何乐而不为呢?
代码如下:
#include <iostream>
//*树是一个连通/无环的无向图
#include <queue>
#define MAX 501
using namespace std;
struct ArcNode //边表节点
{
int adjvex;
ArcNode* next;
};
struct VertexNode //顶点表节点
{
int vertex;
ArcNode* firstedge;
};
class AdjGraph //邻接链表表示图
{
private:
VertexNode adjlist[MAX];//存放顶点的数组
int vertexNum, arcNum;//图的顶点数和边数
int visited[MAX]; //用来判断顶点是否被访问过
public:
AdjGraph(int n, int m);//构造函数 n个顶点 m条边
void Insert (int a, int b);
void DFS(int v); //深度优先搜索
bool validTree();
void show();//显示邻接链表构造 测试用
};
AdjGraph::AdjGraph(int n, int m)
{
vertexNum = n;
arcNum = m;
for(int i = 0; i < vertexNum ;i++)
{
visited[i] = 0;
adjlist[i].vertex = i+1;//顶点是从1开始的
adjlist[i].firstedge = NULL;
}
}
void AdjGraph::Insert(int a, int b)
{
ArcNode *pArc = new ArcNode();
pArc->adjvex = b;
if(adjlist[a-1].firstedge == NULL)
{
adjlist[a-1].firstedge = pArc;
}
else
{
ArcNode *p = adjlist[a-1].firstedge;
while(p->next!=NULL)
{
p = p->next;
}
p->next = pArc;
} //接下来重复对 b->a
ArcNode *fpArc = new ArcNode();
fpArc->adjvex = a;
if(adjlist[b-1].firstedge == NULL)
{
adjlist[b-1].firstedge = fpArc;
}
else
{
ArcNode *p = adjlist[b-1].firstedge;
while(p->next!=NULL)
{
p = p->next;
}
p->next = fpArc;
}
}
void AdjGraph:: DFS(int v)
{
visited[v-1] = 1;
ArcNode *p = adjlist[v-1].firstedge;
while(p!=NULL)
{
int j = p->adjvex;
if(visited[j-1]==0)
DFS(j);
p = p->next;
}
}
void AdjGraph:: show()
{
cout<<"邻接链表: "<<endl;
for(int i =0; i < vertexNum ; i++)
{
cout << adjlist[i].vertex;
ArcNode *p = adjlist[i].firstedge;
while (p != NULL)
{
cout<< "-> "<<p->adjvex;
p = p->next;
}
cout<< " ->NULL"<<endl;
}
}
bool AdjGraph::validTree()
{
//证明图无环用|E|=|V|-1
if (arcNum !=(vertexNum -1))
return false;
//证明图是连通的
DFS(1);
for(int i =0; i < vertexNum; i++)
{
if (visited[i]==0)
return false;
}
return true;
}
int main()
{
int T=0;
cin>>T;
for (int i = 0; i < T; i++)
{
int N=0;
int M=0;
cin>>N>>M;
AdjGraph myGraph(N,M);
for(int j = 0; j < M ;j++)
{
int a,b;
cin>>a>>b;
myGraph.Insert(a,b);
}
//myGraph.show();
if(myGraph.validTree()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
/*AdjGraph myGraph(3,2);
myGraph.Insert(3,1);
myGraph.Insert(3,2);
myGraph.show();
*/
return 0;
}
上一篇: VueRouter动态获取路由
推荐阅读
-
mysql索引原理之B+/-Tree
-
EasyUI Tree树组件无限循环实例分析
-
Oracle B树索引简介(B-Tree Index)
-
教你通过B+Tree平衡多叉树理解InnoDB引擎的聚集和非聚集索引
-
【POJ.3321】Apple Tree(树状数组)
-
HihoCoder#1279 : Rikka with Sequence(dp 枚举子集 二进制 神仙题)
-
Extjs中通过Tree加载右侧TabPanel具体实现_extjs
-
Thinkphp的list_to_tree 实现无限级分类列出所有节点_PHP教程
-
图解MySQL索引--B-Tree(B+Tree)
-
C#实现获取系统目录并以Tree树叉显示的方法