码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-03-31 14:31:51      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

 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(二分图染色)

标签:

原文地址:http://www.cnblogs.com/zstu-jack/p/5340842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!