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

            The 2010 ACM-ICPC Asia Chengdu Regional Contest - G Go Deeper 二分+2-SAT

            Go Deeper

            Time Limit: 2 Seconds      Memory Limit: 65536 KB

            Here is a procedure's pseudocode:

            	   go(int dep, int n, int m)
            begin
            output the value of dep.
            if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m)
            end
            

             

            In this code n is an integer. a, b, c and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths of array a, b and c are m while the length of array x is n.

            Given the elements of array a, b, and c, when we call the procedure go(0, n , m) what is the maximal possible value does the procedure output?

            Input

            There are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers follow. The i-th(1 ≤ im) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1, bi-1 < n, 0 ≤ ci-1 ≤ 2).

            Output

            For each test case, output the result in a single line.

            Sample Input

            3
            2 1
            0 1 0
            2 1
            0 0 0
            2 2
            0 1 0
            1 1 2
            

             

            Sample Output

            1
            1
            2
            
            解法:
            看到x為二進制數組就很敏感了,然后又看到c的值只能取0,1,2。很快想到了2-sat
            t1=a[i],t2=b[i]
            如果x[t1]+x[t2]!=0,那么就是說t1的0和t2的0沖突,添加t10->t21和t20->t11 
            如果x[t1]+x[t2]!=2,那么就是說t1的1和t2的1沖突,添加t11->t20和t21->t10 
            如果x[t1]+x[t2]!=1,那么就是說t1的0和t2的1沖突,以及t1的1和t2的0沖突,添加t10->t20和t21->t11 以及t11->t21和t20->t10 
            貼代碼
             1# include <cstdio>
             2# include <vector>
             3# include <cstring>
             4using namespace std;
             5int n,m;
             6vector<int> g[500];
             7int a[10005],b[10005],c[10005];
             8int low[500],dfn,color[500],stack[500],top,co;
             9void tarjan(int pos)
            10{
            11   int now=dfn++;
            12   low[pos]=now;
            13   stack[++top]=pos;
            14   for(int i=0;i<g[pos].size();i++)
            15   {
            16      if(low[g[pos][i]]==-1) tarjan(g[pos][i]);
            17      if(low[g[pos][i]]<low[pos]) low[pos]=low[g[pos][i]];
            18   }

            19   if(low[pos]>=now)
            20   {
            21       do
            22       {
            23          color[stack[top]]=co;
            24          low[stack[top]]=2*n;
            25       }
            while(stack[top--]!=pos);
            26       co++;
            27   }

            28   
            29}

            30bool chk(int num)
            31{
            32   for(int i=0;i<(n<<1);i++)
            33      g[i].clear();
            34   for(int i=0;i<=num;i++)
            35      switch(c[i])
            36      {
            37         case 0:
            38              g[a[i]*2].push_back(b[i]*2+1);
            39              g[b[i]*2].push_back(a[i]*2+1);
            40              break;
            41         case 1:
            42              g[a[i]*2].push_back(b[i]*2);
            43              g[b[i]*2+1].push_back(a[i]*2+1);
            44              g[a[i]*2+1].push_back(b[i]*2+1);
            45              g[b[i]*2].push_back(a[i]*2);
            46              break;
            47         case 2:
            48              g[a[i]*2+1].push_back(b[i]*2);
            49              g[b[i]*2+1].push_back(a[i]*2);
            50              break;
            51      }
            ;
            52   memset(low,-1,sizeof(low));
            53   dfn=co=0;
            54   memset(color,-1,sizeof(color));
            55   for(int i=0;i<2*n;i++)
            56     if(low[i]==-1)
            57     {
            58       top=-1;
            59       tarjan(i);
            60     }

            61   for(int i=0;i<n;i++)
            62     if(color[2*i]==color[2*i+1])
            63       return false;
            64   return true;
            65}

            66int main()
            67{
            68    int test;
            69    scanf("%d",&test);
            70    while(test--)
            71    {
            72        scanf("%d%d",&n,&m);
            73        for(int i=0;i<m;i++)
            74          scanf("%d%d%d",a+i,b+i,c+i);
            75        int s=0,e=m-1;
            76        while(s<=e)
            77        {
            78           int mid=(s+e)>>1;
            79           if(chk(mid)) s=mid+1;
            80           else e=mid-1;
            81        }

            82        printf("%d\n",s);
            83    }

            84    return 0;
            85}

            86
            87

            posted on 2010-11-16 00:44 yzhw 閱讀(308) 評論(0)  編輯 收藏 引用 所屬分類: graph

            <2011年1月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久久久高潮毛片免费全部播放| 欧美精品乱码99久久蜜桃| 精品无码久久久久国产动漫3d| 久久久久久噜噜精品免费直播| 久久精品无码一区二区三区日韩| 一级A毛片免费观看久久精品| 狠狠88综合久久久久综合网| 久久精品国产WWW456C0M| 一极黄色视频久久网站| 亚洲国产另类久久久精品| 久久99精品久久久久久噜噜| 亚洲国产另类久久久精品小说| 99久久成人国产精品免费| 亚洲AV日韩AV天堂久久| 97久久精品无码一区二区| 久久精品极品盛宴观看| 99久久国产热无码精品免费久久久久| 亚洲欧洲久久av| 久久97精品久久久久久久不卡| 久久精品毛片免费观看| 久久久久国色AV免费看图片| 日本免费久久久久久久网站| 久久99国产综合精品| 中文成人无码精品久久久不卡 | 欧美日韩中文字幕久久伊人| 精品国产乱码久久久久久人妻 | 亚洲伊人久久综合影院| 久久se精品一区二区| 久久久久久久97| 99久久99久久精品免费看蜜桃| 婷婷综合久久中文字幕蜜桃三电影| 要久久爱在线免费观看| 欧美国产精品久久高清| 精品久久综合1区2区3区激情| 少妇人妻88久久中文字幕| 久久人人爽人人爽人人av东京热| 久久亚洲国产成人影院网站 | 久久久久久午夜成人影院| 7777精品久久久大香线蕉| 99久久婷婷免费国产综合精品| 久久婷婷五月综合色奶水99啪 |