• <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>
            隨筆-72  評論-126  文章-0  trackbacks-0
            很好玩的算法
            強(qiáng)連通+縮點(diǎn)可以把一塊點(diǎn)看成一個(gè)點(diǎn),大大加快算法。還有一些無法解決的問題也可以用這個(gè)來解決
            前幾天在林學(xué)院做題的時(shí)候胡搞搞出來了,哈哈
            今天又A了一道
            最近對圖對樹越來越有感覺了

            http://acm.hdu.edu.cn/showproblem.php?pid=2767
            #include "stdio.h"
            #include 
            "algorithm"
            using namespace std;
            #define maxn 20001
            struct Node {
                
            int to;
                Node 
            * next;
            }list[maxn],opp[maxn];
            struct SCC{
                
            int time;
                
            int newid;
                
            int idx;
            }hh[maxn];
            int time,newid;
            bool flag;
            bool hash[maxn];
            bool hashid[maxn];
            bool gashid[maxn];
            //--------------------------------------------
            void dfs(int idx) {
                Node 
            * buf;
                buf 
            = list[idx].next;
                
            while(buf) {
                    
            if(!hash[buf->to]) {
                        hash[buf
            ->to] = true;
                        dfs(buf
            ->to);
                    }
                    buf 
            = buf->next;
                }
                
            if(time == 7)
                    time 
            = 7;
                hh[idx].time 
            = time ++;
                hh[idx].idx 
            = idx;
            }
            void dfs2(int idx) {
                Node 
            * buf;
                buf 
            = opp[idx].next;
                
            while(buf) {
                    
            if(!hash[buf->to]) {
                        hash[buf
            ->to] = true;
                        dfs2(buf
            ->to);
                    }
                    buf 
            = buf->next;
                }
                hh[idx].newid 
            = newid;
            }
            void dfs3(int idx) {
                Node 
            * buf;
                buf 
            = list[idx].next;
                
            while(buf) {
                    
            if(hh[idx].newid != hh[buf->to].newid) {
                        hashid[hh[idx].newid] 
            = true;
                        gashid[hh[buf
            ->to].newid] = true;
                    }
                    
            if(!hash[buf->to]) {
                        hash[buf
            ->to] = true;
                        dfs3(buf
            ->to);
                    }
                    buf 
            = buf->next;
                }
            }
            bool cmp(SCC a,SCC b) {
                
            return a.time > b.time;
            }
            //-----------------------------------------
            int main() {
                
            int n,i,a,b,m,T;
                Node 
            * buf;
                scanf(
            "%d",&T);
                
            while(T--) {
                    scanf(
            "%d%d",&n,&m);
                    
            for(i = 1 ; i <= n ; i ++) {
                        list[i].next 
            = NULL;
                        opp[i].next 
            = NULL;
                    }
                    
            while(m --) {
                        scanf(
            "%d%d",&a,&b);
                        buf 
            = (Node *)malloc(sizeof(Node));        //正圖
                        buf->to = b;
                        buf
            ->next = list[a].next;
                        list[a].next 
            = buf;

                        buf 
            = (Node *)malloc(sizeof(Node));        //反圖
                        buf->to = a;
                        buf
            ->next = opp[b].next;
                        opp[b].next 
            = buf;
                    }
                    memset(hash,
            false,sizeof(bool)*(n+1));
                    time 
            = 0;
                    
            for(i = 1 ; i <= n ; i ++) {                //先確定時(shí)間戳
                        if(!hash[i]) {
                            hash[i] 
            = true;
                            dfs(i);
                        }
                    }
                    sort(hh
            +1,hh+1+n,cmp);                        //按時(shí)間戳排序
                    memset(hash,false,sizeof(bool)*(n+1));
                    newid 
            = 0;
                    
            for(i = 1 ; i <= n ; i ++) {                //把點(diǎn)分成幾塊
                        if(!hash[hh[i].idx]) {
                            hash[hh[i].idx] 
            = true;
                            hh[hh[i].idx].newid 
            = ++newid;
                            dfs2(hh[i].idx);
                        }
                    }
                    
            if(newid == 1) {
                        puts(
            "0");
                        
            continue;
                    }
                    memset(hash,
            false,sizeof(bool)*(n+1));
                    memset(hashid,
            false,sizeof(bool)*(newid+1));
                    memset(gashid,
            false,sizeof(bool)*(newid+1));
                    
            for(i =1 ; i <= n ; i ++) {                    //找出塊的出度入度
                        if(!hash[i]) {
                            hash[i] 
            = true;
                            dfs3(i);
                        }
                    }
                    
            int cnt = 0;
                    
            int cnt1 = 0;
                    
            for(i = 1; i <= newid ; i ++) {
                        
            if(!hashid[i])
                            cnt 
            ++;
                        
            if(!gashid[i])
                            cnt1 
            ++;
                    }
                    printf(
            "%d\n",cnt>cnt1?cnt:cnt1);
                }
                
            return 0;
            }


            posted on 2009-05-17 20:36 shǎ崽 閱讀(686) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品9999久久久久| 婷婷久久综合九色综合九七| 久久精品国产99久久无毒不卡| 狠狠色丁香婷婷久久综合| 99久久精品久久久久久清纯| 久久黄视频| 99久久亚洲综合精品网站| 久久亚洲私人国产精品vA| 久久丫精品国产亚洲av不卡| 久久996热精品xxxx| 久久97久久97精品免视看| 久久国产精品国语对白| 久久久受www免费人成| 久久涩综合| 国内精品久久久久影院亚洲| 性做久久久久久久久浪潮| 中文成人久久久久影院免费观看| 久久精品国产亚洲Aⅴ香蕉 | 久久99久久无码毛片一区二区| 色欲综合久久躁天天躁| 成人精品一区二区久久久| 久久精品国产欧美日韩99热| 热久久国产欧美一区二区精品 | 久久综合综合久久狠狠狠97色88| 欧美黑人激情性久久| 久久婷婷激情综合色综合俺也去| 久久精品日日躁夜夜躁欧美 | 久久精品国产99国产精品亚洲| 无码人妻久久一区二区三区免费 | 免费国产99久久久香蕉| 色综合久久中文综合网| 国产成人久久久精品二区三区| 久久精品国产亚洲精品| 中文字幕亚洲综合久久2| 少妇精品久久久一区二区三区| 亚洲精品国产综合久久一线| 久久久久国产亚洲AV麻豆| 久久综合伊人77777麻豆| 色偷偷91久久综合噜噜噜噜| 九九久久精品国产| 日产久久强奸免费的看|