E. Number of Simple Paths 题解(思维)
题目链接
题目大意
给你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 题解(思维)的更多相关文章
- Codeforces Round #686 (Div. 3) E. Number of Simple Paths (思维,图,bfs)
题意:有一个\(n\)个点,\(n\)条边的图,问你长度至少为\(1\)的简单路径有多少条. 题解:根据树的性质,我们知道这颗树一定存在一个环,假如一棵树没有环,那么它的所有长度不小于\(1\)的简单 ...
- [JZOJ5280]膜法师题解--思维+前缀和
[JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于
- D - Number of Multisets 题解(思维dp)
题目链接 题目大意 给你一个数k和n,表示用n个\(1/2^i(i=0,1,2.....)\)组成k有多少种方案数 题目思路 这个dp实属巧妙 设\(dp[i][j]表示i个数构成j\) 这i个数可以 ...
- Ural 2003: Simple Magic(数论&思维)
Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is e ...
- 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 ...
- CF293B Distinct Paths题解
CF293B Distinct Paths 题意 给定一个\(n\times m\)的矩形色板,有kk种不同的颜料,有些格子已经填上了某种颜色,现在需要将其他格子也填上颜色,使得从左上角到右下角的任意 ...
- [NOIP10.6模拟赛]1.merchant题解--思维+二分
题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...
- [NOIP10.5模拟赛]3.c题解--思维
题目链接 这次不咕了 https://www.luogu.org/problemnew/show/AT2389 闲扯 考场20分爆搜走人 \cy 话说这几天T3都很考验思维啊 分析 我们先钦定一只鸡( ...
- [NOIP10.4模拟赛]3.z题解--思维
题目链接: 咕咕 闲扯: 哈哈这道T3考场上又敲了5个namespace,300+行,有了前车之鉴还对拍过,本以为子任务分稳了 结果只有30分哈哈,明明用极限数据对拍过不知怎么回事最后数据又是读不全, ...
随机推荐
- 解放双手,不写SQL!一个开源mybatis神器
什么是通用 Mapper? 它是一个可以方便的使用 Mybatis 进行单表的增删改查优秀开源产品.它使用拦截器来实现具体的执行 Sql,完全使用原生的 Mybatis 进行操作.在 Github 上 ...
- python常用模块numpy解析(详解)
numpy模块 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 后打开浏览器输入网址http://local ...
- [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)
题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...
- 记elementUI一个大坑
1. 表格中 用v-if 切换不同表字段时 表头字段顺序经常互换 解决方法:在table-column中加入:key="Math.random()"2. v-if控制的el-t ...
- [C#.NET 拾遗补漏]11:最基础的线程知识
线程的知识太多,知识点有深有浅,往深的研究会涉及操作系统.CUP.内存,往浅了说就是一些语法.没有一定的知识积累,很难把线程的知识写得全面,当然我也没有这个能力.所以想到一个点写一个点,尽量总结一些有 ...
- mysql存储过程加事务
create procedure sp_sw2() begin declare error int default 0; declare continue handler for SQLEXCEPTI ...
- 1 Prism概述
架构目标 以模块化方式开发应用,这些模块被独立团队用WPF技术开发,集成,部署,这是使用Prism的最大好处. 最小化交叉团队依赖.允许团队在不同领域专业化,比如UI设计,商业逻辑实现,基础代码开发 ...
- ado.net 连接数据库
一.用SqlConnection连接SQL Server 1..加入命名空间 using System.Data.SqlClient; 2.连接数据库 SqlConnection myConnecti ...
- 2Git分支问题
1,查看所有分支: git branch *号在哪表明当前分支在哪. 2,新建一个分支: git branch featureq(分支名) 转到该分支下: git checkout featureq ...
- k8s event监控利器kube-eventer对接企微告警
背景 监控是保障系统稳定性的重要组成部分,在Kubernetes开源生态中,资源类的监控工具与组件监控百花齐放. cAdvisor:kubelet内置的cAdvisor,监控容器资源,如容器cpu.内 ...