poj3417
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5665 | Accepted: 1602 |
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
此题可采用树上差分,差分时w[x],w[y]分别加一,而w[lca(x,y)]-=2;
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<cmath> using namespace std; struct my{ int next; int v; }; const int maxn=1000000+10; int adj[maxn],fa,d[maxn],f[maxn][21],t,w[maxn]; queue<int>Q; my bian[maxn*2]; void myinsert(int u,int v){ bian[++fa].v=v; bian[fa].next=adj[u]; adj[u]=fa; } void bfs(){ d[1]=1; Q.push(1); while(!Q.empty()){ int x=Q.front(); Q.pop(); for (int i=adj[x];i;i=bian[i].next){ int v=bian[i].v; if(d[v]) continue; d[v]=d[x]+1; f[v][0]=x; for (int j=1;j<=t;j++) f[v][j]=f[f[v][j-1]][j-1]; Q.push(v); } } } int lca(int x,int y){ if(d[x]>d[y]) swap(x,y); for (int i=t;i>=0;i--) if(d[f[y][i]]>=d[x]) y=f[y][i]; if(x==y) return x; for (int i=t;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i]; return f[x][0]; } void dfs(int x,int fa){ for (int i=adj[x];i;i=bian[i].next){ int v=bian[i].v; if(v==fa) continue; dfs(v,x); w[x]+=w[v]; } } int main(){ int n,m; int u,v; scanf("%d%d",&n,&m); for (int i=1;i<n;i++){ scanf("%d%d",&u,&v); myinsert(u,v); myinsert(v,u); } t=int(log(n)/log(2))+1; bfs(); for (int i=1;i<=m;i++){ scanf("%d%d",&u,&v); w[u]++; w[v]++; w[lca(u,v)]-=2; } int ans=0; dfs(1,1); for (int i=2;i<=n;i++){ if(!w[i]) ans+=m; else if(w[i]==1) ans++; } printf("%d\n",ans); return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)