[CC-LONCYC]Lonely Cycles

题目大意:

\(T(T\le1000)\)组数据。

给定一张简单图(不含重边与自环),图中有\(n(n\le2\times10^5)\)个节点和\(m(\sum n+m\le5\times10^6)\)条边。每个节点最多属于一个简单环。

对于每条边,求出有多少简单路径包含这条边且至多包含一条在简单环上的边。

思路:

缩点后根据是否为环上边讨论,环上边的方案数就是两边结点数之积。去掉这些环就只剩下若干棵树,可以树形DP。

源代码:

#include<stack>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=2e5+1,M=5e6;
struct Edge2 {
int u,v,id;
};
Edge2 edge[M];
struct Edge {
int to,id;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &id) {
e[u].push_back((Edge){v,id});
e[v].push_back((Edge){u,id});
}
bool ins[N],vis[N];
std::stack<int> s;
int dfn[N],low[N],scc[N],size[N],top[N],par[N],sum[N];
int64 ans[M];
void tarjan(const int &x,const int &par) {
s.push(x);
ins[x]=true;
dfn[x]=low[x]=++dfn[0];
for(auto &j:e[x]) {
const int &y=j.to;
if(y==par) continue;
if(!dfn[y]) {
tarjan(y,x);
low[x]=std::min(low[x],low[y]);
} else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x]) {
scc[0]++;
int y;
do {
y=s.top();
s.pop();
ins[y]=false;
scc[y]=scc[0];
} while(y!=x);
}
}
void dfs(const int &x,const int &par) {
::par[x]=par;
size[x]=vis[x]=1;
top[x]=par?top[par]:x;
for(auto &j:e[x]) {
const int &y=j.to;
if(y==par||scc[x]==scc[y]) continue;
dfs(y,x);
size[x]+=size[y];
}
}
void dp1(const int &x) {
for(auto &j:e[x]) {
const int &y=j.to;
if(y==par[x]||top[x]!=top[y]) continue;
dp1(y);
sum[x]+=sum[y];
}
}
void dp2(const int &x) {
for(auto &j:e[x]) {
const int &y=j.to,&id=j.id;
if(y==par[x]||top[x]!=top[y]) continue;
dp2(y);
ans[id]+=(int64)(size[top[x]]-size[y])*size[y];
ans[id]+=(int64)(sum[top[x]]-sum[y])*size[y];
ans[id]+=(int64)sum[y]*(size[top[x]]-size[y]);
}
}
int main() {
for(register int T=getint();T;T--) {
const int n=getint(),m=getint();
for(register int i=0;i<m;i++) {
edge[i].u=getint();
edge[i].v=getint();
edge[i].id=i;
add_edge(edge[i].u,edge[i].v,i);
}
for(register int i=1;i<=n;i++) {
if(!dfn[i]) tarjan(i,0);
}
for(register int i=1;i<=n;i++) {
if(!vis[i]) dfs(i,0);
}
for(register int i=0;i<m;i++) {
const int &u=edge[i].u,&v=edge[i].v;
if(scc[u]!=scc[v]) continue;
ans[i]=(int64)size[top[u]]*size[top[v]];
sum[u]+=size[top[v]];
sum[v]+=size[top[u]];
}
for(register int i=1;i<=n;i++) {
if(i==top[i]) dp1(i);
}
for(register int i=1;i<=n;i++) {
if(i==top[i]) dp2(i);
}
for(register int i=0;i<m;i++) {
printf("%lld\n",ans[i]);
}
//Reset
for(register int i=1;i<=n;i++) {
e[i].clear();
}
std::fill(&sum[1],&sum[n]+1,0);
std::fill(&dfn[0],&dfn[n]+1,0);
std::fill(&ans[0],&ans[m],0);
std::fill(&vis[1],&vis[n]+1,false);
scc[0]=0;
}
return 0;
}

[CC-LONCYC]Lonely Cycles的更多相关文章

  1. Codechef August Challenge 2018 : Lonely Cycles

    传送门 几波树形dp就行了. #include<cstdio> #include<cstring> #include<algorithm> #define MN 5 ...

  2. Atitti.dw cc 2015 绿色版本安装总结

    Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...

  3. 【Hello CC.NET】CC.NET 实现自动化集成

    一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...

  4. 浅谈iptables防SYN Flood攻击和CC攻击

    ------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...

  5. checking for fcc ....no checking for cc .. no

         源码编译,提示缺少gcc cc cl.exe 解决方案:       yum install -y gcc glibc

  6. 编译器 cc、gcc、g++、CC 的区别

    gcc 是GNU Compiler Collection,原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C.C++.Objective-C.Ada.Fort ...

  7. [CC]区域生长算法——点云分割

    基于CC写的插件,利用PCL中算法实现: void qLxPluginPCL::doRegionGrowing() { assert(m_app); if (!m_app) return; const ...

  8. [CC]点云密度计算

    包括两种计算方法:精确计算和近似计算(思考:local density=单位面积的点数 vs  local density =1/单个点所占的面积) 每种方法可以实现三种模式的点云密度计算,CC里面的 ...

  9. 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】

    转自:http://www.cnblogs.com/skynet/archive/2011/03/29/1998970.html 在项目开发时,有时候想知道源码文件中有多少后缀名为.cc..c..h的 ...

随机推荐

  1. Class create, device create, device create file【转】

    来自:http://www.hovercool.com/en/Class_create,_device_create,_device_create_file 开始写Linux设备驱动程序的时候,很多时 ...

  2. jq常用功能操作

    //表示所有选中的商品 var $goods=$(".goods:checked"); var arr=[]; for(i=0;i<$goods.length;i++){ a ...

  3. java 基础(转自索宁)

    一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...

  4. html的结构-厂子型的布局

    上图所示的布局设计是很常见的.这个该怎么做呢? 技术需求:header 要固定住在顶部,不随鼠标滚动而向上移动:左边的div的有一定的宽度,但是要贴浏览器的底部(屏幕顶部):右边的dv要占据右边的全屏 ...

  5. cacti系列(三)之cacti添加对mysql服务器主从的监控

    1.配置主从同步 主服务器: 建立从服务器的复制权限账号 GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.16 ...

  6. Expm 7_1树中的最大独立集问题

    [问题描述] 给定一个无回路的无向图(即树),设计一个动态规划算法,求出该图的最大独立集,并输出该集合中的各个顶点值. package org.xiu68.exp.exp7; import java. ...

  7. GitHub上优秀的Go开源项目

    近一年来,学习和研究Go语言,断断续续的收集了一些比较优秀的开源项目,这些项目都非常不错,可以供我们学习和研究Go用,从中可以学到很多关于Go的使用.技巧以及相关工具和方法.我把他们整理发出来,大家有 ...

  8. vue系列之项目结构

    参考地址:链接 build webpack配置相关 config webpack配置相关 node_modules  npm install 安装的依赖代码库 src  存放项目源码 static 存 ...

  9. SpringMVC(4.1):Controller接口控制器详解(1)

    原文出处: 张开涛 4.1.Controller简介 Controller控制器,是MVC中的部分C,为什么是部分呢?因为此处的控制器主要负责功能处理部分: 1.收集.验证请求参数并绑定到命令对象: ...

  10. Java集合框架入门介绍(一)

    Java工具包(java.util)提供了强大的数据结构,主要有以下几种接口或类 枚举Enumeration 接口 从数据集合中取回一系列连续值的方法 位集合 BitSet 可以单独清楚或设置的位和标 ...