[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的 ...
随机推荐
- linux统计某个特定文件名的大小总和【原创】
[hch@EAISRVBJ2 log]$find ./ -name "test_chs_00*"|xargs du -ck|grep total|awk 'BEGIN{sum=0} ...
- eclipse自动添加注释
自动添加注释 快捷键:alt shift jwindows-->preference Java-->Code Style-->Code Templates code-->new ...
- client模式下对应接口加入桥接出错
client模式下,响应的接口wlan0 加入桥接时出现如下错误: root@root:~# brctl addif br-lan wlan0brctl: bridge br-lan: Operati ...
- xpath定位
XML 实例文档 我们将在下面的例子中使用这个 XML 文档. <?xml version="1.0" encoding="ISO-8859-1"?> ...
- Coursera台大机器学习技法课程笔记07-Blending and Bagging
这一节讲如何将得到的feature或hypothesis组合起来用于预测. 1. 林老师给出了几种方法 在选择g时,需要选择一个很强的g来确保Eval最小,但如果每个g都很弱该怎么办呢 这个时候可以选 ...
- C++ code:指针类型(pointer types)
#include <iostream> using namespace std; int main() { float f = 34.5; int *ip = reinterpret_ca ...
- PHP获取数组最后一个元素的键和值
<?php /** * PHP获取数组中最后一个元素下标和值 */ $arr = ['1' => 'name', '3' => 2, 5 => 6, 'name' => ...
- cf343c 二分答案+模拟
/* 怎么判断能否在时间k内完成扫描 贪心:每次取出最靠左边的磁头去扫描最左边的,然后再往右扫描即可 如果当前点无法扫到最左侧点,那么后继点一样无法扫到 */ #include<bits/std ...
- C#--整型与字节数组byte[]之间的转换
using System; int i = 123;byte [] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组lob.Write ...
- 【C++ Primer 第十三章】4. 拷贝控制示例
拷贝控制示例 #include<iostream> #include<string> #include<set> #include<vector> us ...