CF553C Love Triangles
题目链接
题意:给定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的更多相关文章
- CF553C Love Triangles(二分图)
Tyher推的好题. 题意就是给你一些好边一些坏边,其他边随意,让你求符合好坏坏~,或者只包含好好好的三元环的无向图个数. 坏坏的Tyher的题意是这样的. 再翻译得更加透彻一点就是:给你一些0(好边 ...
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)
Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...
- acdream.Triangles(数学推导)
Triangles Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Stat ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- 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 ...
- 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 ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
随机推荐
- FDQuery sqlserver 临时表
用FDQuery执行创建临时表,查不到临时表,用ADOQuery和BDEQuery均正常,比较发现用ADOQuery执行的时候只有SQL没有调用sql的系统存储过程sp_prepexec. 是fdqu ...
- gen_projective_mosaic(Halcon例子)
* This example program shows how several images of a PCB can be combined * into a large mosaic image ...
- 迷你MVVM框架 avalonjs 0.93发布
这段时间吸取@limodou, @东灵等人的意见,做了以下改进 重构isArrayLike,提高avalon.each的性能,原来avalon.each是依赖于isArrayLike来判定是循环普通对 ...
- 新手C#构造函数、继承、组合的学习2018.08.06/07
构造函数,是一种特殊的方法.主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同 ...
- ShaderForge打造自定义光照模型
[ShaderForge打造自定义光照模型] 1.Lambert逻辑图. 2.Blinn-Phong逻辑图. 参考:https://www.youtube.com/watch?v=EjCXwV0YYd ...
- 并发之CAS无锁技术
CAS算法即是:Compare And Swap,比较并且替换: CAS算法存在着三个参数,内存值V,旧的预期值A,以及要更新的值B.当且仅当内存值V和预期值B相等的时候,才会将内存值 ...
- 01c-1: 主流长远
- Multithreading in C
Multithreading in C, POSIX(可移植操作系统接口Portable Operating System Interface X ) style Multithreading - A ...
- mybatis框架入门程序:演示通过mybatis实现数据库的删除操作
1.mybatis的基本配置工作可以在我的这篇博客中查看:https://www.cnblogs.com/wyhluckdog/p/10149480.html 2.删除用户的映射文件: <!-- ...
- 安装scrapy解决的坑
在windows下: pip install scrapy 最后一步报错: 本来以为是缺少 Microsoft Visual C++ 14.0 这个库,一看下载需要400多M,算了再看下报错... 感 ...