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 ...
随机推荐
- ASP.NET中高级程序员 面试题
1. 简要说一下.Net的编译过程. 2.ASP.NET与ASP的区别 3.谈一下ASP.NET页面生命周期 4.ASP.NET程序的运行机制.可以从一个页面的请求到返回的角度谈 5.Javascri ...
- C#流对象使用完后不立即释放的问题
public class testwriter : MonoBehaviour { // Use this for initialization void Start () { } [MenuItem ...
- 【316】python.requests 读取网页信息
参考:Python:在网页中查找字符串的一般方法--in 参考:python怎么安装requests 参考:Requests 快速上手 操作步骤如下: 添加环境变量,将 python 所在文件夹添加至 ...
- Python_01-入门基础
以后我会发表一系列python脚本的学习资料,python版本为2.x. 目录: 1 Python入门基础 1.1 学习资源 1.2 所有语言的入门程序---Hello World! 1.3 帮助函 ...
- 【hdu3507】Print Article 【斜率优化dp】
题意 https://cn.vjudge.net/problem/HDU-3507 分析 斜率优化的模板题 #include <cstdio> #include <cstring&g ...
- UNITY5 为什么Inspector视图中脚本前面的勾选框没了
结果发现了一个奇葩的问题..凡事脚本没有勾选项的,都是因为你的脚本没有Start方法..如果你想让勾选框显示出来,把如下方法加入即可,不信你可以试试.嘿嘿.
- PHP ImageMagick
简介: ImageMagick是用C语言开发图片处理程序.可以对图片进行改变大小.旋转.锐化.减色或增加特效等操作.对图片的操作,即可以通过命令行进行,也可以用C/C++.Perl.Jav ...
- 转载:Candy? 在线性时间内求出素数与欧拉函数
转载自:http://www.cnblogs.com/candy99/p/6200660.html 2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB ...
- python 开发简单的聊天工具-乾颐堂
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...
- Java程序设计11——GUI设计与事件处理B
4 Java事件模型的流程 为了使图形界面能够接收用户的操作,必须给各个组件加上事件处理机制. 在事件处理的过程中,主要涉及3类对象: 1.Event Source(事件源):事件发生的场所,通常就是 ...