南阳理工 oj 图的邻接表dfs dfs 均可
2013-06-04 11:55
218 查看
//有点郁闷深搜过来, 广搜没过,求大神指点啊
#include <stdio.h> #include <stdlib.h> #include <string.h> int n; struct node { int num; struct node * next; }; struct data { int prior; node * last_linked; }map[100000]; node q[20000]; int front, rear; void del( ) { int i; node *p, *q; for (i = 1; i <= n; i++) { p = map[i].last_linked; while (p != NULL) { q = p->next; delete p; p = q; } } }; void dfs( int current, int prior ) { map[current].prior = prior; node * p = map[current].last_linked; while( p != NULL ) { if( map[p->num].prior == 0 ) { dfs( p->num, current ); } p = p->next; } }; void bfs( int current, int prior ) { node last; front = rear = 0; map[current].prior = prior; node * p; node temp; temp.num = current; temp.next = NULL; q[rear++] = temp; while( front < rear ) { last = q[front++]; p = map[last.num].last_linked; while( p != NULL ) { if( map[p->num].prior == 0 ) { map[p->num].prior = last.num; q[rear++] = *p; } p = p->next; } } } int main() { freopen( "1.txt", "r", stdin ); node *p; int T, start, u, v; scanf( "%d\n", &T ); while( T-- ) { scanf( "%d%d", &n, &start ); for( int i = 1; i <= n; i++ ) { map[i].prior = 0; map[i].last_linked = NULL; } for( int i = 1; i < n; i++ ) { scanf( "%d%d", &u, &v ); p = new node; p->num = v; p->next = map[u].last_linked; map[u].last_linked = p; p = new node; p->num = u; p->next = map[v].last_linked; map[v].last_linked = p; } bfs( start, -1 ); //dfs(start, -1 ); int i; for ( i = 1; i < n; i++) { printf("%d ", map[i].prior ); } printf("%d\n", map[i].prior ); del( ); } return 0; }
相关文章推荐
- 南阳理工OJ 迷宫寻宝(一)DFS
- hrbust 1954 哈理工oj 1954 神奇的国家【图论+邻接表+dfs】
- hrbust 1326 哈理工oj 1326 Leyni的国家【DFS+邻接表建图】
- 第七届河南省赛 zzuoj 10401: A.物资调度 (dfs)
- 南阳理工OJ_1的个数
- 南阳理工OJ_奇偶数分离
- 【杭电oj1010 】- Tempter of the Bone(dfs,奇偶剪枝)
- xynuoj 1277: 子集和问题(dfs)
- 成长路上的小程序之——图的邻接表DFS、BFS
- ZZULIOJ/ 郑轻 OJ 1908: 小火山的围棋梦想(DFS,题型模板)
- 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
- 南阳理工OJ115(最短路径)
- POJ2230 Watchcow 欧拉回路[dfs+邻接表]
- 小白算法练习 hdu courses 1083 匈牙利算法 dfs 邻接矩阵 vector模拟邻接表
- DFS(邻接表表示)
- poj 3050(dfs+回溯或bfs均可)
- 南阳理工OJ_题目709 异形卵
- DFS--深度优先搜索--图的邻接表表示
- 南阳理工OJ_题目995 硬币找零
- 南阳理工OJ 15 括号匹配