Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一点提示题目中所定义的树要么是空树,要么满足以下条件: 1> 所有节点的入度不大于1; 2> 顶点数 = 边数 + 1. C代码: #include <stdio.h> #define N 128 int main() { int ncases = 0, u, v; while (scanf("%d%d", &u, &v) != EOF && u >= 0) { int vert[N] = {0}, paren[N] = {0}, ne, nv; int flag = 1; if (u != 0) { ne = 0, nv = 0; do { if (paren[v] == 0) { paren[v] = u; } else { flag = 0; } if (vert[u] == 0) vert[u] = 1, nv++; if (vert[v] == 0) vert[v] = 1, nv++; vert[u] = vert[v] = 1; ne++; } while (scanf("%d%d", &u, &v) != EOF && u != 0); if (ne != nv - 1) { flag = 0; } } if (flag == 1) { printf("Case %d is a tree.\n", ++ncases); } else { printf("Case %d is not a tree.\n", ++ncases); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator