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条 ...
随机推荐
- JS 数据类型入门与typeof操作符
标准的数据类型划分: 基本类型: number(数字).string(字符串).undefined.boolean(布尔值).null(空对象) //空对象与非空对象,最大的区别就是不能进行属性操作 ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- 洛谷 P3808 【模板】AC自动机(简单版)洛谷 P3796 【模板】AC自动机(加强版)
https://www.cnblogs.com/gtarcoder/p/4820560.html 每个节点的后缀指针fail指针指向: 例如he,she,his,hers的例子(见蓝书P214): 7 ...
- 2018年全国多校算法寒假训练营练习比赛(第五场):A题:逆序数(树状数组or归并排序)
题目描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.比如一个序列为4 5 1 3 2, 那么这个序列 ...
- 题解报告:hdu 1212 Big Number(大数取模+同余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...
- UIAlertController的使用,代替UIAlertView和UIActionSheet
在iOS8以后,UIAlertView就开始被抛弃了. 取而代之是UIAlertController 以前是警示框这样写: UIAlertView *alert = [[UIAlertView all ...
- 224 Basic Calculator 基本计算器
实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式可以包含左括号 ( ,右括号),加号+ ,减号 -,非负整数和空格 . 假定所给的表达式语句总是正确有效的. 例如: "1 + ...
- Stamus Networks的产品SELKS(Suricata IDPS、Elasticsearch 、Logstash 、Kibana 和 Scirius )的下载和安装(带桌面版和不带桌面版)(图文详解)
不多说,直接上干货! SELKS是什么? SELKS 是Stamus Networks的产品,它是基于Debian的自启动运行发行,面向网络安全管理.它基于自己的图形规则管理器提供一套完整的.易于使 ...
- (三)SpringIoc之初了解
IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来 ...
- CF599B Spongebob and Joke
思路: 模拟,注意特判. 实现: #include <iostream> #include <cstdio> using namespace std; ], x[], y[], ...