I - Nice to Meet You
Time limit : 2sec / Memory limit : 256MB
Score : 100 points
Problem Statement
There are N islands floating in Ringo Sea, and M travel agents operate ships between these islands. For convenience, we will call these islands Island 1, 2, …, N, and call these agents Agent 1, 2, …, M.
The sea currents in Ringo Sea change significantly each day. Depending on the state of the sea on the day, Agent i (1≤i≤M) operates ships from Island ai to bi, or Island bi to ai, but not both at the same time. Assume that the direction of the ships of each agent is independently selected with equal probability.
Now, Takahashi is on Island 1, and Hikuhashi is on Island 2. Let P be the probability that Takahashi and Hikuhashi can travel to the same island in the day by ships operated by the M agents, ignoring the factors such as the travel time for ships. Then, P×2M is an integer. FindP×2M modulo 109+7.
Constraints
- 2≤N≤15
- 1≤M≤N(N−1)⁄2
- 1≤ai<bi≤N
- All pairs (ai,bi) are distinct.
Input
Input is given from Standard Input in the following format:
N M a1 b1 : aM bM
Output
Print the value P×2M modulo 109+7.
Sample Input 1
4 3 1 3 2 3 3 4
Sample Output 1
6
The 2M=8 scenarios shown above occur with equal probability, and Takahashi and Hikuhashi can meet on the same island in 6 of them. Thus, P=6⁄2M and P×2M=6.
Sample Input 2
5 5 1 3 2 4 3 4 3 5 4 5
Sample Output 2
18
Sample Input 3
6 6 1 2 2 3 3 4 4 5 5 6 1 6
Sample Output 3
64
一个 n 个点,m 个边,的图,你要为每条边定向。
使得从 1 出发和从 2 出发的两个人可以见面。
分析:
直接做并不好做。
全部减去见不到的。
枚举集合 i,j 计算从 1 恰好能到 i,从 2 恰好能到 j 的方案数。
i,j 不相交,时间复杂度 O(3 n )。
1 恰好能到 i 集合的方案数,怎么做呢,容斥。
设 c i 是两端都在 i 集合内的边。
设 d i 是至少一端都在 i 集合内的边。
i 集合内的边随意定向 2 c[i] 。
枚举 i 的子集 j,全部减去 f[j] 乘以 2 c[i−j]
好的,思路很好,可是我写不出来,只有借鉴一位大(ju)佬的程序:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[120], b[120], p = 1000000007;
int c[32777], d[32777], t[120];
long long f[2][32777], z;
int main() {
scanf("%d%d", &n, &m);
for (int i = t[0] = 1; i <= 105; i++) {
t[i] = t[i - 1] * 2 % p;
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]--;
b[i]--;
}
for (int i = 0; i < 1 << n; i++) {
for (int j = 0; j < m; j++) {
if ((i >> a[j] & 1) & (i >> b[j] & 1)) {
c[i]++;
}
if ((i >> a[j] & 1) | (i >> b[j] & 1)) {
d[i]++;
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1 << n; j++) {
if (~j >> i & 1) {
continue;
}
f[i][j] = t[c[j]];
for (int k = j; k > 0; k = (k - 1) & j) {
f[i][j] = (f[i][j] + p - f[i][j - k] * t[c[k]]) % p;
}
}
}
for (int i = 0; i < 1 << n; i++) {
for (int j = 0; j < 1 << n; j++) {
if ((i & j) || c[i] + c[j] != c[i | j]) {
continue;
}
z = (z + f[0][i] * f[1][j] % p * t[m - d[i | j]]) % p;
}
}
printf("%d\n", (t[m] + p - z) % p);
}
再看几遍,还是不会。。
那我补充几个知识:
看完了,就差不多了。