Codeforces554e:love Triangles

Source: Internet
Author: User

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

You Hate love Triangles (A-B was in love and b-c be in love, but a-c hate all other), and you also hate it when nobody was I N Love. So, considering any three characters, you'll be happy if exactly one pair are in Love (A and B love each other, and C hat Es both A and b), or if all three pairs is in Love (a loves B, B loves C, C loves A).

You is given a list of m known relationships in the anime. You know-sure that certain pairs love each other, and certain pairs hate. You ' re wondering how many ways you can fill in the remaining relationships so is happy with every triangle. The ways is considered different if the characters is in love in one and the hate each and the other. Print This count modulo 1?000?000?007.

Input

The first line of input would contain the integers n,? M (3?≤? N. ≤?100?000, 0?≤? M.≤?100?000).

The nextmLines would contain the description of the known relationships. TheI-th line would contain three integers ai,? b i,? C I . If Ci is 1 and then ai and bi is in love, otherwise, they hate all other (1?≤? a i,? b i? ≤? N , ai? ≠? b I , ).

Each pair of people is described no more than once.

Output

Print a single integer equal to the number of ways to fill in the remaining pairs so, you is happy with every Triangl E 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 is to:

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

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


Test instructions

Give an no-map, ask to add a few days to make it a complete picture, so that any three-point ring without a triangle love, or not love each other


Ideas:

And the complete picture is conditional, first we give each edge according to the title of the assignment value, 1 for love, 0 for not Love

Then for any three-point three edges, there must be only two cases: all 1, or two is 0, and the other is 1

Then we can divide the situation into those edges that have not yet been constructed after the definite edges have been constructed according to the input of the topic requirement.

1. If there are two sides of the same color, then the other side must be 1

2. If two sides are different colors, then the other side must be 0

Then we can do it with a search.


#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack > #include <queue> #include <map> #include <set> #include <vector> #include <math.h># Include <bitset> #include <list> #include <algorithm> #include <climits>using namespace std;# Define Lson 2*i#define Rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define Up (i,x,y) for (i=x;i<=y;i++) # Define down (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __ GCD (A, b) #define LL long long#define N 100005#define INF 0x3f3f3f3f#define EXP 1e-8#define MPa make_pair#define lowbit (x) ( x&-x) const int mod = 1e9+7;vector<pair<int,int> > Mat[n];int n,m;int vis[n];        LL ans;void dfs (int u) {for (int i = 0;i<mat[u].size (); i++) {int v = mat[u][i].first;        int p = Mat[u][i].second; if (vis[v]==-1) {if (p==1) vis[v]=vis[u];            else Vis[v]=!vis[u];        DFS (v);        } if (P==1 && vis[u]!=vis[v]) ans=0;    if (p==0 && vis[u]==vis[v]) ans=0;    }}int Main () {int i,j,k,x,y,z;    scanf ("%d%d", &n,&m);        while (m--) {scanf ("%d%d%d", &x,&y,&z);        Mat[x].push_back (MPa (y,z));    Mat[y].push_back (MPa (x,z));    } MEM (vis,-1);    Ans = (mod+1)/2;            for (i = 1;i<=n;i++) {if (vis[i]==-1) {ans = (ans*2)%mod;            Vis[i] = 0;        DFS (i);    }} printf ("%i64d\n", ans); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces554e:love Triangles

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.