Codeforces Round #309 (Div. 1) C. Love Triangles(二分图)
2015-07-03 10:44
375 查看
C. Love Triangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters.
The characters are labeled from 1 to n.
Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).
You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates
both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).
You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs
hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count
modulo 1 000 000 007.
Input
The first line of input will contain two integers n, m (3 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000).
The next m lines will contain the description of the known relationships. The i-th
line will contain three integers ai, bi, ci.
If ci is
1, then aiand bi are
in love, otherwise, they hate each other (1 ≤ ai, bi ≤ n, ai ≠ bi,
).
Each pair of people will be described no more than once.
Output
Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo1 000 000 007.
Sample test(s)
input
output
input
output
input
output
Note
In the first sample, the four ways are to:
Make everyone love each other
Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this).
In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.
明确一点,同一个连通分量里的动物只能相互喜欢或讨厌,否则就会出现三角形。用二分图判断(和无向图不太一样)一下,不同的联通分量合并的方案数是2^(分量个数-1)
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
const int maxn = 100005;
const int MOD = (int)1e9 + 7;
vector <pii> E[maxn];
bool fail, vis[maxn];
int g[maxn];
int modExp(int a, int b) {
long long t = 1, y = a;
while(b) {
if(b & 1) t = t * y % MOD;
y = y * y % MOD;
b >>= 1;
}
return t;
}
void bipartite(int x, int y) {
if(fail) return;
vis[x] = 1;
g[x] = y;
for(int i = 0; i < E[x].size(); i++) {
int u = E[x][i].first;
int v = E[x][i].second;
int ny = 3 - y;
if(v == 1) ny = y;
if(!vis[u]) {
bipartite(u,ny);
} else {
if(ny != g[u]) fail = 1;
}
}
}
int main() {
int n, m, a, b, c;
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; i++) E[i].clear();
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
E[a].push_back(make_pair(b,c));
E[b].push_back(make_pair(a,c));
}
int idx = 0;
fail = 0;
memset(vis,0,sizeof vis);
for(int i = 1; i <= n && !fail; i++) {
if(!vis[i]) {
bipartite(i, 1);
idx++;
}
}
if(fail) {
printf("0\n");
} else {
printf("%d\n", modExp(2, idx - 1));
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters.
The characters are labeled from 1 to n.
Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).
You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates
both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).
You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs
hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count
modulo 1 000 000 007.
Input
The first line of input will contain two integers n, m (3 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000).
The next m lines will contain the description of the known relationships. The i-th
line will contain three integers ai, bi, ci.
If ci is
1, then aiand bi are
in love, otherwise, they hate each other (1 ≤ ai, bi ≤ n, ai ≠ bi,
).
Each pair of people will be described no more than once.
Output
Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo1 000 000 007.
Sample test(s)
input
3 0
output
4
input
4 41 2 12 3 13 4 04 1 0
output
1
input
4 41 2 12 3 13 4 04 1 1
output
0
Note
In the first sample, the four ways are to:
Make everyone love each other
Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this).
In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.
明确一点,同一个连通分量里的动物只能相互喜欢或讨厌,否则就会出现三角形。用二分图判断(和无向图不太一样)一下,不同的联通分量合并的方案数是2^(分量个数-1)
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
const int maxn = 100005;
const int MOD = (int)1e9 + 7;
vector <pii> E[maxn];
bool fail, vis[maxn];
int g[maxn];
int modExp(int a, int b) {
long long t = 1, y = a;
while(b) {
if(b & 1) t = t * y % MOD;
y = y * y % MOD;
b >>= 1;
}
return t;
}
void bipartite(int x, int y) {
if(fail) return;
vis[x] = 1;
g[x] = y;
for(int i = 0; i < E[x].size(); i++) {
int u = E[x][i].first;
int v = E[x][i].second;
int ny = 3 - y;
if(v == 1) ny = y;
if(!vis[u]) {
bipartite(u,ny);
} else {
if(ny != g[u]) fail = 1;
}
}
}
int main() {
int n, m, a, b, c;
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; i++) E[i].clear();
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
E[a].push_back(make_pair(b,c));
E[b].push_back(make_pair(a,c));
}
int idx = 0;
fail = 0;
memset(vis,0,sizeof vis);
for(int i = 1; i <= n && !fail; i++) {
if(!vis[i]) {
bipartite(i, 1);
idx++;
}
}
if(fail) {
printf("0\n");
} else {
printf("%d\n", modExp(2, idx - 1));
}
}
return 0;
}
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Div2 244AB
- Coder-Strike 2014 - Finals (online edition, Div. 2)AB
- Codeforces Div2 243AB
- Codeforces Div2 242AB
- Codeforces 193 Div 2