Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一点提示

Posted by bestofme at 2014-04-17 14:40:11 on Problem 1308
题目中所定义的树要么是空树,要么满足以下条件:
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator