题目链接:codeforces553C Love Triangles

我们来看一下对于一个合法三角形可能出现的边

我们发现,在确定了两边之后,第三条边是什么也就随之确定了

我们用\(1\)表示\(love\),用\(0\)表示\(hate\)

那么\(111-->11,1\)

​ \(100-->\ 00,1/10,1\)

我们发现,当两条边的数字相同时,第三条边的数字为\(1\),否则为\(0\)

很明显这个条件在反过来时也是成立的

这有什么作用?

我们推广一下:假设我们已知一个点\(u\)它连出去的所有边,那么我们能得到什么?

我们能得到的是这个图的情况,比如两条边\((u,v),(u,w)\),由它们是否相同可以推出\((v,u)\)的情况

所以我们考虑去推出与一个点相连的所有边的情况,我们取这个点为1

那么在一个联通块内的所有点与1点的边的关系是有一个相对关系的,即在确定了一条边的颜色后,我们可以确定整个联通块的边的颜色

所以对这个联通块我们有2种染色方式

再在减去1号点所在的联通块的颜色应该是已知的,所以最后的答案就是2的联通块数-1的乘方

在找联通块的dfs中顺便判掉是否有解

#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxd=1e9+7;
struct node{
int to,nxt,cost;
}sq[200200];
int n,m,all=0,head[100100],tag[100100];
bool no_so=0; int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
} void add(int u,int v,int w)
{
all++;sq[all].to=v;sq[all].nxt=head[u];sq[all].cost=w;head[u]=all;
} void dfs(int u)
{
int i;
for (i=head[u];i;i=sq[i].nxt)
{
int v=sq[i].to,w=sq[i].cost;
if (tag[v]==-1)
{
if (w==1) tag[v]=tag[u];else tag[v]=1-tag[u];
dfs(v);
}
else
{
if ((w==1) && (tag[v]!=tag[u])) {no_so=1;return;}
else if ((w==0) && (tag[v]==tag[u])) {no_so=1;return;}
}
}
} int main()
{
n=read();m=read();int i;
for (i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
add(u,v,w);add(v,u,w);
}
memset(tag,-1,sizeof(tag));int cnt=-1;
for (i=1;i<=n;i++)
{
if (tag[i]==-1)
{
tag[i]=0;
dfs(i);
if (no_so) {printf("0");return 0;}
else cnt++;
}
}
long long ans=1;
for (i=1;i<=cnt;i++) ans=(ans*2)%maxd;
printf("%I64d",ans);
return 0;
}

codeforces553C 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. OO博客作业1:第1-3周作业总结

    (1)基于度量来分析自己的程序结构 注:UML图中每个划分了的圆角矩形代表一个类或接口,箭头可代表创建.访问数据等行为.类的图形内部分为3个部分,从上到下依次是类的名称.类包含的实例变量(属性).类实 ...

  2. 固态硬盘的PCIE,SATA,M2,NVMe,AHCI分别都指什么?别再搞混了

    原文:https://baijiahao.baidu.com/s?id=1616207956596122967&wfr=spider&for=pc 科技娱乐屋 18-11-0420:5 ...

  3. Python-待

    内置函数总结 https://www.cnblogs.com/jason-lv/p/8243141.html https://www.cnblogs.com/pyyu/p/6702896.html 数 ...

  4. Divisors of Two Integers CodeForces - 1108B (数学+思维)

    Recently you have received two positive integer numbers xx and yy. You forgot them, but you remember ...

  5. 不能再忽视了!宝宝不肯吃粥的N个原因,你避免了几个?

    辅食不懂怎么添加? 宝宝吃饭爱挑食? 营养均衡和多样化的辅食 在这里你都能找到 宝宝辅食微课堂 不能再忽视了!宝宝不肯吃粥的N个原因,你避免了几个? 2017-10-09 09:35 辅食不懂怎么添加 ...

  6. asp.net mvc导出execl_转载

    public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table bo ...

  7. C# List用法 List介绍

    一.#List泛型集合 集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一. 为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList ...

  8. Azure系列2.1.7 —— BlobRequestOptions

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  9. [转帖]NUMA

    作者:ibless 来源:CSDN 原文:https://blog.csdn.net/ibless/article/details/80114009 其实 很早之前对这一块有了解 比较多的的是 CCN ...

  10. python爬虫之Phantomjs安装和使用

    phantomjs: PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引擎.它原生支持多种web 标准:DOM 操作,CSS选择器,JSON,Canvas 以及SVG. phantom ...