【POJ3417】Network

9 篇文章 0 订阅
8 篇文章 0 订阅

Network


  • 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 Format

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 Format

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

  • Hint

【题目大意】
先给你一颗树,然后再在这棵树上加若干条边。接着你可以断掉一条原树上的边和一条新边。问有多少种方法可以使树变成两个部分以上。


  • 分析

建好原树后,每有一条新边,就把边的两个结点之间的路径被走过的次数加一。最终,如果原树上的边,一次都没被走过,说明他可以同时和任意一条新边断掉;如果被走过一次,那他只能和走过他的那条边一起断掉;如果被走过多次,那他不可能和其他的新边一起断掉(因为总会有其他新边连接着这两部分)。


#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max 100001
using namespace std;
int last[Max],Father[Max][21],Deep[Max],Tag[Max],n,m,u,v,Ans,tot;
struct Data{int to,next,back,c;}E[2*Max];
void Addline(int u,int v){
    E[++tot].to=v;E[tot].next=last[u];last[u]=tot;
    E[++tot].to=u;E[tot].next=last[v];last[v]=tot;
}
int Lca(int u,int v){
    if (Deep[v]>Deep[u]) swap(u,v);
    for (int i;Deep[u]>Deep[v];u=Father[u][i-1])
        for (i=1;i<=20 && Deep[Father[u][i]]>=Deep[v];i++);
    for (int i;u!=v;u=Father[u][i-1],v=Father[v][i-1])
        for (i=1;i<=20 && Father[u][i]!=Father[v][i];i++);
    return u;
}
void Dfs(int fa,int u){
    Deep[u]=Deep[fa]+1; Father[u][0]=fa;
    for (int i=1;i<=20;i++) Father[u][i]=Father[Father[u][i-1]][i-1];
    for (int i=last[u];i;i=E[i].next){
        if (fa==E[i].to) continue;
        Dfs(u,E[i].to);
    }
}
int GetTag(int fa,int u){
    for (int i=last[u];i;i=E[i].next){
        if (fa==E[i].to) continue;
        GetTag(u,E[i].to); Tag[u]+=Tag[E[i].to];
    }
    if (Tag[u]==0 && u!=1) Ans+=m;
    if (Tag[u]==1) Ans+=1;
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<n;i++){scanf("%d%d",&u,&v);Addline(u,v);}
    Dfs(0,1);
    for (int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        Tag[u]+=1; Tag[v]+=1; Tag[Lca(u,v)]-=2;
    }
    GetTag(0,1);
    printf("%d",Ans);
    fclose(stdin); fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值