UESTC Training for Graph Theory——M、Network

Network

Time Limit: 5000 ms Memory Limit: 65536 kB Solved: 31 Tried: 177

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

In the first line of input there is an integer T, meaning there is T test cases.
For each test case,
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

For each test case, first output "Case #C: ", where C is the number of test case, from 1 to T. Then output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

1
4 1
1 2
2 3
1 4
3 4

Sample Output

Case #1: 3

Source

POJ Monthly--2007.10.06, Yang Mu

/*算法思想:
  给了一个数,还有一些边,要求我们删掉一条树边和一条添加的边,使得图不连通,问共有多少种方法
  我们可以给原树中每条边增加一个标记,统计的是树中每条边被新添加的边覆盖的次数。例如
   4---1---2
       |
       |
       3
  假设我们以 4 作为根节点,现在添加了一条边:2---3,那么 1--2 被覆盖一次,1--3 被覆盖一次,但
  是 4---1 没有被覆盖。如果在添加一条边:4---3,那么1---4 和 1---3 的覆盖次数又要+1,我们对每
  条边统计一下被覆盖的次数
  1、覆盖次数为1:这条边 + 覆盖它的新边一起删去是一种删边方案。总的方案数 +1
  2、覆盖次数为0:这条边 + 任何一条新边一起删去是一种覆盖方案。总的方案数 +m
  计算覆盖的次数的时候,我们可以这样做:
  从上面举的例子可以看见,边被覆盖的截止点是他们的最近公共祖先。那么用一个数组dp[i],表示i节点
  被覆盖的次数,那么每次覆盖两条边就可以将他们的dp值增 +1,而他们的最近公共祖先的dp值 -2
*/

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 400005
using namespace std;
int n,m;
bool vs[N];
int dp[N],fa[N];
long long ans;
int head1[N],head2[N],tot;
struct data
{
    int en,next;
} edge[N],newe[N];
int find_set(int x)
{
    if(x!=fa[x]) fa[x]=find_set(fa[x]);
    return fa[x];
}
void add_edge(int st,int en)  //向原来树中添加边
{
    edge[tot].en=en;
    edge[tot].next=head1[st];
    head1[st]=tot++;
}
void add_newe(int st,int en)  //向原来的树中添加新的边,记录在另一个边集中
{
    newe[tot].en=en;
    newe[tot].next=head2[st];
    head2[st]=tot++;
}
void tarjan(int v)  //tarjan找最近公共祖先,并且沿路更新节点的dp值
{
    vs[v]=true;
    for(int i=head2[v];i!=-1;i=newe[i].next)
        if(vs[newe[i].en])
            dp[find_set(newe[i].en)]-=2;  //最近公共祖先的dp值 -2
    for(int i=head1[v];i!=-1;i=edge[i].next)
        if(!vs[edge[i].en])
        {
            tarjan(edge[i].en);
            fa[edge[i].en]=v;
            dp[v]+=dp[edge[i].en];
            if(dp[edge[i].en]==0) ans+=m;  //统计
            if(dp[edge[i].en]==1) ans++;  //统计
        }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%d%d",&n,&m);
        tot=1;
        memset(head1,-1,sizeof(head1));
        int a,b;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
        }
        memset(head2,-1,sizeof(head2));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            if(a!=b)
            {
                add_newe(a,b);
                add_newe(b,a);
                dp[a]++;
                dp[b]++;
            }
        }
        for(int i=0;i<=n;i++)
            fa[i]=i;
        ans=0;
        memset(vs,0,sizeof(vs));
        tarjan(1);
        printf("Case #%d: %lld\n",ca,ans);
    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值