poj1308(并查集)

题目链接:http://poj.org/problem;jsessionid=436A34AE4BE856FB2DF9B264DCA9AA4E?id=1308

题意:给定一些边让你判断是否构成数。

思路:这道题细节很多,wa了好久。利用并查集将一棵树的节点并在一块,如果当前的边的两个端点祖先相同,则不能够成树(有环),利用num数组记录每个结点的入度,入度不能大于1。输入为 0 0表示空树,满足条件。可能出现森林,所以用vector记录出现的结点编号,最后判断所有结点是否有相同的祖先。还有不能存在i i,即指向自己的边。

AC代码:

#include<cstdio>
#include<vector>
using namespace std;

const int maxn=1e6+5;
int root[maxn],num[maxn],t1,t2,flag,cas=1;
vector<int> v;

int getr(int k){
    if(root[k]==k) return k;
    else return root[k]=getr(root[k]);
}

int main(){
    while(~scanf("%d%d",&t1,&t2)&&t1>=0){
        if(!t1){
            printf("Case %d is a tree.\n",cas++);
            continue;
        }
        v.clear();
        v.push_back(t1);
        v.push_back(t2);
        for(int i=1;i<=1000000;++i)
            root[i]=i,num[i]=0;
        flag=1;
        if(t1==t2) flag=0;
        int r1=getr(t1),r2=getr(t2);
        root[r2]=r1,++num[t2];
        while(~scanf("%d%d",&t1,&t2)&&t1){
            if(flag){
                v.push_back(t1);
                v.push_back(t2);
                r1=getr(t1),r2=getr(t2);
                if(r1!=r2&&!num[t2]&&t1!=t2)
                    root[r2]=r1,++num[t2];
                else
                    flag=0;
            }
        }
        if(flag){
            int r=getr(v[0]);
            for(int i=1;i<v.size();++i)
                if(getr(v[i])!=r){
                    flag=0;
                    break;
                }
        }
        if(flag)
            printf("Case %d is a tree.\n",cas++);
        else
            printf("Case %d is not a tree.\n",cas++);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/FrankChen831X/p/10667201.html

时间: 2024-08-09 23:43:46

poj1308(并查集)的相关文章

poj1308 并查集

题意:和hdu1272差不多,只不过给出的是有向图,问图中的点是否是一颗树. 还是用并查集合并点,对于一条边,如果连接的两点已经在同一并查集内,则可以直接判否.合并时按边的方向记录点的入度,如果某个点入度大于1也就是某个点有多个父亲节点,则说明不是树.合并时顺便记录合并总次数,最后合并 点数-1 次则是树. 1 #include<stdio.h> 2 #include<string.h> 3 4 int fa[100005],num[100005]; 5 bool vi[10000

HDU1325 &amp;&amp;poj1308 基础并查集

和上一道小希的迷宫差不多,但是在HDU上提交一直WA,POJ过了 HDU的数据太强了吧,强的我感觉数据有问题 题意:输入若干对点,判断是否是一颗树,转化过来也就是是否存在环 点数-边数=1,还要判断每个点的入度都<=1 POJ AC代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const

POJ-1308 Is It A Tree?(并查集判断是否是树)

http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.There is exactl

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

【bzoj3674】 可持久化并查集加强版

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Solution 其实就是用主席树的叶子节点维护并查集的可持久化数组fa[]. 细节 终于认识到了按秩合并的强大,单纯写个路径压缩Re飞,写了路径压缩+按秩合并比单纯的按秩合并每快多少→_→ 代码 // bzoj3674 #include<algorithm> #include<iostream>

BZOJ1015[JSOI2008]星球大战starwar[并查集]

1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit][Status][Discuss] Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或间接地连接. 但好景不长,很快帝国又重

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>