题目链接: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. 一、Xadmin------安装

    翻译:http://xadmin.readthedocs.io/en/docs-chinese 1.安装方法: 1)pip install django-xadmin 2)通过源文件安装,我是通过这种 ...

  2. Django的model form组件

    前言 首先对于form组件通过全面的博客介绍,对于form我们应该知道了它的大致用法,这里我们需要明确的一点是,我们定义的form与model其实没有什么关系,只是在逻辑上定义form的时候字段名期的 ...

  3. Joyride (spaf)

    题目链接:https://codeforces.com/gym/101873/problem/C spaf的复杂度有点迷,按道理来说,一个简单的spaf在这题的复杂度是1e9,所以不敢写,然后用优先队 ...

  4. Day11 Python基础之装饰器(高级函数)(九)

    在python中,装饰器.生成器和迭代器是特别重要的高级函数   https://www.cnblogs.com/yuanchenqi/articles/5830025.html 装饰器 1.如果说装 ...

  5. JDK8-新特性-附demo

    import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; impor ...

  6. Ubuntu端口开放

    一.关于iptable的介绍 维基百科:https://zh.wikipedia.org/wiki/Iptables 注意:iptables的操作需要root权限 二.具体操作 sudo apt-ge ...

  7. PAT L2-009 抢红包

    https://pintia.cn/problem-sets/994805046380707840/problems/994805066890854400 没有人没抢过红包吧…… 这里给出N个人之间互 ...

  8. VMware虚拟机中常见的问题汇总

    在使用虚拟机进行开发工作的时候,经常会遇到各种各样的问题, 总结再次, 防微杜渐 1. wget: unable to resolve host address的解决方法 原因分析: DNS域名解析的 ...

  9. Mac上通过iterm 上传文件到服务器

    .安装 brew install lrzsz #这里以homebrew方式安装12.脚本 拉取 https://github.com/mmastrac/iterm2-zmodem 两个sh文件,将他们 ...

  10. .Net中EF通用数据层小结

    增删改查: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; u ...