题目链接

题意:给定n个点,给出一些边权为0/1的边,构造完全图,满足对于任何一个三元环,三条边权和为奇。求符合条件的完全图数量,对\(1e9+7\)取模。

分析:其实原题给定的边权是love/hate,love即1,hate即0。

所以对于三元环而言,只存在“爱爱爱”或“爱恨恨”。

如果我们按此讨论点与点之间的关系,我们会想到什么?

敌人的敌人就是朋友,朋友的朋友还是朋友。

那么这道题就和[BOI2003]团伙的描述有些类似了。

我们显然可以用到并查集。

团伙那题,我们确定两点关系可以建立补集,也可以使用带权并查集。

这题,我们发现,一个集合是否有补集在统计答案时并无差别(都相当于一个点),所以我们使用带权并查集。

带权并查集的方法就十分显然了,对于love的边,连一条权值为0的边,对于hate的边,连一条权值为1的边(注意与题目所给的相反)。每次连边是顺便检查两点关系。

最后我们得到\(k\)个集合,把\(k\)个集合放入两个桶中,有\(2^k\)种方法,再去重,最后的答案就是\(2^{k-1}\)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fa[100100], val[100100];//fa是所在集合,val是与祖先关系 inline int read()// Fast input
{
int x=0,f=1; char ch=getchar();
for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
} int find(int x)//带权并查集
{
if (fa[x]==x) return x;
int t=find(fa[x]);
val[x]^=val[fa[x]];
return fa[x]=t;
} int main()
{
int n=read(), k=read();
for (int i=1; i<=n; i++) fa[i]=i;
for (int i=1; i<=k; i++)
{
int u=read(), v=read(), w=read()^1;
int fu=find(u), fv=find(v);
if (fu!=fv)
{
fa[fv]=fu;
val[fv]=val[u]^val[v]^w;
}
else
{
if (w && !(val[u]^val[v])) {puts("0"); return 0;}
if (!w && val[u]^val[v]) {puts("0"); return 0;}
}
}
int res=0, ans=1;
for (int i=1; i<=n; i++) if (find(i)==i) res++;
for (int i=1; i<=res-1; i++) ans=(ans*2)%mod;//(其实可以写quick_power的qwq
printf("%d\n",ans);
return 0;
}

CF553C Love Triangles的更多相关文章

  1. CF553C Love Triangles(二分图)

    Tyher推的好题. 题意就是给你一些好边一些坏边,其他边随意,让你求符合好坏坏~,或者只包含好好好的三元环的无向图个数. 坏坏的Tyher的题意是这样的. 再翻译得更加透彻一点就是:给你一些0(好边 ...

  2. Count the number of possible triangles

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

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

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

  4. acdream.Triangles(数学推导)

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

  5. UVA 12651 Triangles

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

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

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

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

  8. 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 ...

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

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

随机推荐

  1. Java 目标

    Java 技术 其次掌握的技能树主要有三个方面:第一个是基础,比如对集合类,并发包,IO/NIO,JVM,内存模型,泛型,异常,反射,等有深入了解,最好是看过源码了解底层的设计.比如一般面试都会问Co ...

  2. UNITY polygon collider不随物体旋转

    U3D中的一般包围框如 boxcollider, meshcollider, capsule collider等都会随物体旋转而旋转.然而polygon collider却不会. 补充:原来所有2D包 ...

  3. iOS开发者有价值的工具集

    转载于:http://www.cocoachina.com/applenews/devnews/2014/0307/7936.html 我一直比较推崇聪明地工作要远胜于刻苦地工作.使用正确的工具可以帮 ...

  4. hadoop之HDFS学习笔记(二)

    主要内容:hdfs的核心工作原理:namenode元数据管理机制,checkpoint机制:数据上传下载流程 1.hdfs的核心工作原理 1.1.namenode元数据管理要点 1.什么是元数据? h ...

  5. Country Meow

    Country Meow 和这基本一样 https://www.cnblogs.com/Fighting-sh/p/9809518.html #include<iostream> #inc ...

  6. 解题报告-Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. Oracle ADF 创建序列

    双击VO,打开Attribute 标签页在弹出的窗口中选择Default Value Type 为“Expression”, value 的值为:(new oracle.jbo.server.Sequ ...

  8. Linq操作DataTable

    IEnumerable<DataRow> q = from dr in dt.AsEnumerable()                          where dr.Field& ...

  9. qt4代码到qt5代码出错

    1.error C2039: “toAscii”: 不是“QString”的成员 QT5.0.1把这个函数取消掉了,可以用toLatin1这个函数

  10. 不要怂,就是GAN (生成式对抗网络) (一): GAN 简介

    前面我们用 TensorFlow 写了简单的 cifar10 分类的代码,得到还不错的结果,下面我们来研究一下生成式对抗网络 GAN,并且用 TensorFlow 代码实现. 自从 Ian Goodf ...