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 |
怎么就错了哦。。。无语#include<stdio.h> #include<string.h> #define MAX 10000 bool graph[100][100]; bool hash[100], isleaf[100]; int root, maxid; int edges, nodes; int init(int a, int b) { int i; nodes = 0; edges = 0; root = -1; maxid = 0; memset(graph, 0, sizeof(graph)); memset(hash, 0, sizeof(hash)); while(a+b != 0) { graph[a][b] = 1; if(!hash[a]) nodes++; if(!hash[b]) nodes++; edges++; hash[a] = 1; hash[b] = 1; isleaf[b] = 1; if(a > maxid) maxid = a; if(b > maxid) maxid = b; scanf("%d %d", &a, &b); } for(i = 0; i <= maxid; i++) if(!isleaf[i] && hash[i]) { if(root != -1) return -1; root = i; } if(edges+1 != nodes) root = -1; return root; } int BFS(int root) { int queue[MAX]; int rear = 1, front = 0, i; int cnt = 1; memset(hash, 0, sizeof(hash)); queue[0] = root; while(rear != front) { int cur = queue[front]; front = (front+1)%MAX; for(i = 0; i <= maxid; i++) { if(!hash[i] && graph[cur][i]) { queue[rear] = i; hash[i] = 1; cnt ++; rear = (rear+1)%MAX; } } } if(cnt == nodes) return 1; else return 0; } int main() { int a, b, cs = 1; while(scanf("%d %d", &a, &b) && (a != -1 && b !=-1)) { //if(a == b && a == 0) {printf("Case %d is a tree.\n", cs++); continue;} root = init(a, b); //printf("root = %d\n", root); if(root != -1 && BFS(root)) printf("Case %d is a tree.\n", cs++); else printf("Case %d is not a tree.\n", cs++); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator