HDU 1325 POJ 1308 Is It A Tree? (並查集),hdupoj

來源:互聯網
上載者:User

HDU 1325 POJ 1308 Is It A Tree? (並查集),hdupoj

這道題就是裸並查集,關鍵在於對不是樹幾種的判斷

1. 空樹是樹 2. 森林不是樹 3. 無環

或者從入度來看:1,無環;2,除了根,所有的入度為1,根入度為0;3,這個結構只有一個根,不然是森林了。


這道題本來暑假做的POJ 1308 但是HDU沒有過。在於空樹沒有考慮。

用並查集判斷有多少個森林注意編號是隨機的,不是次序....

/*input:0 01 1 0 01 2 1 2 0 0 1 2 2 3 4 5 0 01 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 01 2 2 1 0 0 -1 -1output:Case 1 is a tree.Case 2 is not a tree.Case 3 is not a tree.Case 4 is not a tree.Case 5 is not a tree.Case 6 is not a tree.*/#include<math.h>#include<stdio.h>  #include<string.h>#define inf 0x3ffffff#define maxn 10+100000#define max(a,b) ((a)>(b)?(a):(b))int N,M,K;int parent[maxn];int vis[maxn];int find(int *parent,int k){    if(parent[k]==-1)        return k;    parent[k]=find(parent,parent[k]);    return parent[k];}int main(){    int i,j,again;    int e,r,ans,ee,rr,cnt;int nn;    memset(parent,-1,sizeof(parent));    memset(vis,0,sizeof(vis));    nn=ans=again=cnt=0;    while(1)    {        if(again)        {            for(j=0,i=1;i<=nn;i++)            {                if(vis[i] && parent[i]==-1)                    j++;                if(j>1)                {ans=1;break;}            }if(nn==0)ans=0;            if(ans)                printf("Case %d is not a tree.\n",++cnt);                        else                printf("Case %d is a tree.\n",++cnt);            ans=again=0;            memset(parent,-1,sizeof(parent));            memset(vis,0,sizeof(vis));        }        scanf("%d%d",&e,&r);nn=max(nn,max(e,r));        if(e<0 && r<0)            break;        if(e==0  && r==0 )        {again=1;continue;}vis[r]=vis[e]=1;        if(ans==1)            continue;        ee=find(parent,e);        rr=find(parent,r);        if(r!=rr || rr==ee)            ans=1;        parent[rr]=ee;                }   return 0;  }



小弟想知道杭電 hdu上那些題目是只考察並查集的,菜鳥我想練練,希望最好可以把大概難易程度列出謝了

把我知道的都列出來了。。
簡單並查集
1213 How Many Tables
1232 暢通工程
(杭電簡單的並查集不是很多)

簡單最小產生樹
1233 還是暢通工程
1863 暢通工程
1874 暢通工程再續
1875 繼續暢通工程
1162 Eddy's picture
1102 Constructing Roads
1301 Jungle Roads
(可以通過最小產生樹練習並查集)

稍微有點難度並查集
1272 小希的迷宮
1325 Is It A Tree?
1856 More is better

有點難度的並查集
1116Play on Words
1829A Bug's Life
1198Farm Irrigation
3635Dragon Balls
2473Junk-Mail Filter
3172Virtual Friends
3047Zjnu Stadium
3038How Many Answers Are Wrong
1558Segment set
1598find the most comfortable road
3461Code Lock
2818Building Block
3367Pseudoforest
3234Exclusive-OR
 
高人,教看看我poj1308 為何wa

給你兩組數
input: 0 0
yes
input:3 8 6 8 0 0
no
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.