• <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
            數(shù)據(jù)加載中……

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

            并查集好經(jīng)典的應(yīng)用,搞明白這個(gè)題目,可以秒殺POJ所有并查集題目!

            struct node{
                  
            int father;根節(jié)點(diǎn)
                  
            int relation;與根節(jié)點(diǎn)關(guān)系:0 同類,1吃根節(jié)點(diǎn),2被吃
            } line[maxn];

            int find(int x)
            {
                  
            if (x!=line[x].father)
                  {
                        
            int tmp=line[x].father;
                        line[x].father
            =find(tmp);
                        
            //找規(guī)律,求出公式
                        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;
                   
            //找規(guī)律,求出公式
                  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 閱讀(260) 評論(0)  編輯 收藏 引用 所屬分類: ACM-數(shù)據(jù)結(jié)構(gòu)

            久久精品国产亚洲av瑜伽| 囯产极品美女高潮无套久久久| 久久91亚洲人成电影网站| 精品人妻伦一二三区久久| 波多野结衣久久一区二区| 国产精品无码久久综合| 日韩十八禁一区二区久久| 久久婷婷五月综合97色| 内射无码专区久久亚洲| 久久99久久99精品免视看动漫| 久久久久人妻精品一区三寸蜜桃| 77777亚洲午夜久久多人| 狠狠久久综合伊人不卡| 97久久国产亚洲精品超碰热| 免费无码国产欧美久久18| 国产巨作麻豆欧美亚洲综合久久 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久精品国产亚洲av麻豆图片 | 久久99这里只有精品国产| 久久国产精品99精品国产987| 久久亚洲AV成人无码软件| 99久久精品无码一区二区毛片| 色欲久久久天天天综合网| 日日狠狠久久偷偷色综合免费| 93精91精品国产综合久久香蕉| 国产精品99久久免费观看| 亚洲∧v久久久无码精品| 国产A三级久久精品| 久久成人小视频| 国产精品99久久久久久宅男小说| 久久人人爽人人爽人人片AV东京热| 久久青草国产精品一区| 久久久久一区二区三区| 国产精品久久久天天影视| 粉嫩小泬无遮挡久久久久久| 色婷婷综合久久久中文字幕| 久久国产色AV免费看| a高清免费毛片久久| 久久亚洲国产午夜精品理论片| 99久久国产综合精品成人影院| 久久93精品国产91久久综合|