• <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 閱讀(301) 評論(0)  編輯 收藏 引用 所屬分類: graph

            <2010年11月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            亚洲欧美日韩精品久久亚洲区 | 国产伊人久久| 色综合久久久久网| 久久亚洲国产成人精品无码区| 久久伊人精品青青草原高清| 久久夜色精品国产亚洲av| 亚洲а∨天堂久久精品| 99精品国产在热久久| AAA级久久久精品无码区| 久久亚洲精品成人无码网站| 久久综合给合久久狠狠狠97色 | 久久99热这里只有精品国产| 中文精品久久久久国产网址 | 性做久久久久久久| 日韩久久无码免费毛片软件| 精品久久一区二区| 色综合久久综合中文综合网| 久久人人爽人人爽人人片AV麻烦| 国产AⅤ精品一区二区三区久久| 99re久久精品国产首页2020| 久久精品国产网红主播| 国内精品人妻无码久久久影院 | 国产精品国色综合久久| 欧美久久一级内射wwwwww.| 777午夜精品久久av蜜臀| 精品视频久久久久| 久久99国产精品久久| 午夜天堂精品久久久久| 久久久久国产一区二区| 97久久精品人人澡人人爽| 国产精品美女久久久m| 久久久久久伊人高潮影院| 麻豆久久| 最新久久免费视频| 一级女性全黄久久生活片免费 | 人妻无码αv中文字幕久久琪琪布| 婷婷久久综合| 久久久久亚洲AV无码观看| 国产精品久久久久久久久软件| 性做久久久久久久久| 2020国产成人久久精品|