cf Round #309 (Div. 2) E - Love Triangles(二分图染色)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf Round #309 (Div. 2) E - Love Triangles(二分图染色)相关的知识,希望对你有一定的参考价值。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 
 5 #include<string> 
 6 #include<vector>
 7 #include<set>
 8 #include<map>
 9 #include<queue>
10 #include<math.h>
11 
12 #include<algorithm>
13 #include<iostream>
14 
15 const int INF = 0x7f7f7f7f;
16 const int MOD = 1000000007;
17 using namespace std;
18 typedef long long ll;
19 
20 vector<pair<int,int> > vec[100005];
21 int vis[100005];
22 ll ans;
23 
24 void dfs(int u){
25     for(int i = 0 ; i < vec[u].size() ; i ++){
26         int v= vec[u][i].first;
27         int p = vec[u][i].second;
28         if(vis[v] == -1){
29             if(p == 1){
30                 vis[v] = vis[u];
31             }else{
32                 vis[v] = !vis[u];
33             }
34             dfs(v);    
35         }
36         if(p == 0 && vis[u] == vis[v]) ans = 0;
37         if(p == 1 && vis[u] != vis[v]) ans = 0;
38     }
39 }
40 
41 int main(){
42     
43     int n,m;
44     while(scanf("%d%d",&n,&m) != EOF){
45         for(int i = 0 ; i <= n ; i ++)    vec[i].clear();
46         for(int i = 0 ; i < m ; i ++){
47             int u,v,r;
48             scanf("%d%d%d",&u,&v,&r);
49             vec[u].push_back(make_pair(v,r));
50             vec[v].push_back(make_pair(u,r));
51         }
52         
53         for(int i = 0 ; i <= n ; i ++)    vis[i] = -1;
54             
55         ans = (MOD+1)/2;
56         for(int i = 1 ; i <= n ; i ++){        
57             if(vis[i] == -1){                
58                 vis[i] = 0;
59                 ans = (ans*2)%MOD;
60                 dfs(i);
61             }    
62         }
63         cout << ans << endl;
64     }
65     return 0;
66 }

 

以上是关于cf Round #309 (Div. 2) E - Love Triangles(二分图染色)的主要内容,如果未能解决你的问题,请参考以下文章

CF778B(round 402 div.2 E) Bitwise Formula

B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))

Educational Codeforces Round 107 (Rated for Div. 2,CF1511)A~E题解

Codeforces Round #357 (Div. 2) E 计算几何

题解 Educational Codeforces Round 80 [Rated for Div. 2](CF1288)

Codeforces Round #799(Div. 4,CF1692)全题解