Codeforces Round #309 (Div. 1) C. Love Triangles Bipartite Graph

Source: Internet
Author: User

Codeforces Round #309 (Div. 1) C. Love Triangles Bipartite Graph

 

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

There are deleting 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 hasNCharacters. The characters are labeled from 1N. 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 ).

You are given a listMKnown 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 0000000 0000000 0000007.

Input

The first line of input will contain two integersN, Bytes,M(3 cores ≤ CoresNLimit ≤ limit 100 0000000, 0 limit ≤ limitMLimit ≤ limit 100 0000000 ).

The nextMLines will contain the description of the known relationships.I-Th line will contain three integersAI, Bytes,BI, Bytes,CI. IfCIIs 1, thenAIAndBIAre in love, otherwise, they hate each other (1 rows ≤ othersAI, Bytes,BILimit ≤ limitN,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 limit 000 limit 000 limit 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:

  • Make everyone love each other
  • Make 1 and 2 love each other, and 3 hate 1 and 2 (cost rically, 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.

     

    The question is: give n vertices and m edges to meet the number of triangles that do not exist, that is, the valid graph requires that any three points do not exist. a prefers B to c, but a hates c, neither does a B c hate each other. If 1 is liked, 0 is hated, and a B c is also in a triangle composed of three points, it is valid only when there are three 1 s or two 0 s and one 1 s, this is defined as the triangle rule.

    Through this rule, we can see that if a is a connected block and B is c, in order to satisfy the triangle rule above, you can certainly launch a and c (because if a hates c, it is not a valid graph). In this way, a favorite will constitute a set, and any two of them in this set will be liked, on the contrary, if a hates B, the set in which a is located and the set in which B is located are annoying between any two points (it is good proof, because if a's set is another d, because a-d = 1 a-B = 0, the side of B-d must be 0 to satisfy the triangle rule above ). The above analysis shows that the connected block must be a bipartite graph. In the bipartite graph, x sets y are mutually liked, and x y sets are mutually annoying. We only need to use the 0 1 color of the bipartite graph, and there is no conflict at last. This connected block is valid. Otherwise, there is no solution and the answer is 0.

    In the last step, k independent connected blocks can be formed through the above dyeing. The k independent connected blocks are arranged and combined, that is, the k independent connected blocks must be divided into 2 heaps at will, and the total number is 2 ^ (k-1 ). as you can understand, the first heap can be 0 1 2 first... k, total: c (0, k) + c (1, k) + c (2, k) +... + c (k, k) = 2 ^ (k ). but the first heap and the second heap are the same, so divide by 2, the total number is 2 ^ (k-1 ). in computing, you can also use a fast quadratic power, which is faster, but there is a simple method. The initial ans = (mod + 1)/2; each ans = (ans + ans) % mod calculates k times. For the first time, it is good (ans + ans) % mod is 1, so it is equivalent to the calculation of 2 ^ (k-1) out.

     

    #define N 100050#define M 100005#define maxn 205#define MOD 1000000007int 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;}
       


     

     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.