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)的更多相关文章

  1. codeforces 407 div1 A题(Functions again)

    codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...

  2. 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 ...

  3. 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 ...

  4. 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey

    考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...

  5. CodeForces - 789D Weird journey

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. 【cf789D】Weird journey(欧拉路、计数)

    cf788B/789D. Weird journey 题意 n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次.边集不同的路径就是不同的. 题解 将所有非自环的边变成 ...

  7. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  8. 【题解】Weird journey Codeforces 788B 欧拉路

    传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...

  9. 【codeforces 789D】Weird journey

    [题目链接]:http://codeforces.com/problemset/problem/789/D [题意] 给你n个点,m条边; 可能会有自环 问你有没有经过某两条边各一次,然后剩余m-2条 ...

随机推荐

  1. Java知识点脑图

    做服务器开发有十几年了,其中大部分用到的都是Java服务器开发,从JDK1.4到现在的JDK1.8,从基本的Java Application到 J2EE(JBOSS,Glassfish),OSGI,到 ...

  2. Hdu 3605 Escape (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  3. Hello!六月

    把这里当做记事本应该没人介意吧: 太忙了!六月! ACM: 背包九讲

  4. 数学 SCU 4436 Easy Math

    题目传送门 /* 数学题:当有一个数开根号后是无理数,则No */ #include <cstdio> #include <algorithm> #include <cs ...

  5. [BZOJ2005][NOI2010]能量采集 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 发现与$(0,0)$连线斜率相同的点会被挡住.也就是对于$(a,b)$且$gcd(a ...

  6. 第二章 TCP/IP 基础知识

    第二章 TCP/IP 基础知识   TCP/IP  transmission control protocol and ip internet protocol 是互联网众多通信协议中最为著名的.   ...

  7. js 日期时间大小比较

    <body> 开始时间:<input onfocus="setday(this)" id="startTime" name="sta ...

  8. HTML5——loading

    https://www.cnblogs.com/wangmeijian/p/4449150.html https://www.cnblogs.com/yunser/p/canvas-baidu-loa ...

  9. 迅为IMX6UL工业级商业扩展级核心板兼容同一底板

    商业级IMX6UL核心板: ARM Cortex-A7架构 主频高达528 MHz 核心板512M DDR内存 8G EMMC 存储 运行温度:-20℃ ~ +80℃ CPU集成电源管理 核心板尺寸仅 ...

  10. RackTables在LNMP系统的安装及使用

    RackTables是一款优秀的机房管理系统,可以十分方便的登记机房设备和连接情况,非常适合小型机房的运维.RackTables是PHP开发的免费系统,最新版本为0.20.14,PHP版本要求不低于P ...