题目链接

题目大意

给你n个点(\(\sum n<=2e5\)),n条边,求有多少条路径

题目思路

要明白任意两点的路径只能是1条或者2条

先topo找环(双向边也是可以找的)

然后把环上的每个点当作一棵树的根,dfs求每棵树的节点

设第一颗树的节点为tree[i]...以此类推

pre[i]=tree[1]+.....tree[i-1]+tree[i]

显然若两点位于不同子树,则路径为2,位于同一子树则路径为1

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-10;
int n;
int head[maxn],cnt,deg[maxn];
bool vis[maxn];
vector<int> vec;
int tree[maxn];
int pre[maxn];
struct edge{
int to,next;
}e[maxn<<1];
void add(int u,int v){
e[++cnt]={v,head[u]};
head[u]=cnt;
deg[v]++;
}
void topo(){
queue<int> que;
for(int i=1;i<=n;i++){
if(deg[i]==1){
que.push(i);
}
}
while(!que.empty()){
int x=que.front();
que.pop();
for(int i=head[x];i;i=e[i].next){
deg[e[i].to]--;
if(deg[e[i].to]==1){
que.push(e[i].to);
}
}
}
}
void dfs(int id,int son,int fa){
tree[id]++;
for(int i=head[son];i;i=e[i].next){
if(e[i].to==fa||vis[e[i].to]) continue;
dfs(id,e[i].to,son);
}
}
void init(){
vec.clear();
cnt=0;
for(int i=1;i<=n;i++){
deg[i]=tree[i]=head[i]=vis[i]=0;
}
}
int main(){
int _;scanf("%d",&_);
while(_--){
scanf("%d",&n);
init();
for(int i=1,u,v;i<=n;i++){
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
topo();
for(int i=1;i<=n;i++){
if(deg[i]>=2){
vis[i]=1;
vec.push_back(i);
}
}
int tot=0;
for(int i=0;i<vec.size();i++){
dfs(++tot,vec[i],vec[i]);
}
for(int i=1;i<=tot;i++){
pre[i]=pre[i-1]+tree[i];
}
ll ans=0;
for(int i=1;i<=tot;i++){
ans+=1ll*tree[i]*(tree[i]-1)/2+1ll*2*(tree[i])*(pre[tot]-pre[i]);
}
printf("%lld\n",ans);
}
return 0;
}

E. Number of Simple Paths 题解(思维)的更多相关文章

  1. Codeforces Round #686 (Div. 3) E. Number of Simple Paths (思维,图,bfs)

    题意:有一个\(n\)个点,\(n\)条边的图,问你长度至少为\(1\)的简单路径有多少条. 题解:根据树的性质,我们知道这颗树一定存在一个环,假如一棵树没有环,那么它的所有长度不小于\(1\)的简单 ...

  2. [JZOJ5280]膜法师题解--思维+前缀和

    [JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于

  3. D - Number of Multisets 题解(思维dp)

    题目链接 题目大意 给你一个数k和n,表示用n个\(1/2^i(i=0,1,2.....)\)组成k有多少种方案数 题目思路 这个dp实属巧妙 设\(dp[i][j]表示i个数构成j\) 这i个数可以 ...

  4. Ural 2003: Simple Magic(数论&思维)

    Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is e ...

  5. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  6. CF293B Distinct Paths题解

    CF293B Distinct Paths 题意 给定一个\(n\times m\)的矩形色板,有kk种不同的颜料,有些格子已经填上了某种颜色,现在需要将其他格子也填上颜色,使得从左上角到右下角的任意 ...

  7. [NOIP10.6模拟赛]1.merchant题解--思维+二分

    题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...

  8. [NOIP10.5模拟赛]3.c题解--思维

    题目链接 这次不咕了 https://www.luogu.org/problemnew/show/AT2389 闲扯 考场20分爆搜走人 \cy 话说这几天T3都很考验思维啊 分析 我们先钦定一只鸡( ...

  9. [NOIP10.4模拟赛]3.z题解--思维

    题目链接: 咕咕 闲扯: 哈哈这道T3考场上又敲了5个namespace,300+行,有了前车之鉴还对拍过,本以为子任务分稳了 结果只有30分哈哈,明明用极限数据对拍过不知怎么回事最后数据又是读不全, ...

随机推荐

  1. vue学习笔记(六) ----- vue组件

    一.模块化与组件化 模块化的定义 模块化在Node中是一个规范,定义一些模块的相关的规则,从代码角度上来说,方便做区别,如果不使用模块化,写在js文件中不利于后期维护和扩展,从代码的层面上就把相关的功 ...

  2. python gunicorn详解

    Gunicorn是一个unix上被广泛使用的高性能的Python WSGI UNIX HTTP Server.和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点. gunicorn 安装 ...

  3. Hadoop基础------>MR框架-->WordCount

    认识Mapreduce Mapreduce编程思想 Mapreduce执行流程 java版本WordCount实例 1. 简介: Mapreduce源于Google一遍论文,是谷歌Mapreduce的 ...

  4. 从小白到 6 个 offer,我究竟是怎么刷题的?

    最近自习室里又兴起了一阵刷题潮,大家相约刷题~ 今天和大家系统分享下我去年转行时的一个刷题过程和方法,希望对你有所帮助. 首先介绍下我的编程基础,我学的是金融工程专业,硕士时学过 C++ 的课,这也是 ...

  5. 通过一个很常用的场景来展示vue数据驱动的应用

    需求:可以动态增减组合条件来进行数据查询. 界面运行效果如下图所示: 界面第一次加载时,默认会显示一个空的查询条件,如下图所示: 点击"加"图标,可以无限增加查询条件,也可以点击& ...

  6. 错误解析:org.apache.catalina.LifecycleException: Protocol handler start failed

    以下是报错代码: org.apache.catalina.LifecycleException: Protocol handler start failed at org.apache.catalin ...

  7. sdasd

    create PROCEDURE v4(in c_year int) BEGIN declare num int(10) default 0; declare num1 int(10); select ...

  8. 070_GUI编程

    目录 简介 图形用户界面 Graphical User Interface,简称 GUI,又称图形用户接口 GUI核心技术:Swing AWT 不再流行的原因 为什么学习 AWT AWT(Abstra ...

  9. Spider--补充--Re模块_2

    # @ Author : Collin_PXY # Python 正则表达式的应用(二) # 正则表达式之所以让人头疼,很大程度是因为表达式里有大量的符号及它们的组合,还有很多匹配模式,想要记住比较困 ...

  10. MyBatis动态Sql之if标签的注意事项

    if标签的test属性必填,该属性值是一个符合OGNL要求的判断表达式,一般只用true或false作为结果. 判断条件property != null 或 property == null,适用于任 ...