ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF553C Love Triangles

2020-11-25 18:02:01  阅读:206  来源: 互联网

标签:二分 10 ch Love int CF553C const 联通 Triangles


题目描述:

给出n个点,要求构造合法的完全图,已经给出了一些边。

边有爱边和恨边,其中任意三个点,连成的边合法的组合有爱爱爱,爱恨恨。

题解:

根据朋友的朋友是朋友,敌人的敌人是朋友这个道理,是构造合法二分图,二分图可以用染色法和并查集判。然后会有k个联通块,把联通快想成点以后,又需要成为二分图,每个联通块之间确认第一条边后,状态就被确定了。所有方案数是2的(k-1)次。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 4e5 + 10;
 5 const int M = 2e5 + 10;
 6 const ll mod = 1e9 + 7;
 7 inline int read()
 8 {
 9     int x = 0, f = 1; char ch = getchar();
10     while(ch < '0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
11     while(ch <= '9' && ch >= '0') {    x = (x << 1) + (x << 3) + ch -'0';ch = getchar();}
12     return x * f;
13 }
14  
15 int n, m, q;
16 int p[N], d[N];
17  
18 int find(int x)
19 {
20     if(p[x] == x) return x;
21     int t = find(p[x]);
22     d[x] ^= d[p[x]];
23     return p[x] = t;
24 }
25  
26 int main()
27 {
28     n = read(), m = read();
29     for (int i = 1; i <= n; i ++) p[i] = i;
30     for (int i = 1; i <= m; i ++)
31     {
32         int x = read(), y = read(), w= read() ^ 1;
33         int fx = find(x), fy = find(y);
34         int t = d[x] ^ d[y] ^ w;
35         if(fx != fy)
36         {
37             p[fx] = fy;
38             d[fx] = t;
39         }
40         else if(t & 1)
41         {
42             puts("0");
43             return 0;
44         }
45     }
46     int res = 0;
47     for (int i = 1; i <= n; i ++)
48     {
49         if(find(i) == i)
50         {
51             res ++;
52         }
53     }
54     ll ans = 1;
55     for (int i = 0; i < res - 1; i ++)
56         ans = ans * 2 % mod;
57     cout << ans << '\n';
58 }
View Code

 

标签:二分,10,ch,Love,int,CF553C,const,联通,Triangles
来源: https://www.cnblogs.com/xwdzuishuai/p/14037534.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有