Gym - 100342I Travel Agency(割顶)
来源:互联网 发布:网络打鱼害死了多少人 编辑:程序博客网 时间:2024/06/15 23:33
题意:给一个无向图,对于每个节点a,统计有多少点对(u,v)之间的路径必须经过a。
思路:首先求一个图的割顶,在这颗dfs时间树中我们可以发现,对于一个结点u,如果他的一颗子树不能连回u以上的结点,那么这一棵子树的结点与除u以外的结点之间的路径必然经过u,那么在dfs的过程中不断更新答案即可。
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<ctime>#define eps 1e-6#define LL long long#define pii (pair<int, int>)//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int maxn = 25000;//const int INF = 0x3f3f3f3f;int n, m;int pre[maxn], ans[maxn], dfs_clock, cnt[maxn];vector<int> G[maxn];//无向图的割顶和桥int dfs(int u, int fa) { //u在dfs树中的父节点为fa int lowu = pre[u] = ++dfs_clock;ans[u] = 0; cnt[u] = 1;int sum = 0;for(int i = 0; i < G[u].size(); i++) {int v = G[u][i];if(!pre[v]) { //没有访问过v int lowv = dfs(v, u);lowu = min(lowu, lowv); //用后代的low函数更新u的low函数 cnt[u] += cnt[v];if(lowv >= pre[u]) {ans[u] += sum * cnt[v];sum += cnt[v];}}else if(pre[v] < pre[u] && v != fa) lowu = min(lowu, pre[v]); //用反向边更新u的low函数 } ans[u] += (n-1-sum) * sum + n - 1;return lowu; } int main() { freopen("travel.in", "r", stdin); freopen("travel.out", "w", stdout);//freopen("input.txt", "r", stdin);cin >> n >> m;for(int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u);}dfs(1, -1);for(int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0;}
0 0
- Gym - 100342I Travel Agency(割顶)
- 割顶(桥)
- poj1144 Network(割顶)
- 割顶
- POJ1144-Network(割顶模板题)
- POJ 1144 Network (割顶)
- 洛谷3388 【模板】割点(割顶)
- 洛谷3388 割点(割顶)模板
- 割点 洛谷P3388 【模板】割点(割顶) 学习板子
- 2015 四川省赛 I Travel(bfs)
- UVALive 5135 Mining Your Own Business(BCC、割顶)
- HDU 4587 —— TWO NODES(枚举+割顶)
- 无向图的割顶(poj1523,1144)
- POJ 1523 SPF (割顶 点双连通分量)
- uva 10765 Doves and Bombs(割顶)
- uoj#67. 新年的毒瘤(割顶)
- GYM 101128 F.Landscaping(最小割-Dinic)
- GYM 101149 D.Behind the Wall(最小割-Dinic)
- 修改cmd的默认编码
- 矩阵快速幂 【模板】
- Hadoop2.6.0集群搭建过程报错及详解
- 回调机制
- UISlider
- Gym - 100342I Travel Agency(割顶)
- 寻找一个主元(出现次数大于总元素的一半)的算法
- 8月 (August)总结规划
- poj 3070 Fibonacci 【矩阵快速幂 求第N个斐波那契数%1000】
- cocos2d 之autorelease\ratain\release的理解
- 幸运的“浪人”
- matlab out-of-memory 3Gswitch
- 计算机网络知识点
- oracle自动备份