codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey)
题意:
给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环。问满足条件的路径数
题解:
推论:对于一条边u-->v,我们将其选作为那两条边之一,那么剩下一条边必然与之相邻或者是自环,因为这样才能满足这两条边只走1次。
那么这条边的贡献值为这条边的相邻边数+自环数,假如这条边本身为自环,那么由于剩下的边可以任选(前一个推论),贡献值为m-1
这个AC时间很6啊23333
import java.io.*;
import java.util.*;
public class Main {
static class MyInputStream extends InputStream {
public BufferedInputStream bis = new BufferedInputStream(System.in);
public int read() throws IOException {
int i;
while ((i = bis.read()) < 48)
if (i == -1)
return -1;
int temp = 0;
while (i > 47) {
temp = temp * 10 + i - 48;
i = bis.read();
}
return temp;
}
}
static class node{
public int from,to;
node(){
from=to=0;
}
};
static final int N = (int)1e6+10;
static int fa[]=new int[N];
static node a[]=new node[N];
static int in[]=new int[N];
static int siz[]=new int[N];
private static MyInputStream cin;
static int find(int x){
if(fa[x]==-1) return x;
fa[x]=find(fa[x]);
return fa[x];
}
public static void main(String[] args) throws IOException{
cin = new MyInputStream();
//while (true)
{
int n = cin.read(),m=cin.read();
int u,v,f1,f2,loop=0;
Arrays.fill(fa, -1);
Arrays.fill(in, 0);
Arrays.fill(siz, 0);
for(int i=0;i<m;i++){
u=cin.read();v=cin.read();
if(a[i]==null) a[i]=new node();
a[i].from=u;a[i].to=v;
in[u]++;in[v]++;
if(u==v){
loop++;
continue;
}
siz[u]++;siz[v]++;
f1=find(u);f2=find(v);
if(f1!=f2) fa[f2]=f1;
}
int ca=find(1);
for(int i=1;i<=n;i++){
if(in[i]>0){
ca=find(i);
break;
}
}
boolean flag=false;
for(int i=1;i<=n;i++){
if(ca!=find(i)&&in[i]>0){
flag=true;
break;
}
}
long ans=0;
if(!flag)
for(int i=0;i<m;i++){
u=a[i].from;v=a[i].to;
if(u!=v){
ans+=siz[u]+siz[v]-2+loop;
}else{
ans+=m-1;
}
}
System.out.println(ans/2);
}
}
}
codeforces 407 div1 B题(Weird journey)的更多相关文章
- codeforces 407 div1 A题(Functions again)
codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...
- Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)
D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...
- 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey
考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...
- CodeForces - 789D Weird journey
D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 【cf789D】Weird journey(欧拉路、计数)
cf788B/789D. Weird journey 题意 n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次.边集不同的路径就是不同的. 题解 将所有非自环的边变成 ...
- Codeforces 789D Weird journey - 欧拉路 - 图论
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...
- 【题解】Weird journey Codeforces 788B 欧拉路
传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...
- 【codeforces 789D】Weird journey
[题目链接]:http://codeforces.com/problemset/problem/789/D [题意] 给你n个点,m条边; 可能会有自环 问你有没有经过某两条边各一次,然后剩余m-2条 ...
随机推荐
- A - Supercentral Point CodeForces - 165A
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of point ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- Tree CodeForces -932D
错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...
- Lync客户端证书安装
安装完Lync客户端后,运行时Lync客户端时,报出如下错误: [原因解析] Lync客户端没有正确安装CA证书链. [解决办法] 第一种方法:将计算机加入域. 第二种方法:不加入域的处理方法: 1. ...
- C# KeepAlive的设置
C# KeepAlive的相关设置 网上有很多相关KeepAlive的内容,终于找到了有关C#的这方面资料,设置了下,有行可靠! TcpListener myListener = new TcpLis ...
- SSM学习
一.https://www.cnblogs.com/zyw-205520/p/4771253.html 二.https://blog.csdn.net/dwhdome/article/details/ ...
- 使用Xamarin.Forms跨平台开发入门 Hello,Xamarin.Forms 第一部分 快速入门
本文介绍了如何使用VisualStudio开发Xamarin.Forms 应用程序和使用Xamarin.Forms开发应用的基础知识,包括了构建和发布Xamarin.Forms应用的工具,概念和步骤. ...
- Java集合框架源码(二)——hashSet
注:本人的源码基于JDK1.8.0,JDK的版本可以在命令行模式下通过java -version命令查看. 在前面的博文(Java集合框架源码(一)——hashMap)中我们详细讲了HashMap的原 ...
- html中 accept 属性
1.HTML <input> 标签的 accept 属性 在文件上传中使用 accept 属性,本例中的输入字段可以接受 GIF 和 JPEG 两种图像: <form> < ...
- java 对sql格式化
public class SqlFormat{ public static void main(String[] args){ String sql=""; sqlFormat(s ...