[CC-LONCYC]Lonely Cycles
[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的更多相关文章
- Codechef August Challenge 2018 : Lonely Cycles
传送门 几波树形dp就行了. #include<cstdio> #include<cstring> #include<algorithm> #define MN 5 ...
- Atitti.dw cc 2015 绿色版本安装总结
Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...
- 【Hello CC.NET】CC.NET 实现自动化集成
一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...
- 浅谈iptables防SYN Flood攻击和CC攻击
------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...
- checking for fcc ....no checking for cc .. no
源码编译,提示缺少gcc cc cl.exe 解决方案: yum install -y gcc glibc
- 编译器 cc、gcc、g++、CC 的区别
gcc 是GNU Compiler Collection,原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C.C++.Objective-C.Ada.Fort ...
- [CC]区域生长算法——点云分割
基于CC写的插件,利用PCL中算法实现: void qLxPluginPCL::doRegionGrowing() { assert(m_app); if (!m_app) return; const ...
- [CC]点云密度计算
包括两种计算方法:精确计算和近似计算(思考:local density=单位面积的点数 vs local density =1/单个点所占的面积) 每种方法可以实现三种模式的点云密度计算,CC里面的 ...
- 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】
转自:http://www.cnblogs.com/skynet/archive/2011/03/29/1998970.html 在项目开发时,有时候想知道源码文件中有多少后缀名为.cc..c..h的 ...
随机推荐
- java并发编程系列一、多线程
一.什么是线程 一个应用就是一个进程.一个进程由多个线程组成.一个生产车间比作是一个进程.工人比作是线程.当任务比较多的时候,增加工人可以提高效率,同时成本就是支付费用(机器资源,内存)也会增加. p ...
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列 前台传来的字符串:'589,321' SELECT*FROM TAB_A T1 WHERE T1.CODE IN ( SEL ...
- cacti系列(三)之cacti添加对mysql服务器主从的监控
1.配置主从同步 主服务器: 建立从服务器的复制权限账号 GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.16 ...
- centos6.5系统bash损坏之救援模式修复
1.模拟bash被损坏的情况 # mv /bin/bash /tmp [root@localhost ~]# sync [root@localhost ~]# shutdown -r now 2.挂载 ...
- 初识numpy
from numpy import * 导入numpy包 random可以生成随机数组 通过mat函数,将数组转换成矩阵,可以对矩阵进行求逆计算等.其中.I操作实现了矩阵求逆计算操作. 执行矩阵乘 ...
- Linux(CentOS7)安装zip、unzip命令
安装命令: yum install -y unzip zip
- 擅于使用JS的eval方法
样例如下: var appsDetails = {“app1”:"", “app2”:"", “app3”:"", “app4”:" ...
- android录音实现不再担心—一个案例帮你解决你的问题
最近有小伙伴经常android的录音怎么实现,有没有相关的案例.今天给大家推荐一个android中实现录音和播放的小案例. 效果图: 一.实现录音的 Service 关键代码: // 开始录音 pub ...
- bootstrap----屏幕大小切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Chrome浏览器JSON格式化插件
经常我们使用浏览器调试某个接口,返回的结果看起来很乱如下 我们使用JSON-handle来进行格式化 安装 插件下载地址 插件下载后,在浏览器输入:chrome://extensions/ 将下载后的 ...