Codeforces 553C Love triangles (graph theory)

Source: Internet
Author: User

Solution:

A better graph theory problem.

To do this, first of all to analyze love relationship and hate relationship, love relationship is transitive. More crucially, the hate relationship is not a singular ring.

It is natural to see the characteristics of the binary diagram without the odd ring.

Then the current task is to dye the first two points, to determine whether a given edge is conflicting, and to indent.

Let's assume that the next point in the graph is the K-point. The hate relationship of this K-point satisfies the relationship of the binary graph.

Then calculate the number of combinations, the total 2^ (k-1) method.

#include <bits/stdc++.h>using namespacestd;Const intN =100009; typedef pair<int,int>II;intVis[n], N, M, Flag, S;vector<ii>E[n];voidDFS (intXintk) {Vis[x]=K; --s;  for(Auto &I:e[x]) {        if(!~Vis[i.first]) {DFS (I.first, K^I.second); } Else {            if((Vis[x]^vis[i.first])! =i.second) {flag=1; return ; }        }        if(flag)return; }}intMain () {memset (Vis,-1,sizeofvis); Ios::sync_with_stdio (0 ); CIN>> N >>m; S=N;  for(inti =1, u, V, c; I <= m; ++i) {cin>> u >> v >>C; E[u].push_back (Make_pair (V, c^1) ); E[v].push_back (Make_pair (U, c^1) ); }     for(inti =1; I <= N; ++i) {if(!~vis[i] &&!E[i].empty ()) {            ++s; DFS (i,1); }        if(flag) {cout<<0<<Endl; return 0; }    }    Long LongAns =1, p =2; --s;  while(S >0 ) {        if(S &1) ans = ans * p%1000000007; P= (p * p)%1000000007; S>>=1; } cout<< ans <<Endl;}
View Code

Codeforces 553C Love triangles (graph theory)

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.