题目链接

题目大意

给你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. Tensorflow学习笔记No.9

    模型的保存与恢复 介绍一些常见的模型保存与恢复方法,以及如何使用回调函数保存模型. 1.保存完整模型 model.save()方法可以保存完整的模型,包括模型的架构.模型的权重以及优化器. model ...

  2. pxe装机部署

    批量全自动安装操作系统 dhcp:自动分配IP tftp:微系统安装系统 httpd:网络源 检查环境(永久性关闭selinux) setenforce 0 sed -i s/=enforcing/= ...

  3. 关于DevOps的七大误解,99%的人都曾中过招!

    [摘要] DevOps方法可以为组织带来显著的积极影响,降低成本.提高效率,使开发团队的工作更加精简.为了掌握这个过程的优势,有必要认识到DevOps是什么.不是什么.在本文中,就将讨论一些流传甚广的 ...

  4. php 导出excel 10万数据

    php导出excel 10万数据(此代码主要测试用) 在工作当中要对一些基本信息和其他信息导出 起初信息比较小无所谓.... 但当信息超出65535的时候 发现点问题了 超出了 而且 反应速度很慢 实 ...

  5. -bash: bash_profile: command not found问题

    这个问题一般就是.bash_profile 文件内容错误,里面内容没加注释之内的,vi .bash_profile打开文件检查一下,然后:wq!保存退出 我的错误就是红圈处没有注释造成的

  6. VBA_50段代码总结

    ''                                                          30个有用的VBA代码 '目录: '1--合理使用数组:'2--一次保存并关闭所 ...

  7. [MIT6.006] 16. Dijkstra

    先回顾下上节课的内容: 下面来看一个定理:对于所有的点来说,放松操作总是满足 d[v] ≥ δ(s, v).即点s到点v的最短路径总是小于或等于当前点d的路径权重.证明如下: 在正是进入复杂的图前,先 ...

  8. http代理阅读3 发送mem处理

    每次客户端有可读数据触发时,优先检测是否还有数据没有发送,如果有则发送数据,然后在读取client数据 //向后端发送请求的调用过程 //ngx_http_upstream_send_request_ ...

  9. 全文思维导图------redis设计与实现

  10. charles技能之修改请求参数/返回数据(map Local、Rewrite、Breakpoints)

    之前一直用postman调接口比较多,但有时候想要去修改APP的页面展示,造数据又会比较麻烦,此时可以用以下三种方法修改请求参数或修改响应: map Local(本地映射).Breakpoints(打 ...