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 modulo 1 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 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.

题意:

给出一个无向图,要求加入几天边使得其成为一个全然图,使得随意三个点组成的环没有三角恋,或者互不相爱

思路:

而全然图是有条件的,首先我们给每条边按题目要求赋值,1代表相爱,0代表不相爱

那么对于随意三个点的三条边而言,必定仅仅有两种情况:所有为1。或者两个为0,一个为1

那么在按题目要求的输入先把确定的边构造好后。对于那些还没有构造的边我们能够分情况

1.假设有两条边是同色,那么还有一条边必须是1

2.假设两条边不同色,那么另外一条边必定是0

那么我们就能够用搜索完毕

#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;
}

Codeforces554E:Love Triangles的更多相关文章

  1. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  2. [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

    Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...

  3. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  4. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  5. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  6. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  7. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  8. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  9. Project Euler 91:Right triangles with integer coordinates 格点直角三角形

    Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...

随机推荐

  1. varchar2(100 char)是什么意思

    最佳答案   varchar2(100 char)最长可以插入100个任意字符而varchar2(100)最长可以插入100个英文字符

  2. Python3简明教程(七)—— 字符串

    字符串是 Python 中最常用的数据类型.本节实验将会学习如何对 Python3 的字符串进行处理操作. 字符串的三种表示 可以通过几种不同的方式表示字符串.如单引号('...')或双引号(&quo ...

  3. mysql利用binlog恢复数据

    需求:需要给开发提供一个2018年9月30号的数据,按照我们公司正常备份策略来说,直接找到对应时间的备份数据,解压导入即可,恰好这个时间节点的数据没有,只备份到2018年9月25号的,糟糕了吧 咋办呢 ...

  4. hasOneOf # if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) {

    if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) { export const hasOneOf = (targetarr, ar ...

  5. Python基础3 函数 变量 递归 -DAY3

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  6. Word转html并移植到web项目

    1.打开对应word文件 建议使用web视图查看文档 这样可以提前预览转转成html样式 2.如果有图片修改图片大小及格式 在web视图下,把图片调制适当大小,不然导出的html可能图片较小 3.点击 ...

  7. 微信小程序工具真机调试提示page "xxx/xxx/xxx" is not found

    解决方法: pages对象添加该页面

  8. ajax 请求json数据中json对象的构造获取问题

    前端的界面中,我想通过ajax来调用写好的json数据,并调用add(data)方法进行解析,请求如下: json数据如下: { “type”:"qqq", "lat&q ...

  9. loj6063 Shadow

    题目描述 题解: 显然凸多面体投下来一定是个凸多边形. 对于$30$分,直接投到$x-y$平面上即可. 对于$100$分,考虑搞出平面的一般式方程$ax+by+cz+d=0$. 给出平面上三个点$A, ...

  10. [CF] 986 A. Fair

    http://codeforces.com/problemset/problem/986/A n个点的无向连通图,每个点有一个属性,求每个点到s个不同属性点的最短距离 依稀记得那天晚上我和Menteu ...