关闭

POJ 3417 Network(dp+tarjian LCA)

359人阅读 评论(0) 收藏 举报
分类:
Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4929   Accepted: 1413

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 buildsM 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 theM 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 nodea and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between nodea 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

【题意】给了一颗树和一些新的边的关系,要你毁掉一条树边和一条新的边,问你有多少种方法使得树不连通。

【解题方法】

我们知道加入新边必定会成环,可以发现
对于被环覆盖2次及以上的树边,贡献是0
对于被环覆盖1次的树边,贡献是1
对于被环覆盖0次的树边,贡献是M
然后用树形dp,dp[u]:=(u,f)这条边被环覆盖的次数 对于(u,v),先计数dp[u]++,dp[v]++,自底向上dp之后就可以把环上的点都更新了,但是对于环上的点来说显然lca要减去,dp[lca]−=2

【AC 代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 200010;
int dp[maxn],n,m;//dp[i]表示i到fa[i]的被覆盖次数
int headE[maxn],tot1;
struct Edge{
    int v,next;
}E[maxn*2];
void addEdge(int u,int v)
{
    E[tot1].v=v,E[tot1].next=headE[u],headE[u]=tot1++;
}
void init1()
{
    memset(headE,-1,sizeof(headE));
    tot1=0;
}

int headQ[maxn],tot2;
struct Query{
    int v,next,id;
}Q[maxn<<2];
void addQuery(int u,int v,int i)
{
    Q[tot2].v=v,Q[tot2].id=i,Q[tot2].next=headQ[u],headQ[u]=tot2++;
}
void init2()
{
    memset(headQ,-1,sizeof(headQ));
    tot2=0;
}

bool vis[maxn],check[maxn];
int ancestor[maxn];
int Find(int x)
{
    if(x==ancestor[x]) return x;
    else return ancestor[x]=Find(ancestor[x]);
}
void tarjian(int u)
{
    ancestor[u]=u;
    vis[u]=true;
    for(int i=headE[u]; ~i; i=E[i].next)
    {
        int v=E[i].v;
        if(vis[v]) continue;
        tarjian(v);
        ancestor[v]=u;
    }
    for(int i=headQ[u]; ~i; i=Q[i].next)
    {
        int v=Q[i].v,id=Q[i].id;
        if(!vis[v]) continue;
        if(check[id]) continue;
        check[id]=true;
        int lca=Find(v);
        dp[lca]-=2;
    }
}
void dfs(int u,int fa)
{
    for(int i=headE[u]; ~i; i=E[i].next)
    {
        int v=E[i].v;
        if(v==fa) continue;
        dfs(v,u);
        dp[u]+=dp[v];
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v;
        init1();
        init2();
        memset(dp,0,sizeof(dp));
        for(int i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            addEdge(u,v);
            addEdge(v,u);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            addQuery(u,v,i);
            addQuery(v,u,i);
            ++dp[u],++dp[v];
        }
        memset(vis,false,sizeof(vis));
        memset(check,false,sizeof(check));
        tarjian(1);
        dfs(1,-1);
        int ans=0;
        for(int i=2; i<=n; i++)
        {
            if(dp[i]==0) ans+=m;
            else if(dp[i]==1) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}



0
0
查看评论

POJ - 3417 Network(LCA + DP)

题目大意:给出一棵N个结点的无根树,现在要在上面加上M条边,问,有多少种破坏方式(破坏一条树边,一条新边),能使这张新图变成至少两部分解题思路:首先,假设添加的边为(u,v),那么u – > lca(u,v) –> v – >u就形成了一个环了,也就是说,每条添加的边都会在树上形成...
  • L123012013048
  • L123012013048
  • 2015-08-18 00:42
  • 506

poj3417Network(LCA+dfs)

题意:给一个树,然后又在树上加了好多的边,现在问你删除一条原先的边和新加的边的一条,有多少中方法使得树不连通思路:对于新加的一条边来说,肯定会与之前的树形成一个环,而此时环内的所有边删除并且新加的这条变也删除就会是一种方案,而这道题是将所有新边都加入后的情况,那么我们看每条边,如果没有与它形成环的情...
  • zcmartin2014214283
  • zcmartin2014214283
  • 2016-07-10 12:39
  • 534

POJ 3417 Network LCA

POJ 3417 Network LCA
  • NNDXNM
  • NNDXNM
  • 2015-05-15 20:46
  • 401

POJ 3417 Network (LCA + DP)

题意:先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂。 我们知道,这m条边连上后这颗树必将成环,假设新边为(u,v),那么环为u---->LCA(u,v)------->v-------->...
  • u011788531
  • u011788531
  • 2015-03-04 10:32
  • 802

【LCA】 POJ 3417 Network 记数

mark 明天再写 #include #include #include #include #include #include #include #include using namespace std; #include #include #include #include...
  • u012749539
  • u012749539
  • 2014-10-13 22:26
  • 503

POJ - 3417 Network 【LCA模板+树形DP】

POJ - 3417入口  题意:先给出一棵n个点的无根树,然后下面再给出m条边,把这m条边连上,然后每次你能破坏两条边,规定一条是树边,一条是新边,问有多少种方案能使树分为两部分。 思路:我们知道,这m条边连上后这颗树必将成环,假设新边为(u,v),那么环为u---->...
  • m0_37953323
  • m0_37953323
  • 2017-09-22 21:50
  • 95

poj 3417(LCA应用)

Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2496   Accepted: 726 D...
  • fp_hzq
  • fp_hzq
  • 2011-09-15 11:24
  • 1385

POJ 3417 Network (LCA应用,5级)

K - Network Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO For...
  • nealgavin
  • nealgavin
  • 2013-09-06 21:33
  • 640

*POJ 3417 - Network(LCA + 树形DP)

题目: http://poj.org/problem?id=3417 题意: 给出一棵有n个节点的树,新加入m条边,求出断开一条旧边和一条心边,使得图变成两个连通块的方案数。 思路: (摘抄) 我们知道,这m条边连上后这颗树必将成环,假设新边为(u,v),那么环为u----&...
  • u013534690
  • u013534690
  • 2015-07-30 09:49
  • 328

LCA+poj1986

Language: Default Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 9081   ...
  • u010660276
  • u010660276
  • 2014-08-09 21:03
  • 444
    个人资料
    • 访问:415855次
    • 积分:16535
    • 等级:
    • 排名:第744名
    • 原创:1274篇
    • 转载:21篇
    • 译文:0篇
    • 评论:61条
    博客专栏
    文章分类