{"id":768,"date":"2020-08-07T19:25:32","date_gmt":"2020-08-07T11:25:32","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=768"},"modified":"2020-08-07T21:03:59","modified_gmt":"2020-08-07T13:03:59","slug":"%e6%b1%82%e6%a0%91%e4%b8%8a%e4%bb%bb%e6%84%8f%e4%b8%a4%e7%82%b9%e9%97%b4%e8%b7%9d%e7%a6%bb%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/08\/07\/%e6%b1%82%e6%a0%91%e4%b8%8a%e4%bb%bb%e6%84%8f%e4%b8%a4%e7%82%b9%e9%97%b4%e8%b7%9d%e7%a6%bb%e4%b9%8b%e5%92%8c\/","title":{"rendered":"\u6c42\u6811\u4e0a\u4efb\u610f\u4e24\u70b9\u95f4\u8ddd\u79bb\u4e4b\u548c"},"content":{"rendered":"<h3><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2376\">HDU2376 Average distance<\/a><\/h3>\n<p>\u6c42\u6811\u4e0a\u4efb\u610f\u4e24\u70b9\u95f4\u8ddd\u79bb\u4e4b\u548c\uff1a\u6811\u6709n\u4e2a\u7ed3\u70b9\uff0c\u5bf9\u4e8e\u4e00\u6761\u8fb9\uff0c\u5047\u8bbe\u8fde\u63a5\u7684\u7ed3\u70b9\u662fu\uff0cv\uff0cu\u4e3a\u7236\u8282\u70b9\uff0c\u8bbe\u4ee5v\u4e3a\u6839\u7684\u5b50\u6811\u5171\u6709x\u4e2a\u7ed3\u70b9\uff0c\u90a3\u4e48\u8fd9\u6761\u8fb9\u5bf9\u7ed3\u679c\u7684\u8d21\u732e\u5c31\u662fvalue*x*(n-x)\uff0c\u5373\u8fd9\u6761\u8fb9\u5171\u88ab\u8ba1\u7b97\u4e86x*(n-x)\u6b21\uff01<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\ntypedef long long ll;\nconst ll maxn = 10005;\nstruct edge{\n    int from, to, next;\n    ll val;\n}e[maxn*2];\nll n, tot, ans;\nint head[maxn], vis[maxn];\nll dfs(int x){\n    vis[x] = 1;\n    ll sons = 1;\n    for (int i=head[x]; i; i=e[i].next) if (!vis[e[i].to]){\n        ll tmp = dfs(e[i].to);\n        sons += tmp;\n        ans += tmp*(n-tmp)*e[i].val;\n    }\n    return sons;\n}\ninline void add(int from, int to, int val){\n    tot++;\n    e[tot].from = from, e[tot].to = to, e[tot].val = val, e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\ninline int read(){\n    int x=0;    char ch = getchar();\n    while (ch&lt;&#039;0&#039; || ch&gt;&#039;9&#039;)    ch = getchar();\n    while (ch&gt;=&#039;0&#039; &amp;&amp; ch&lt;=&#039;9&#039;){\n        x = x*10 + ch-&#039;0&#039;;\n        ch = getchar();\n    }\n    return x;\n}\nint main(){\n    int t, from, to, val;\n    scanf (&quot;%d&quot;, &amp;t);\n    while (t--){\n        n = read(); tot = ans = 0;\n        for (int i=1; i&lt;=n; i++)    head[i] = vis[i] = 0;\n        for (int i=1; i&lt;n; i++){\n            from = read()+1, to = read()+1, val = read();\n            add(from, to, val); add(to, from, val);\n        }\n        dfs(1);\n        printf (&quot;%.6lf\\n&quot;, (double)ans\/(n*(n-1)\/2));\n    }\n    return 0;\n}<\/code><\/pre>\n<h4><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=6446\">HDU6446 Tree and Permutation<\/a><\/h4>\n<p>\u6c42\u6811\u4e0a\u4efb\u610f\u4e24\u70b9\u95f4\u8ddd\u79bb\u4e4b\u548c\uff0c\u518d\u4e58\u4e0a2*(n-1)!\uff0c\u5c31\u662f\u7b54\u6848\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\ntypedef long long ll;\nconst ll maxn = 1e5+5, mod = 1e9+7;\nstruct edge{\n    int from, to, next, val;\n}e[maxn*2];\nll n, fac[maxn];\nint head[maxn], tot, vis[maxn];\nll ans;\nll getSons(int x){\n    vis[x] = 1;\n    ll sons = 1;\n    for (int i=head[x]; i; i=e[i].next) if (!vis[e[i].to]){\n        ll tmp = getSons(e[i].to);\n        ans = (ans+tmp*(n-tmp)%mod*e[i].val%mod)%mod;\n        sons += tmp;\n    }\n    return sons;\n}\ninline void add(int from, int to, int val){\n    tot++;\n    e[tot].from = from, e[tot].to = to, e[tot].val = val, e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\nvoid init(){\n    fac[1] = 1;\n    for (ll i=2; i&lt;=100000; i++)   fac[i] = fac[i-1]*i%mod;\n    return;\n}\nint main(){\n    int from, to, val;\n    init();\n    while (~scanf (&quot;%lld&quot;, &amp;n)){\n        ans = 0;\n        tot = 0;\n        for (int i=1; i&lt;=n; i++)    head[i] = vis[i] = 0;\n        for (int i=1; i&lt;n; i++){\n            scanf (&quot;%d %d %d&quot;, &amp;from, &amp;to, &amp;val);\n            add(from, to, val); add(to, from, val);\n        }\n        getSons(1);\n        printf (&quot;%lld\\n&quot;, ans*2*fac[n-1]%mod);\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HDU2376 Average distance \u6c42\u6811\u4e0a\u4efb\u610f\u4e24\u70b9\u95f4\u8ddd\u79bb\u4e4b\u548c\uff1a\u6811\u6709n\u4e2a\u7ed3\u70b9\uff0c\u5bf9\u4e8e\u4e00\u6761\u8fb9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/768"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":3,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":772,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/768\/revisions\/772"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}