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

來源:互聯網
上載者:User

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

C. Love Trianglestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 ≤ nai ≠ 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 41 2 12 3 13 4 04 1 0
output
1
input
4 41 2 12 3 13 4 04 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 1000000007int n,m,a,b,c,vis[N],ans;vector<pii> 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\n",ans);    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.