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

            糯米

            TI DaVinci, gstreamer, ffmpeg
            隨筆 - 167, 文章 - 0, 評論 - 47, 引用 - 0
            數據加載中……

            POJ 1733 Parity game 牛題 -> 并查集+hash

            思路:

            這題是道好題!
            假比確定了 [2, 4]、[5, 6]、[7, 8] 的奇偶性
            [4, 5] 的奇偶性無法得知
            [2, 6] 的奇偶性是知道的
            所以只有詢問的區間符合以下要求:
            1. 頭部和某個已知區間的頭部重合
            2. 尾部和某個已知區間的尾部重合
            3. 區間內每一段都是已知的
            才能得出結論

            我只想到這一步,然后用鏈表寫了一遍,TLE 了。上網搜解題報告,看到“并查集”三字,恍然大悟。嗎的太牛逼了。

            我們把首尾相接的多個已知區間歸為一類。
            將這多個區間的尾部的點歸在同一個集合中。
            它們的父親就是最左邊區間頭部。
            它們的權值就是從左邊區間頭部的到自己的這段區間的奇偶性。

            插入區間 [i, j] 的時候,首先查找 i, j 對應的父親。就是 find 操作。
            如果它們的父親相同,則表明它們處在同一個段的多個已知區間中。
            那么 i, j 的權值相減,就很容易得出 [i, j] 的奇偶性了。
            如果它們的父親不同,則需要將 i, j 分別對應的區間段關聯起來。也就是 union 操作。
            這時候要注意判斷 i, j 父親的位置先后。因為這個 WA 了一次。

            由于節點的位置分得很散,用 hash 存放。

            #include <stdio.h>

            #define HASH_SIZE (1 << 18)
            #define NODES_NR 65536

            struct node {
                
            struct node *root, *next;
                
            int idx, val;
            }
            ;

            struct node *hash[HASH_SIZE], nodes[NODES_NR];
            int nodes_cnt;

            inline 
            void find(struct node *t)
            {
                
            static struct node *stk[NODES_NR];
                
            int i;

                
            for (i = 0; t->root != t; t = t->root)
                    stk[i
            ++= t;
                
            for (i--; i >= 0; i--{
                    stk[i]
            ->val += stk[i]->root->val;
                    stk[i]
            ->root = t;
                }

            }


            inline 
            struct node *insert(int idx)
            {
                
            int h;
                
            struct node *t;

                h 
            = idx & (HASH_SIZE - 1);
                
            for (t = hash[h]; t; t = t->next) {
                    
            if (t->idx == idx)
                        
            break;
                }

                
            if (!t) {
                    t 
            = &nodes[nodes_cnt++];
                    t
            ->root = t;
                    t
            ->idx = idx;
                    t
            ->next = hash[h];
                    hash[h] 
            = t;
                }

                
            return t;
            }


            inline 
            int solve(int start, int end, int val)
            {
                
            struct node *a, *b;

                a 
            = insert(start);
                b 
            = insert(end);
                find(a);
                find(b);

                
            if (a->root == b->root)
                    
            return ((b->val - a->val) & 1== val;

                val 
            -= b->val;
                val 
            += a->val;
                a 
            = a->root;
                b 
            = b->root;
                
            if (a->idx < b->idx) {
                    b
            ->root = a;
                    b
            ->val = val;
                }
             else {
                    a
            ->root = b;
                    a
            ->val = -val;
                }


                
            return 1;
            }


            int main()
            {
                
            int n, i, x, a, b;
                
            char str[16];

                freopen(
            "e:\\test\\in.txt""r", stdin);

                scanf(
            "%d%d"&n, &x);
                
            for (i = 0; i < x; i++{
                    scanf(
            "%d%d%s"&a, &b, str);
                    
            if (!solve(a, b + 1, str[0== 'o'))
                        
            break;
                }

                printf(
            "%d\n", i);

                
            return 0;
            }



            posted on 2010-04-22 21:38 糯米 閱讀(881) 評論(0)  編輯 收藏 引用 所屬分類: POJ

            欧美亚洲日本久久精品| 久久99精品国产麻豆婷婷| 亚洲国产成人久久综合碰| 久久人人爽人人爽人人片AV东京热| 999久久久国产精品| 精品久久久无码人妻中文字幕| 亚洲日韩中文无码久久| 一本一道久久精品综合| 色综合久久夜色精品国产| 97久久超碰国产精品2021| 久久电影网| 久久免费国产精品一区二区| 久久久久久久综合狠狠综合| 国产精品久久久久aaaa| 亚洲欧洲久久久精品| 亚洲狠狠综合久久| 亚洲国产精品无码久久久蜜芽| 欧美精品一本久久男人的天堂| 久久久精品人妻一区二区三区蜜桃| 国产精品天天影视久久综合网| 亚洲伊人久久综合中文成人网| 久久无码av三级| 久久精品国产亚洲av日韩| 午夜精品久久久久久影视riav| 情人伊人久久综合亚洲| 久久久久亚洲精品天堂| 亚洲精品无码专区久久久 | 亚州日韩精品专区久久久| AAA级久久久精品无码片| 漂亮人妻被中出中文字幕久久 | 久久久久亚洲精品无码蜜桃| 久久精品免费全国观看国产| 精品久久人人做人人爽综合| 久久国产精品国产自线拍免费| 久久66热人妻偷产精品9| 久久精品国产亚洲av麻豆蜜芽| 漂亮人妻被中出中文字幕久久 | 精品亚洲综合久久中文字幕| 久久精品人成免费| 国产精品久久久久久久| 国产精品久久亚洲不卡动漫|