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统计答案.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #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; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞