程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #309 (Div. 1) C. Love Triangles 二分圖

Codeforces Round #309 (Div. 1) C. Love Triangles 二分圖

編輯:C++入門知識

Codeforces Round #309 (Div. 1) C. Love Triangles 二分圖


 

C. Love Triangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

There are many anime that are about love triangles: Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).

You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).

You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo1 000 000 007.

Input

The first line of input will contain two integers n, m (3 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000).

The next m lines will contain the description of the known relationships. The i-th line will contain three integers ai, bi, ci. If ci is 1, then aiand bi are in love, otherwise, they hate each other (1 ≤ ai, bi ≤ n, ai ≠ bi, \).

Each pair of people will be described no more than once.

Output

Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo1 000 000 007.

Sample test(s) input
3 0
output
4
input
4 4
1 2 1
2 3 1
3 4 0
4 1 0
output
1
input
4 4
1 2 1
2 3 1
3 4 0
4 1 1
output
0
Note

In the first sample, the four ways are to:

  • Make everyone love each other
  • Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this).

    In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.

     

    題意是, 給出n個點,m個邊,要求滿足不存在三角戀的個數,也就是合法圖要求任意三點間不存在 a 喜歡 b b 喜歡c,但a討厭 c,也不存在a b c相互討厭。如果1看成喜歡,0看成討厭,也在a b c三點組成的三角形內,只有可能是三個1,或兩個0 一個1,才是合法的,這個定義為三角形法則吧。

    通過這個規則,就說明了,如果這是一個連通塊,如果a喜歡b b喜歡c,則為了滿足上面的三角形法則,一定可以推出a也喜歡c(因為如果a討厭c,不是合法的圖),這樣一來,喜歡的會構成一個集合,這個集合內任意兩個都是喜歡的,相反,如果a討厭b,則a所在的集合與b所在的集合,任意兩點間都是討厭的(很好證明,因為假如a的集合另一點d,因a - d = 1 a - b = 0,則為了滿足上面三角形法則,b-d邊一定是0 )。通過上面的分析發現,這個連通塊一定是二分圖。二分圖x 集 y集內是相互喜歡,x y集間是相互討厭的。只需要用二分圖的 0 1 染色,最後沒有沖突,這個連通塊就是合法的,否則,不存在解,答案為0。

    最後一步,通過上面的染色,可以形成k個獨立的連通塊。這k個獨立的連通塊通過排列組合,也就是要求把這k個獨立的連通塊任意分成2堆,總的個數自然是2^(k-1).可以這樣理解,第一堆可以先0 1 2 ... k個,總和為c(0,k) + c(1,k) + c(2,k) + . .. + c(k,k) = 2 ^ (k).但第一堆第二堆 是一樣的,所以要除以2,總個數就用2^(k-1).在計算的時候,當然也可以用快速二次冪了,這樣更快,但有一個簡單的方法。初始ans = (mod+1)/2;每次ans = (ans + ans)%mod 計算k次,第一次,則好(ans + ans) % mod 為1 ,所以等價於計算了2^(k-1)出來了。

     

    #define N 100050
    #define M 100005
    #define maxn 205
    #define MOD 1000000007
    int n,m,a,b,c,vis[N],ans;
    vector p[N];
    void DFS(int f){
        FI(p[f].size()){
            int v = p[f][i].first;
            int g = p[f][i].second;
            if(vis[v] == -1){
                if(g) vis[v] = vis[f];
                else vis[v] = 1 - vis[f];
                DFS(v);
            }
            if(g){
                if(vis[v] != vis[f]) ans = 0;
            }
            else {
                if(vis[v] == vis[f]) ans = 0;
            }
        }
    }
    int main()
    {
        while(S2(n,m)!=EOF)
        {
            FI(n) p[i+1].clear();
            FI(m){
                S2(a,b);S(c);
                p[a].push_back(make_pair(b,c));
                p[b].push_back(make_pair(a,c));
            }
            memset(vis,-1,sizeof(vis));
            ans = (MOD + 1)/2;
            for(int i= 1;i<=n;i++){
                if(vis[i] == -1){
                    ans = (ans + ans)%MOD;
                    vis[i] = 0;
                    DFS(i);
                }
            }
            printf(%d
    ,ans);
        }
        return 0;
    }
    


     

     

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved