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 ...
随机推荐
- centos7 二进制安装MySQL5.7.22
1.详细描安装的过程 1.1关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service ...
- TNS:no listener error in Oracle XE after changing computer name
This morning at work when trying to log on to my computer I noticed not my username on login screen ...
- 【Directshow】IFilterGraph::AddFilter方法参数问题<转>
看dx里dshow的例子和别人的程序,用IFilterGraph::AddFilter()这个API添加filter到Graph的时候,第二个参数是一个设备友好名称: 我看有的填的是通过IProper ...
- dom4j读取XML文件内容
<?xml version="1.0" encoding="UTF-8"?> <RESULT> <VALUE> <NO ...
- 开源地理空间基金会OSGeo简介
开源地理空间基金会 OSGeo 相关站点: OSGeo官方站点:http://www.osgeo.org/home OSGeo中国中心:http://www.osgeo.cn/ OSGeo GitHu ...
- editplus 链接FTP失败,超时
最近在用editplus链接服务器是出现了超时连接不上的情况 检查后发现FTP配置没问题 后来打开高级设置后发现没有配置端口号 配置后登陆成功
- Django+python实现网页数据的excel导出
一直都想做一个网页的excel导出功能,最近抽时间研究了下,使用urllib2与BeautifulSoup及xlwt模块实现 urllib2这个模块之前有用过,关于BeautifulSoup模块,可参 ...
- matplotlib —— 添加文本信息(text)
[详细]http://hyry.dip.jp/tech/book/page/scipy/matplotlib_fast_plot.html http://blog.csdn.net/lanchunhu ...
- 若p是与10互质的质数,则p-1个9能被p整除
[若p是与10互质的质数,则k(p-1)个9能被p整除] 因为(p,10)=1,所以(p,10^k)=1.根据费马定理,10^(k*(p-1))-1|p. 而10^k*(p-1)-1是一个位数为(p- ...
- jmeter 使用cookie管理器
1.jmeter.properties 中 将CookieManager.save.cookies 设置为true 2.添加一个cookie管理器,什么都不用填 3.把需要用到的请求放到登录后面.后 ...