poj1308 (并查集)Is It A Tree?
来源:互联网 发布:王者荣耀mvp新算法 编辑:程序博客网 时间:2024/05/29 05:56
题目链接:poj1308
题目的要求是让判断给出的点能否连接成一颗树,一棵树的话就只有一个树根,同时一个子节点不能同时拥有两个父亲,解法是用并查集,用father数组来记录每个节点的父节点,v数组来记录是否输入了节点。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;int father[100],v[100];void set(){ for(int i = 0; i < 100; i ++) father[i] = i, v[i] = 0;}int find_father(int x){ if(father[x] != x) { father[x] = find_father(father[x]); } return father[x];}int main(){ int i,x,y,p = 1; while(scanf("%d%d",&x,&y), x !=-1 && y != -1) { if(!x && !y)//空树也是树,真心坑人。。 { printf("Case %d is a tree.\n",p++); continue; } set(); father[y] = x; v[x] = v[y] = 1; int flag = 1; if(x == y) flag = 0;//两个数相等表示同一点指向自己,不是树 while(scanf("%d%d",&x,&y),x&&y) { if(!flag) continue; v[x] = v[y] = 1; if(father[y] != y) flag = 0;//表示该节点已经有父节点了 int fun = find_father(x); if(fun == y) flag = 0;//如果x的父节点是y,不能构成树,列如1 2 2 1 0 0 else father[y] = fun; } if(!flag) { printf("Case %d is not a tree.\n",p++); continue; } int num = 0; for(i = 0; i < 100; i ++) { if(v[i] && father[i] == i) { num ++;//表示根的数量 } } if(num > 1) printf("Case %d is not a tree.\n",p++); else printf("Case %d is a tree.\n",p++); } return 0;}
- poj1308 (并查集)Is It A Tree?
- POJ1308 - Is It A Tree? (并查集)
- POJ1308--并查集--Is It A Tree?
- Is It A Tree?--POJ1308--并查集
- hdu1325&&poj1308 Is It A Tree? 基础并查集
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
- Poj1308 Is It A Tree? 并查集
- POJ1308 Is it a tree? 基础并查集
- POJ1308 Is It A Tree? 并查集
- POJ1308 Is It A Tree?(树,并查集)
- kuangbin专题五并查集 POJ1308 Is It A Tree?(并查集)
- HDU1325 Is It A Tree? + POJ1308 Is It A Tree?【并查集】
- hdu1325is it a tree?&&poj1308 is it a tree?(并查集)
- HDU1272&&POJ1308 小希的迷宫&&Is It A Tree?(并查集)
- NYOJ129 树的判定 || POJ1308 Is It A Tree? 【并查集应用,树的定义】
- hdu 1325&&poj1308 Is It A Tree? 基础并查集★
- POJ1308 Is It A Tree? 并查集入门|判树
- poj1308 Is It A Tree?
- Solar V5.0 Source
- web画图技术(svg、canvas、fusioncharts、anychart)简介
- 关于ics lab8 performance中的smooth
- 社交平台舆情分析项目的总结和感想(LU学习,EM,KNN)(二)
- ASP.NET页面运行机制
- poj1308 (并查集)Is It A Tree?
- ForSecondWork-No.1:C/C++笔试题回忆并整理
- Android的HelloWorld
- 表达式求值(nyoj305)
- n个小球t次跳出问题球的算法问题
- linux gbd 调试代码
- gcc常用命令
- 才高行厚的hibernate(7)---hibernate的优化
- __int64