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

            QuXiao

            每天進步一點點!

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              50 隨筆 :: 0 文章 :: 27 評論 :: 0 Trackbacks
                這幾天做了些并查集的題目,其中有一些簡單題就是直接套模板,沒什么可以思考的。但今天做的兩道題就稍微有些難度了,只有真正理解了“并”和“查”的過程才有解題的思路。

            思路:
                告訴你[a,b]之間1個數(shù)的奇偶情況,那么你就可以在a-1和b之間連一條邊,權(quán)值就是其奇偶情況。這樣一來,比如[1,2]和[3,4]的情況已知,[1,4]的情況也就知道了。當(dāng)題目給出[a,b]的情況時,首先分別從a和b往上找,找到他們的根r1和r2,如果r1 !=  r2,表示a,b之間的奇偶情況還不確定,就將r1和r2之間連起來,根據(jù)a到r1的權(quán)值、b到r2的權(quán)值和題目所給的奇偶情況,設(shè)置r1和r2之間的權(quán)值,以符合題目要求。若r1 == r2,則表示[a,b]之間情況已確定,根據(jù)a到r1的權(quán)值和b到r2的權(quán)值,就可以判斷題目所給的[a,b]的情況是否為真。
                其實當(dāng)時做的時候,還不是很懂,但沒想到稀里糊涂的就AC了。推薦一下這個網(wǎng)頁:http://hi.baidu.com/fandywang_jlu/blog/item/b49e40893ddbb0b00f244485.html,這里面介紹并查集挺詳細的,還有不少推薦題目,有些還不會做。:P

            代碼:

            #include <iostream>
            #include <map>
            using namespace std;

            const int MAX = 10005;

            int n, p;
            int pre[MAX];
            int parity[MAX];        //i到目前集合的根的奇偶情況
            map<int, int> numIndex;        //用于離散化

            int Find (int x)
            {
                if ( pre[x] == -1 )
                    return x;
                int f;
                f = Find(pre[x]);
                parity[x] = (parity[x] + parity[pre[x]]) % 2;    //此時pre[x]已指向最終的集合的根
                pre[x] = f;
                return f;
            }

            bool Query (int x, int y, int odd)
            {
                int r1, r2;
                r1 = Find(x);
                r2 = Find(y);
                if ( r1 == r2 )
                {
                    if ( (parity[x] + parity[y]) % 2 == odd )
                        return true;
                    else
                        return false;
                }
                else            //只是將r1接到r2下面,這邊還可以優(yōu)化
                {
                    pre[r1] = r2;
                    parity[r1] = (parity[x] + parity[y] + odd) % 2;
                    return true;
                }
            }

            void Solve ()
            {
                int i, x, y, index, idx1, idx2, odd;
                char s[10];
                scanf("%d%d", &n, &p);
                index = 0;
                for (i=0; i<p; i++)
                {
                    scanf("%d%d%s", &x, &y, &s);
                    x --;
                    if ( numIndex.find(x) == numIndex.end() )
                        numIndex[x] = index ++;
                    idx1 = numIndex[x];
                    if ( numIndex.find(y) == numIndex.end() )
                        numIndex[y] = index ++;
                    idx2 = numIndex[y];
                    if ( strcmp(s, "odd") == 0 )
                        odd = 1;
                    else
                        odd = 0;
                    if ( Query(idx1, idx2, odd) == false )
                    {
                        break;
                    }
                }
               
                printf("%d\n", i);
            }

            void Init ()
            {
                memset(pre, -1, sizeof(pre));
            }

            int main ()
            {
                Init();
                Solve();

                return 0;
            }
            posted on 2008-08-15 11:23 quxiao 閱讀(1050) 評論(3)  編輯 收藏 引用 所屬分類: ACM

            評論

            # re: PKU 1733 Parity game 2009-01-18 13:02 gugli
            Hi from Brazil!

            I could not understand your logic because I dont understand chinese.
            could you translate to english?

            :)

              回復(fù)  更多評論
              

            # re: PKU 1733 Parity game 2009-01-19 09:47 ACM-Boy
            @gugli
            This problem is solved by me long time ago, so I can't understand the meaning of the problem very clearly. I just translate the article above code into english.

            Tip:
            Tell you whether one number between a and b is odd or even, then you can link a line between a-1 and b, the value of the line is determined by whether the number is odd or even. So if you know the state (even or odd) of [1, 2] and [3, 4], of course you know the state of [1, 4].
            When the state of [a, b] is given, you find the root of a (r1) and the root of b (r2).
            If r1 != r2, it means that the state of [a,b] is not determined. Then link r1 and r2, according to the information that problem give you and the state of a to r1 and b to r2, you can set the value of r1 and r2.
            If r1 == r2, it means the state of [a, b] is determined, you can know whether it's true that the state of [a, b] given.

            Hope you can understand. Happy new year! :)   回復(fù)  更多評論
              

            # re: PKU 1733 Parity game 2012-06-16 14:59 TCH
            What is your country?@gugli
              回復(fù)  更多評論
              

            久久综合狠狠色综合伊人| 久久亚洲AV无码精品色午夜麻豆| 狠狠色婷婷久久综合频道日韩| 中文精品99久久国产 | 久久99热这里只频精品6| 久久精品视频一| 波多野结衣中文字幕久久| 99久久精品国产综合一区| 久久受www免费人成_看片中文 | 欧美麻豆久久久久久中文| 久久久午夜精品| 久久亚洲国产精品一区二区| 午夜精品久久久久久久无码| 人妻精品久久无码区| 久久久久久久亚洲精品| 国产亚洲精久久久久久无码77777| 国产V综合V亚洲欧美久久| 亚洲欧洲久久久精品| 99久久精品免费观看国产| 亚洲精品乱码久久久久久按摩| 精品久久久久中文字| 97久久超碰国产精品旧版| 久久狠狠爱亚洲综合影院 | 久久影视国产亚洲| 久久久久国产精品熟女影院 | 少妇久久久久久被弄高潮| 久久精品国产亚洲Aⅴ香蕉| 欧美熟妇另类久久久久久不卡 | 精品久久久久久亚洲| 亚洲女久久久噜噜噜熟女| 久久久综合香蕉尹人综合网| 国产精品综合久久第一页 | 久久男人AV资源网站| 久久久精品午夜免费不卡| 久久丫精品国产亚洲av| 亚洲精品乱码久久久久久按摩| 久久受www免费人成_看片中文| 日本久久中文字幕| 久久亚洲国产成人影院网站| 久久国产免费| 99久久国产亚洲综合精品|