• POJ3417 Network


    Network

    Language:
    Network
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 7879Accepted: 2253

    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

    给出一个无向图,分别给出n-1条树边(主要边)和m条非树边(附加边),这个无向图可以看做一棵树外加m条附加边,你可以切断一条主要边和一条附加边,求切割后,能够使这个无向图不再连通的切割方案数(即使只切断一条主要边就可以使图不连通,你也需要再切断一条附加边)

    题解

    参照Zolrk的题解。

    我们先考虑只有一条附加边(x,y)时,这时这张图就是一棵基环树

    我们发现如果x,y之间有一条附加边,则这条边和x到y的路径组成了一个环,如果说我们要切割x到y的路径上的一条主要边,我们必须要再切断这条附加边,才能使图不再连通

    那么如果x,y之间有两条或以上附加边,若我们已切割了x到y路径上的一条主要边,那么是无法通过仅再切割一条附加边来使图不再连通

    因而我们每次读入一条附加边,就给x到y的路径上的所有主要边记录上“被覆盖一次”,这样再去遍历所有主要边
    对于我们想要切割的一条主要边,有以下3种情况

    1. 若这条边被覆盖0次,则可以任意再切断一条附加边
    2. 若这条边被覆盖1次,那么只能再切断唯一的一条附加边
    3. 若这条边被覆盖2次及以上,没有可行的方案

    现在的问题是如何快速求出每条边被覆盖了多少次,对于这类问题,可以类比序列差分,有树上差分算法
    设差分数组dif初值为0,若x,y有一条附加边,则dif[x]++,dif[y]++,dif[lca(x,y)]-=2
    设f(x)为以x为根的子树中所有节点dif之和,则f(x)就是x到其父节点的边被覆盖的次数
    没错,求的是子树和,所以说求各种类似前缀和的东西真的很好用,一般可以用这些方式对区间O(1)求解

    #include<iostream>
    #include<cstring>
    #include<vector>
    #define rg register
    #define il inline
    #define co const
    template<class T>il T read(){
        rg T data=0,w=1;rg char ch=getchar();
        for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
        for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
        return data*w;
    }
    template<class T>il T read(rg T&x) {return x=read<T>();}
    typedef long long ll;
    using namespace std;
    typedef pair<int,int> pii;
    
    co int N=2e5+1;
    int n,m,fa[N],ans[N],v[N],f[N],ANS=0;
    vector<int> e[N];
    vector<pair<int,int> > q[N];
    int get(int x) {return fa[x]==x?x:fa[x]=get(fa[x]);}
    void tarjan(int x){
    	v[x]=1;
    	for(int i=0,y;i<e[x].size();++i){
    		if(v[y=e[x][i]]) continue;
    		tarjan(y);
    		fa[y]=x;
    	}
    	for(int i=0,y,id;i<q[x].size();++i){
    		y=q[x][i].first,id=q[x][i].second;
    		if(v[y]==2) ans[id]=get(y);
    	}
    	v[x]=2;
    }
    void dfs(int x){
    	v[x]=1;
    	for(int i=0,y;i<e[x].size();++i){
    		if(v[y=e[x][i]]) continue;
    		dfs(y);
    		f[x]+=f[y];
    	}
    	if(!f[x]) ANS+=m;
    	else if(f[x]==1) ++ANS;
    }
    int main(){
    	read(n),read(m);
    	for(int i=1,x,y;i<n;++i){
    		read(x),read(y);
    		e[x].push_back(y),e[y].push_back(x);
    	}
    	for(int i=1;i<=n;++i) fa[i]=i;
    	for(int i=1,x,y;i<=m;++i){
    		read(x),read(y);
    		if(x==y) ans[i]=x;
    		else{
    			q[x].push_back(pii(y,i));
    			q[y].push_back(pii(x,i));
    		}
    		++f[x],++f[y];
    	}
    	tarjan(1);
    	for(int i=1;i<=m;++i) f[ans[i]]-=2;
    	memset(v,0,sizeof v);
    	dfs(1);
    	printf("%d
    ",ANS-m); // fa[1]
    	return 0;
    }
    
  • 相关阅读:
    vue+element ui 表格自定义样式溢出隐藏
    vue自定义指令directives使用及生命周期
    前端如何下载文件
    js实现活动倒计时
    echarts自定义提示框数据
    vue项目如何刷新当前页面
    数据库——关于索引
    Javascript节点选择
    asp.net 身份验证(Update)
    ASP.config配置
  • 原文地址:https://www.cnblogs.com/autoint/p/10926656.html
Copyright © 2020-2023  润新知