POJ 3417 Network
Last Update:2016-08-05
Source: Internet
Author: User
<span id="Label3"></p><p><p>Each additional edge is added to the diagram, which causes a path in the tree to ring, assuming that there are no remaining edges, then the new diagram is divided into two parts, and if you want to delete an edge on a loop path, you must remove the extra edge as Well.</p></p><p><p>So each additional edge joins, just +1 of the edges on the ring. Finally see how many times each side has been added, was added x times, that is, delete this edge, at least to delete the x-side to be divided into two halves, if not added at a time, meaning that the side of the cut is divided into two halves, the contribution of the answer is m; if added once, then the contribution of the answer is 1; Then there is no contribution to the Answer.</p></p><pre>#include <cstdio><span style="color: #000000;"><span style="color: #000000;">#include</span></span><cstring><span style="color: #000000;"><span style="color: #000000;">#include</span></span><cmath><span style="color: #000000;"><span style="color: #000000;">#include</span></span><algorithm><span style="color: #000000;"><span style="color: #000000;">#include</span></span><vector><span style="color: #000000;"><span style="color: #000000;">#include</span></span><map><span style="color: #000000;"><span style="color: #000000;">#include</span></span><<span style="color: #0000ff;"><span style="color: #0000ff;">Set</span></span>><span style="color: #000000;"><span style="color: #000000;">#include</span></span><queue><span style="color: #000000;"><span style="color: #000000;">#include</span></span><stack><span style="color: #000000;"><span style="color: #000000;">#include</span></span><iostream><span style="color: #0000ff;"><span style="color: #0000ff;">using</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">namespace</span></span><span style="color: #000000;"><span style="color: #000000;">Std;typedef</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span><span style="color: #000000;"><span style="color: #000000;">LL;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>Pi=acos (-<span style="color: #800080;"><span style="color: #800080;">1.0</span></span>), eps=1e-<span style="color: #800080;"><span style="color: #800080;">8</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">File () {freopen (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">D:\\in.txt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">R</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, stdin); Freopen (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">D:\\out.txt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">W</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, stdout);} Inline</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Read () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>c = GetChar ();<span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(!isdigit (C)) C =<span style="color: #000000;"><span style="color: #000000;">GetChar (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>x =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(isdigit (C)) {x = x *<span style="color: #800080;"><span style="color: #800080;">Ten</span></span>+ c-<span style="color: #800000;"><span style="color: #800000;">'</span></span><span style="color: #800000;"><span style="color: #800000;">0</span></span><span style="color: #800000;"><span style="color: #800000;">'</span></span>; c =<span style="color: #000000;"><span style="color: #000000;">GetChar ();} </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">x;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>maxn=<span style="color: #800080;"><span style="color: #800080;">100000</span></span>+<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">n,m,cnt,h[maxn],f[maxn];</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">struct</span></span>edge{<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>to,nx;} e[<span style="color: #800080;"><span style="color: #800080;">2</span></span>*<span style="color: #000000;"><span style="color: #000000;">maxn];</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">BOOL</span></span><span style="color: #000000;"><span style="color: #000000;">flag[maxn];</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Addedge (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>U<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">V) {e[cnt].to</span></span>=v; e[cnt].nx=h[u]; h[u]=cnt++<span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Dfs<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">x) {flag[x]</span></span>=<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i=h[x];i!=-<span style="color: #800080;"><span style="color: #800080;">1</span></span>; i=<span style="color: #000000;"><span style="color: #000000;">E[i].nx) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(flag[e[i].to])<span style="color: #0000ff;"><span style="color: #0000ff;">Continue</span></span><span style="color: #000000;"><span style="color: #000000;">; DFS (e[i].to); f[x]</span></span>=f[x]+<span style="color: #000000;"><span style="color: #000000;">f[e[i].to]; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>f[<span style="color: #800080;"><span style="color: #800080;">2</span></span>*maxn],b[<span style="color: #800080;"><span style="color: #800080;">2</span></span>*<span style="color: #000000;"><span style="color: #000000;">maxn],pos[maxn];</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>rmq[maxn*<span style="color: #800080;"><span style="color: #800080;">2</span></span>][<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">],done;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>DFS (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>U<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>P<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Dep) {f[</span></span>++done]=u,b[done]=<span style="color: #000000;"><span style="color: #000000;">dep; pos[u]</span></span>=<span style="color: #000000;">done <span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>son=h[u];son!=-<span style="color: #800080;"><span style="color: #800080;">1</span></span>; son=<span style="color: #000000;"><span style="color: #000000;">E[son].nx) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>v=<span style="color: #000000;"><span style="color: #000000;">e[son].to; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(v==p)<span style="color: #0000ff;"><span style="color: #0000ff;">Continue</span></span><span style="color: #000000;"><span style="color: #000000;">; DFS (V,U,DEP</span></span>+<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">); f[</span></span>++done]=u,b[done]=<span style="color: #000000;"><span style="color: #000000;">dep; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">initrmq () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">1</span></span>; I<=done;i++) rmq[i][<span style="color: #800080;"><span style="color: #800080;">0</span></span>]=<span style="color: #000000;"><span style="color: #000000;">i; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>j=<span style="color: #800080;"><span style="color: #800080;">1</span></span>;(<span style="color: #800080;"><span style="color: #800080;">1</span></span><<j) <=done;j++<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">1</span></span>; i+ (<span style="color: #800080;"><span style="color: #800080;">1</span></span><<j)-<span style="color: #800080;"><span style="color: #800080;">1</span></span><=done;i++<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(b[rmq[i][j-<span style="color: #800080;"><span style="color: #800080;">1</span></span>]]<b[rmq[i+ (<span style="color: #800080;"><span style="color: #800080;">1</span></span><< (j-<span style="color: #800080;"><span style="color: #800080;">1</span></span>))][j-<span style="color: #800080;"><span style="color: #800080;">1</span></span>]]) rmq[i][j]=rmq[i][j-<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span>rmq[i][j]=rmq[i+ (<span style="color: #800080;"><span style="color: #800080;">1</span></span><< (j-<span style="color: #800080;"><span style="color: #800080;">1</span></span>))][j-<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">];}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>LCA (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>A<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">B) {a</span></span>=pos[a],b=<span style="color: #000000;"><span style="color: #000000;">pos[b]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(a><span style="color: #000000;"><span style="color: #000000;">B) swap (a, a); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>K=log (<span style="color: #800080;"><span style="color: #800080;">1.0</span></span>+b-a)/log (<span style="color: #800080;"><span style="color: #800080;">2.0</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(b[rmq[a][k]]<b[rmq[b-(<span style="color: #800080;"><span style="color: #800080;">1</span></span><<k) +<span style="color: #800080;"><span style="color: #800080;">1</span></span>][k]])<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">f[rmq[a][k]]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>f[rmq[b-(<span style="color: #800080;"><span style="color: #800080;">1</span></span><<k) +<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">][k]];}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">main () {scanf (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">%d%d</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,&n,&<span style="color: #000000;"><span style="color: #000000;">m); </span>done</span>=cnt=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; Memset (h,-<span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span><span style="color: #000000;"><span style="color: #000000;">h); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<n-<span style="color: #800080;"><span style="color: #800080;">1</span></span>; i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>u,v; scanf<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">%d%d</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,&u,&<span style="color: #000000;"><span style="color: #000000;">v); Addedge (u,v); Addedge (v,u); } DFS (</span></span><span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); INITRMQ (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">1</span></span>; i<=m;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>u,v; scanf<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">%d%d</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,&u,&<span style="color: #000000;"><span style="color: #000000;">v); f[u]</span></span>++; f[v]++; F[lca (u,v)]-=<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">; } DFS (</span></span><span style="color: #800080;"><span style="color: #800080;">1</span></span>);<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ans=<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">2</span></span>; i<=n;i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(f[i]==<span style="color: #800080;"><span style="color: #800080;">0</span></span>) ans=ans+<span style="color: #000000;"><span style="color: #000000;">m; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(f[i]==<span style="color: #800080;"><span style="color: #800080;">1</span></span>) ans=ans+<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">; } printf (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">%d\n</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, ans); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span></pre><p><p></p></p><p><p>POJ 3417 Network</p></p></span>