zoukankan      html  css  js  c++  java
  • POJ 3417 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

    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
    题目链接:http://poj.org/problem?id=3417
    解题报告
    树上差分,可以差出一个结点(根节点除外)上方的边被多少条新边取代.
    那么没有被代取的做出贡献为m,能被一条边取代的贡献为1,能被多条边取代的贡献为0.
    dfs统计答案.
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<string>
    #define ll long long
    #define BIG 200011
    #define FOR(i,s,t) for(register int i=s;i<=t;++i)
    using namespace std;
    ll ans;
    int n,m,tot;
    int x,y,z,dfn_num;
    int nxt[BIG],las[BIG],to[BIG],sign[BIG],f[BIG],sz[BIG],dep[BIG],xu[BIG],top[BIG];
    inline int read(){
    	char c=getchar();
    	while(c>'9'||c<'0')c=getchar();
    	int data=0;
    	while(c<='9'&&c>='0')data=(data<<1)+(data<<3)+c-48,c=getchar();
    	return data;
    }
    inline void add(int x,int y){
    	nxt[++tot]=las[x];
    	las[x]=tot;
    	to[tot]=y;
    	return;
    }
    inline void dfs1(int now){
    	++sz[now];
    	for(register int e=las[now];e;e=nxt[e])
    		if(to[e]!=f[now]){
    			dep[to[e]]=dep[now]+1;
    			f[to[e]]=now;
    			dfs1(to[e]);
    			sz[now]+=sz[to[e]];
    		}
    	return;
    }
    inline void dfs2(int now,int chain){
    	xu[now]=++dfn_num,top[now]=chain;
    	register int i=0;
    	for(register int e=las[now];e;e=nxt[e])
    		if(to[e]!=f[now]&&sz[to[e]]>sz[i])i=to[e];
    	if(!i)return;
    	dfs2(i,chain);
    	for(register int e=las[now];e;e=nxt[e])
    		if(to[e]!=f[now]&&to[e]!=i)dfs2(to[e],to[e]);
    	return;
    }
    inline int lca(int x,int y){
    	for(;top[x]!=top[y];dep[top[x]]>dep[top[y]]?x=f[top[x]]:y=f[top[y]]);
    	return dep[x]<dep[y]?x:y;
    }
    inline void dfs3(int now){
    	for(register int e=las[now];e;e=nxt[e])
    		if(to[e]!=f[now]){	
    			dfs3(to[e]);
    			sign[now]+=sign[to[e]];
    		}
    	if(now==1)return;
    	if(sign[now]==1)++ans;
    	if(sign[now]==0)ans+=m;
    	return;
    }
    int main(){
    	n=read(),m=read();
    	FOR(i,1,n-1){
    		x=read(),y=read();
    		add(x,y);add(y,x);
    	}
    	dep[1]=1;dfs1(1);dfs2(1,1);
    	for(register int i=1;i<=m;++i){
    		x=read(),y=read();
    		++sign[x];++sign[y];sign[lca(x,y)]-=2;
    	}
    	dfs3(1);
    	cout<<ans<<endl;
    	return 0;
    }
    
    
    

      

     
  • 相关阅读:
    在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。
    其原因可能是堆被损坏,这说明 100BloodCellSegTest.exe 中或它所加载的任何 DLL 中有 Bug。
    牛顿方法(Newton's Method)
    广义线性模型(Generalized Linear Models)
    逻辑回归(Logistic Regression)
    局部加权回归、欠拟合、过拟合(Locally Weighted Linear Regression、Underfitting、Overfitting)
    java并发编程实战《三》互斥锁(上)
    关于java链接装载的思考
    java并发编程实战《二》java内存模型
    (转)技术人的王者路,看看你在哪个段位?
  • 原文地址:https://www.cnblogs.com/Stump/p/7735134.html
Copyright © 2011-2022 走看看