本文共 1548 字,大约阅读时间需要 5 分钟。
原题链接:
Sample Input6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1Sample OutputCase 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
题意:给你一些点,判断是不是树。
解题思路:和杭电的1272题一模一样,只不过表达方式不一样而已,且这道题也是有向图,需要判断子结点是根结点。还有此题数据也比较刁钻,需要进行很多优化,具体看代码。指路HDU——1272题解blog:。这道题使用并查集模板即可,若对并查集还不是很熟的话,这里指路一篇blog:
AC代码:
/**邮箱:unique_powerhouse@qq.com*blog:https://me.csdn.net/hzf0701*注:文章若有任何问题请私信我或评论区留言,谢谢支持。**/#include//POJ不支持#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。#define pb push_back#define IOS ios::sync_with_stdio(false)#define fi first#define se second#define mp make_pairusing namespace std;const int inf = 0x3f3f3f3f;//无穷大const int maxn = 1e4+10;//最大值。typedef long long ll;typedef long double ld;typedef pair pll;typedef pair pii;//*******************************分割线,以上为自定义代码模板***************************************//int father[maxn],vis[maxn],flag,maxx;void init(){ for(int i=0;i =0&&v>=0) { if(u==0&&v==0) { int cnt=0; for(int i=1;i<=maxn;i++) if(vis[i]&&i==father[i]) cnt++; if(cnt<=1&&!flag) printf("Case %d is a tree.\n",++kase); else printf("Case %d is not a tree.\n",++kase); init(); continue; } maxx=max(maxx,max(u,v)); //统计最大顶点编号,减少不必要的计算。 vis[u]=vis[v]=1; Merge(u,v); } return 0;}
转载地址:http://yusn.baihongyu.com/