ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AtCoder Grand Contest 049A - Erasing Vertices

2020-12-25 12:59:44  阅读:264  来源: 互联网

标签:Erasing AtCoder Contest int graph Vertex done Input operation


题目链接

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;
}

标签:Erasing,AtCoder,Contest,int,graph,Vertex,done,Input,operation
来源: https://blog.csdn.net/qq_45323960/article/details/111670362

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有