标签:scanf fine hdu1325 ase stdin open max str hdu
//判断输入的数字是否可以构成一棵树
//前一个数是后一个的父亲节点,树的定义:有且仅有一个总根节点,根节点到其他任意节点路径唯一,每个节点只能被其根指向,入度只能为1
#include <stdio.h> #include <string.h> #define maxn 10002 int in[maxn], pre[maxn], count; bool vis[maxn]; int ufind(int k) { int a = k; while(pre[k] != -1) k = pre[k]; int b; while(pre[a] != -1){ b = pre[a]; pre[a] = k; a = b; } return k; } int main() { //freopen("stdin.txt", "r", stdin); int a, b, ok = 1, cas = 1; memset(pre, -1, sizeof(pre)); while(scanf("%d%d", &a, &b)){ if(a < 0) break; if(a == 0){ printf("Case %d is ", cas++); if(count != 1) ok = 0; printf(ok ? "a tree.\n" : "not a tree.\n"); ok = 1; count = 0; memset(in, 0, sizeof(in)); memset(vis, 0, sizeof(vis)); memset(pre, -1, sizeof(pre)); continue; } if(!ok) continue; if(!vis[a]){ ++count; vis[a] = 1; } if(!vis[b]){ ++count; vis[b] = 1; } if(++in[b] > 1) ok = 0; a = ufind(a); b = ufind(b); if(a == b) ok = 0; else { pre[a] = b; --count; } } return 0; }
标签:scanf fine hdu1325 ase stdin open max str hdu
原文地址:https://www.cnblogs.com/lijiahui-123/p/13383073.html