AtCoder Grand Contest 049A - Erasing Vertices

题目链接

Problem Statement

We have a directed graph with N N N vertices numbered 1 1 1 to N N N. N N N strings of length N N N each, S 1 , S 2 , … , S N S_1,S_2,\ldots,S_N S1​,S2​,…,SN​, represent the edges in the graph. Specifically, S i , j S_{i,j} Si,j​ is 1 if there is an edge from Vertex i i i to Vertex j j j, and 0 otherwise. The graph has no self-loops and no multi-edges.

Until the graph becomes empty, AtCobear will repeat the following operation:

  • Choose one (unerased) vertex uniformly at random (independently from the previous choices). Then, erase that vertex and all vertices that are reachable from the chosen vertex by traversing some edges. Erasing a vertex will also erase the edges incident to it.

Find the expected value of the number of times the operation is done.

Constraints

  • 1 ≤ N ≤ 100 1 \leq N \leq 100 1≤N≤100
  • S i S_i Si​ is a string of length N N N consisting of 0 and 1.
  • S i , i = S_{i,i}= Si,i​=0

Input

Input is given from Standard Input in the following format:

N N N
S 1 S_1 S1​
S 2 S_2 S2​
⋮ \vdots ⋮
S N S_N SN​

Output

Print the expected value of the number of times the operation is done. Your output will be considered correct when its absolute or relative error from our answer is at most 1 0 − 9 10^{-9} 10−9.


Sample Input 1

Copy

3
010
001
010

Sample Output 1

Copy

1.66666666666666666667

We have the following three scenarios happening with equal probability:

  • Choose Vertex 1 1 1 in the first operation, erasing Vertex 1 1 1, 2 2 2, and 3 3 3. The graph is now empty, so we are done.
  • Choose Vertex 2 2 2 in the first operation, erasing Vertex 2 2 2 and 3 3 3. Then, choose Vertex 1 1 1 in the second operation, erasing Vertex 1 1 1. The graph is now empty, so we are done.
  • Choose Vertex 3 3 3 in the first operation, erasing Vertex 2 2 2 and 3 3 3. Then, choose Vertex 1 1 1 in the second operation, erasing Vertex 1 1 1. The graph is now empty, so we are done.

Thus, the expected value of the number of times the operation is done is ( 1 + 2 + 2 ) / 3 = 5 / 3 (1+2+2)/3=5/3 (1+2+2)/3=5/3.


Sample Input 2

Copy

3
000
000
000

Sample Output 2

Copy

3.00000000000000000000

There will always be three operations.


Sample Input 3

Copy

3
011
101
110

Sample Output 3

Copy

1.00000000000000000000

There will always be one operation.

考虑每一个点 x x x ,如果将点 x x x 删掉,需要选择点集 S S S 中的点,如果恰好选择点 x x x ,则 x x x 对总次数贡献 1 1 1 ,而这种事发生的概率为 1 ∣ S ∣ \frac{1}{|S|} ∣S∣1​,因此点 x x x 的期望贡献为 1 ∣ S ∣ \frac{1}{|S|} ∣S∣1​ 。因此最终期望为: ∑ i = 1 n 1 ∣ S i ∣ \sum\limits_{i=1}^n\frac{1}{|S_i|} i=1∑n​∣Si​∣1​ 。

#include<bits/stdc++.h>

using namespace std;
const int N = 105;
int head[N], ver[N * N << 1], Next[N * N << 1], tot;
int siz[N], n;
bool v[N];
char a[N];

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

void dfs(int x) {
    siz[x]++, v[x] = true;
    for (int i = head[x]; i; i = Next[i])
        if (!v[ver[i]])dfs(ver[i]);
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%s", a + 1);
        for (int j = 1; j <= n; ++j)
            if (a[j] == '1')add(i, j);
    }
    for (int i = 1; i <= n; ++i) {
        memset(v, false, sizeof(bool) * (n + 1));
        dfs(i);
    }
    double ans = 0;
    for (int i = 1; i <= n; ++i)ans += 1.0 / siz[i];
    printf("%.9lf", ans);
    return 0;
}
上一篇:LeetCode笔记:Weekly Contest 221 比赛记录


下一篇:acm-(dp、最小树形图)Sichuan State Programming Contest 2011 I.Smart Typist