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 ...
随机推荐
- leetcode877
public class Solution { public bool StoneGame(int[] piles) { return true; } } 这问题很不好...
- Eclipse去掉对jQuery的错误提示
1.I have found that I can leave the JavaScript Validator enable and ignore specific files by adding ...
- UVALive-7297-Hounded by Indecision
OK, maybe stealing the Duchess’s favorite ruby necklace was not such a good idea. You were makingyou ...
- iOS开发者有价值的工具集
转载于:http://www.cocoachina.com/applenews/devnews/2014/0307/7936.html 我一直比较推崇聪明地工作要远胜于刻苦地工作.使用正确的工具可以帮 ...
- python之with...as
[python之with...as] 参考:http://python.42qu.com/11155501
- DropDownList控件的使用方法
1. 使用代码添加数据 <asp:DropDownList ID="DropDownList1" runat="server"> </asp: ...
- 怎么查这个文件在linux下的哪个目录
因为要装pl/sql所以要查找tnsnames.ora文件..看看怎么查这个文件在linux下的哪个目录 find / -name tnsnames.ora 查到: /opt/app/oracle/p ...
- centos7 dubbokeeper安装
下载dubbokeeper源码 git clone https://github.com/dubboclub/dubbokeeper mysql 先执行install-mysql.sh 编译好 ...
- MVC和WebApi 使用get和post 传递参数(转)
出处:http://blog.csdn.net/qq373591361/article/details/51508806 我们总结一下用js请求服务器的传参方法. Get方式 Get主要是用来查询,一 ...
- Oracle学习笔记(十一)
例外: 例外是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性. 例外分为:系统例外自定义例外 系统例外分为:No_data_found(没有找到数据).Too_many_rows(selec ...