• <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)用,搞明白這個題目,可以秒殺POJ所有并查集題目!

            struct node{
                  
            int father;根節(jié)點
                  
            int relation;與根節(jié)點關(guān)系:0 同類,1吃根節(jié)點,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 閱讀(251) 評論(0)  編輯 收藏 引用 所屬分類: ACM-數(shù)據(jù)結(jié)構(gòu)

            要久久爱在线免费观看| 国产精品久久永久免费| 亚洲精品国产美女久久久| 久久久久无码精品国产不卡| 91精品婷婷国产综合久久 | 日本三级久久网| 香蕉久久久久久狠狠色| 成人妇女免费播放久久久| 欧美日韩中文字幕久久久不卡 | 久久午夜羞羞影院免费观看| 精品熟女少妇aⅴ免费久久| 亚洲人成伊人成综合网久久久| 国产精品成人久久久久久久| 色综合久久无码五十路人妻| 久久婷婷色综合一区二区| 99久久久国产精品免费无卡顿 | 国产精品久久久久久久午夜片| 日日噜噜夜夜狠狠久久丁香五月 | 亚洲中文字幕无码久久精品1| 精品久久久无码中文字幕| 久久久无码一区二区三区| 精品久久亚洲中文无码| 看全色黄大色大片免费久久久| 国产精品久久久久国产A级| 久久精品国产色蜜蜜麻豆| 香蕉久久夜色精品国产2020| 久久亚洲国产精品五月天婷| 99久久99久久精品国产| 免费国产99久久久香蕉| 国产精品久久一区二区三区| 精品永久久福利一区二区| 狼狼综合久久久久综合网| 久久精品国产色蜜蜜麻豆| 久久久久久久久久久久久久| 久久久久久久综合狠狠综合| 亚洲精品无码久久毛片 | 99久久精品午夜一区二区| 97久久国产露脸精品国产| 亚洲精品乱码久久久久久蜜桃不卡 | 青青久久精品国产免费看| 亚洲国产成人精品久久久国产成人一区二区三区综|