首页 技术 正文
技术 2022年11月18日
0 收藏 650 点赞 4,682 浏览 781 个字

cf788B/789D. Weird journey

题意

n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次。边集不同的路径就是不同的。

题解

将所有非自环的边变成两份。然后去掉两条边,看有没有欧拉路。

如果两条边都不是自环,那么只当他们相邻时(共享一个点),剩下的图有两个奇数度的点。有欧拉路。所以第i个点作为共享的点,有\(C(cnt_i,2)\)种路径。

如果其中一个是自环,那么其他m-1条边任意选一个都可以。有loop*(m-1)条,不过每个自环算了两次。所以要减去C(loop,2)。

注意判断一下图是不是联通的,不联通答案是0。

代码

const int N=1001000;
int f[N];
int n,m;
VI e[N];
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
ll loop;
ll ans;
bool o[N];
int main() {
ios::sync_with_stdio(false);//!!!TLE
cin>>n>>m;
rep(i,1,n+1)f[i]=i;int u,v;
rep(i,0,m){
cin>>u>>v;
o[u]=o[v]=1;
if(u==v)++loop;
int fu=find(u),fv=find(v);
if(fu!=fv)f[fu]=fv;
if(u!=v){e[u].pb(v);e[v].pb(u);}
}
rep(i,1,n+1)if(o[i]&&find(i)!=find(u))ans=-1;//o[i]:出现过的点。
if(~ans){
ans=loop*(m-1)-(loop-1)*loop/2;
rep(i,1,n+1)ans+=(ll)(SZ(e[i])-1)*SZ(e[i])/2;
cout<<ans<<endl;
}
else cout<<"0";
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845