POJ 3417 Network (LCA应用,5级)

发布时间:2017-1-22 18:01:20 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"POJ 3417 Network (LCA应用,5级)",主要涉及到POJ 3417 Network (LCA应用,5级)方面的内容,对于POJ 3417 Network (LCA应用,5级)感兴趣的同学可以参考一下。

K - Network Crawling in process...Crawling failedTime Limit:2000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u SubmitStatus Appoint description: sdau_09_wukon (2011-08-12)System Crawler (2013-05-30) 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 思路:求边被环覆盖的次数,0次是桥答案+m 1次则加1 否则不加。计数用个DP,不过公共祖先得减2 不明白:为啥先递归后查询就错了呢,无数次的WA啊!求解? #include<cstdio> #include<cstring> #include<iostream> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int mm=1e5+9; class Edge { public:int v,next; }e[mm*4]; int edge,dp[mm],head[mm],qh[mm],rt[mm],ans; int n,m; bool vis[mm]; void data() { edge=0;clr(head,-1);clr(qh,-1); } void add(int u,int v,int*h) { e[edge].v=v;e[edge].next=h[u];h[u]=edge++; } int find(int x) { if(rt[x]^x) rt[x]=find(rt[x]); return rt[x]; } void dfs(int u) { rt[u]=u; int v;vis[u]=1; for(int i=qh[u];~i;i=e[i].next) { v=e[i].v; if(vis[v]) { dp[find(v)]-=2;//加边公共祖先之上已经不受此边影响了 } } /**************************************** 上下两个for 交换就错了,为啥? *****************************************/ for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!vis[v]) { dfs(v);rt[v]=u; dp[u]+=dp[v];//边被环覆盖次数 if(dp[v]==0)ans+=m; if(dp[v]==1)ans++; } } } void find_bcc() { ans=0; clr(vis,0); dfs(1); } int main() { int a,b; while(~scanf("%d%d",&n,&m)) { data(); FOR(i,2,n) { scanf("%d%d",&a,&b); add(a,b,head);add(b,a,head); } clr(dp,0); FOR(i,1,m) { scanf("%d%d",&a,&b); if(a!=b) add(a,b,qh),add(b,a,qh),++dp[a],++dp[b]; } find_bcc(); printf("%d\n",ans); } }

上一篇:android 上下文菜单(长按)通过配置文件生成