• <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)

            久久99精品久久久久久9蜜桃 | 欧美亚洲日本久久精品| 欧美一区二区精品久久| 久久免费国产精品| 粉嫩小泬无遮挡久久久久久| 人妻系列无码专区久久五月天| 伊人久久大香线蕉综合影院首页| 国产成人久久精品一区二区三区 | www亚洲欲色成人久久精品| 亚洲国产精品综合久久网络 | 久久久无码人妻精品无码| 久久国产高清一区二区三区| 色综合久久久久综合体桃花网| 国产成人久久久精品二区三区| 亚洲国产精品无码久久久蜜芽 | 精品熟女少妇aⅴ免费久久| 无码人妻精品一区二区三区久久| 久久男人中文字幕资源站| 久久精品免费观看| 国产亚洲婷婷香蕉久久精品| 熟妇人妻久久中文字幕| 亚洲天堂久久久| 久久影视综合亚洲| 久久久久久国产精品无码下载| 99久久免费国产精品热| 97久久精品无码一区二区 | 欧美国产成人久久精品| 久久久精品国产Sm最大网站| 久久九九全国免费| 亚洲国产精品久久久久久| 久久精品www人人爽人人| 久久精品人人做人人妻人人玩| 久久无码专区国产精品发布| 亚洲国产精品无码久久青草| 亚洲?V乱码久久精品蜜桃| 伊人久久大香线蕉无码麻豆| 久久99久久成人免费播放| 热久久国产欧美一区二区精品| 久久丝袜精品中文字幕| 无码人妻久久一区二区三区蜜桃 | 亚洲国产精久久久久久久|