| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 2496 | Accepted: 726 |
Description
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN's network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.
As the DN's best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.
Input
The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.
Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.
Output
Output a single integer — the number of ways to divide the network into at least two parts.
Sample Input
4 1 1 2 2 3 1 4 3 4
Sample Output
3
Source
#include<cstdio>
using namespace std;
const int mm=444444;
const int mn=111111;
int t[mm],p[mm];
int h[mn]={0},q[mn]={0},f[mn],mk[mn]={0};
bool vis[mn]={0};
int i,j,k,n,m,e,ans=0;
inline void add(int u,int v,int h[])
{
t[e]=v,p[e]=h[u],h[u]=e++;
t[e]=u,p[e]=h[v],h[v]=e++;
}
int find(int x)
{
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
void tarjan(int u)
{
int i,v;
vis[f[u]=u]=1;
for(i=q[u];i;i=p[i])
if(vis[v=t[i]])mk[find(v)]-=2;
for(i=h[u];i;i=p[i])
if(!vis[v=t[i]])
{
tarjan(v),f[v]=u,mk[u]+=mk[v];
if(mk[v]==1)++ans;
if(mk[v]==0)ans+=m;
}
}
void get(int &a)
{
char c;
while((c=getchar())<'0'||c>'9');
for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0';
}
int main()
{
get(n),get(m);
for(e=k=1;k<n;++k)get(i),get(j),add(i,j,h);
for(k=1;k<=m;++k)
{
get(i),get(j);
if(i!=j)add(i,j,q),++mk[i],++mk[j];
}
tarjan(1);
printf("%d\n",ans);
return 0;
}

本文探讨了在面临竞争对手攻击的情况下,如何通过构建新连接来保护网络,并提供了量化网络可分割方式的方法。
527

被折叠的 条评论
为什么被折叠?



