• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ArcTan

            dfs
            隨筆 - 16, 文章 - 117, 評論 - 6, 引用 - 0
            數據加載中……

            poj 1182食物鏈(并查集的應用)

            并查集好經典的應用,搞明白這個題目,可以秒殺POJ所有并查集題目!

            struct node{
                  
            int father;根節點
                  
            int relation;與根節點關系:0 同類,1吃根節點,2被吃
            } line[maxn];

            int find(int x)
            {
                  
            if (x!=line[x].father)
                  {
                        
            int tmp=line[x].father;
                        line[x].father
            =find(tmp);
                        
            //找規律,求出公式
                        num[x]=(line[x].relation+line[tmp].relation) % 3;        
                   }
                  returen line[x].father;
            }
            void union(int x,int y,int d)
            {
                  
            int fx=find(x);
                  
            int fy=find(y);
                  line[fx].father
            =fy;
                   
            //找規律,求出公式
                  line[fx].relation=(line[y].relation-line[x].relation+2+d) % 3;    
                  
            return ;
            }

            Thinking && Coding!!!

            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #define min(x,y) (x<y?x:y)
            #define max(x,y) (x>y?x:y)
            #define swap(t,x,y) (t=x,x=y,y=t)
            #define clr(list) memset(list,0,sizeof(list))
            #define maxn 50005
            using namespace std;
            int father[maxn];
            int num[maxn];
            int find(int x)
            {
                
            if (x!=father[x])
                {
                    
            int tmp=father[x];
                    father[x]
            =find(father[x]);
                    num[x]
            =(num[x]+num[tmp]) % 3;
                }
                
            return father[x];
            }
            void union_set(int x,int y,int d)
            {
                
            int fx=find(x);
                
            int fy=find(y);
                father[fx]
            =fy;
                num[fx]
            =(num[y]-num[x]+2+d) % 3;
                
            return ;
            }
            int main()
            {
                
            int n,m;
                scanf(
            "%d%d",&n,&m);

                
            int ans=0;
                
            int flag=0;
                
            for (int i=1; i<=n; i++)
                    father[i]
            =i,num[i]=0;
                
            for (int i=1; i<=m; i++)
                {
                    
            int d,x,y;
                    scanf(
            "%d%d%d",&d,&x,&y);
                    
            if (x>|| y>n)
                    {
                        ans
            ++;
                        
            continue;
                    }
                    
            if (d==2 && x==y)
                    {
                        ans
            ++;
                        
            continue;
                    }
                    
            int fx=find(x);
                    
            int fy=find(y);
                    
            if (d==2 && fx==fy)
                    {
                        
            if ((num[x]-num[y]+3)%3!=1)
                            ans
            ++;
                        
            continue;
                    }
                    
            if (d==1 && fx==fy && num[x]!=num[y])
                    {
                        ans
            ++;
                        
            continue;
                    }
                    union_set(x,y,d);
                }
                printf(
            "%d\n",ans);
                
            return 0;
            }





            posted on 2012-07-26 21:51 wangs 閱讀(252) 評論(0)  編輯 收藏 引用 所屬分類: ACM-數據結構

            亚洲午夜精品久久久久久app| 亚洲一本综合久久| 精品久久久中文字幕人妻| 久久天堂AV综合合色蜜桃网 | 色综合久久88色综合天天 | 久久久国产亚洲精品| 精品久久久久久国产| 囯产极品美女高潮无套久久久| 久久综合88熟人妻| 久久久久香蕉视频| 精品免费tv久久久久久久| 尹人香蕉久久99天天拍| 久久最近最新中文字幕大全| 漂亮人妻被中出中文字幕久久| Xx性欧美肥妇精品久久久久久| 久久午夜伦鲁片免费无码| 久久久网中文字幕| 久久久91精品国产一区二区三区 | 久久久99精品一区二区| 精品久久8x国产免费观看| 久久人人爽人人爽人人片av麻烦 | 久久天天躁狠狠躁夜夜96流白浆| 久久精品中文字幕一区| 国内精品久久久久影院优| 无码人妻久久一区二区三区| 久久免费大片| 蜜桃麻豆www久久国产精品| 国产A级毛片久久久精品毛片| 99久久精品国内| 777米奇久久最新地址| 99久久精品午夜一区二区| 久久国产欧美日韩精品| 性欧美大战久久久久久久久| 久久久久久久久久久久久久| 欧美亚洲日本久久精品| 日韩精品无码久久一区二区三 | 亚洲Av无码国产情品久久| 久久久99精品成人片中文字幕 | 亚洲午夜久久久| 久久SE精品一区二区| 久久夜色精品国产噜噜噜亚洲AV |