题目链接: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. vuex状态管理工具

    父子组件之间的通信  props传递  父 向子单向传递:且每次 父组件更新时  子组件的props会跟着更新: 如果需要 子组件把数据传递给父组件,就需要在子组件上绑定自定事件 在子组件使用this ...

  2. JoinPoint

    “JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. ” JoinPoint适用于注解和Sc ...

  3. java web石家庄铁道大学课程管理系统

    package kecheng Kc.java package kecheng; public class Kc { private int id; private String classname; ...

  4. 软工网络15团队作业7——Alpha冲刺之事后诸葛亮

    Deadline: 2018-5-16 22:00PM,以博客提交至班级博客时间为准 事后诸葛亮分析 Alpha冲刺,很多同学经历了"Learning by doing"的学一门新 ...

  5. 使用PSR-4配合composer autoload 自动加载文件夹

    require 文件很麻烦,使用PSR-4搭配composer一次加载,终生受用. 感觉类似java中的import了,自己先记录一下最近理解的. 用composer管理自己的包吧 安装compose ...

  6. WebService实例-CRM系统提供WebService实现用户注册功能

    <—start—> 编写crm的webservice接口,实现客户信息保存操作.在CustomerService接口中新增一个服务接口,用于添加客户注册的信息. @Path("/ ...

  7. laravel添加model文件夹,需要改动的地方

    首先,将app\User(等model文件),移入APP\modellists文件夹中,方便整理 第二,修改模型中命名空间和引用其他model的路径 第三,将文件夹app\admin中的控制器文件,全 ...

  8. 如何使用nodejs快速搭建本地服务器

    1.首先要安装好node,js 2.以下有安装包下载的链接:这里的安装包是.msi,如果要其他的,可以到菜鸟教程上去找 32 位安装包下载地址 : https://nodejs.org/dist/v4 ...

  9. 版本控制--git+idea

  10. app自动化测试Appium+python

    一.node.js安装 https://nodejs.org/en/download/ ##一直下一步 ###cmd查看 二.  .NET Framework安装 https://www.micros ...