题目链接: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. Django Rest framework基础使用之View:APIView, mixins, generic, viewsets

    先看一张图,对DRF的各个APIView,Mixin,Viewset等有个基本印象: 具体使用方法: 1.APIView: DRF 的API视图 有两种实现方式: 一种是基于函数的:@api_view ...

  2. Javascript 综合示例 网页扫雷游戏

    ---------------认定了的事情,只要是对的,干到底! ------------------------------------------------------------------- ...

  3. 闽江学院软件学院2016级JAVA构建之法-学生自学兴趣小组招募通知

    为提升我2016级学生提升JAVA软件开发学习氛围,鼓励更多同学通过自学.团队学习.在线(社区)学习等方式学习并掌握JAVA课程,尤其是鼓励同学们通过微软中国邹欣老师所倡导的"构建之法&qu ...

  4. SVN插件和Tomcat插件地址

    SVN插件: http://subclipse.tigris.org/update_1.8.x Tomcat插件: http://tomcatplugin.sf.net/update 备注:如果svn ...

  5. Integer的NPE问题

  6. ES5与ES6的小差异

    ES5与ES6的小差异 变量的定义 ES6与ES5的区别 ES5: <script> console.log(username); var username; var username = ...

  7. Requires: libc.so.6(GLIBC_2.14)(64bit)

    centos6 - CentOS 6 - libc.so.6(GLIBC_2.14)(64bit) is needed by - Server Faulthttps://serverfault.com ...

  8. winform使用相关

    1.回车键触发按钮点击事件——回车登录 设置窗体的AccessButton属性 2.密码框样式设置 设置PasswordChar为想要的密码显示的样式,如*  3.设置窗口居中 设置StartPosi ...

  9. asp.net mvc5 action多个参数

    需要完成http://site.com/user/add/1/2这样的url解析 使用action的参数直接获取数据的方式 Action声明如下 ) { ViewBag.clubID = id; ) ...

  10. Highgo 瀚高数据库的简单搭建以及处理参数等.

    1. 获取一个瀚高数据库的安装文件 我这边只获取了 瀚高的 2.0.4 的windows x64 版本的. 来源: 同事从供应商那里获取的. 2. windows上面简单安装 很简单 exe 一路ne ...