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条 ...
随机推荐
- 实现自己的ArrayList
最近在学习数据结构和算法,书上有个ArrayList的简单实现,写的很不错. package cn.sp.test4; import java.util.Iterator; import java.u ...
- apache单ip多域名多目录配置
自己的vps上放了别人的网站,那怎么样让自己的网站和别人的网站能同时被访问呢?需要使用apache的虚拟主机配置. 配置httpd.conf文件 比如原来是这种只是指向一个目录的配置 Document ...
- 移动游戏By HYJ
暴力求SG函数即可,记忆化贼方便 /*program from Wolfycz*/ #include<cmath> #include<cstdio> #include<c ...
- bzoj1925 [Sdoi2010] 地精部落【DP】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1925 一个多月前“过”了这道题,还自欺欺人地认为懂了这道题,这直接导致了昨晚多校联测2的T3 ...
- DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas
题目传送门 /* DFS:按照长度来DFS,最后排序 */ #include <cstdio> #include <algorithm> #include <cstrin ...
- synchronized(1)用法简介:修饰方法,修饰语句块
注意: 同一个对象或方法在不同线程中才出现同步问题,不同对象在不同线程互相不干扰. synchronized方法有2种用法:修饰方法,修饰语句块 1.synchronized方法 是某个对象实例内,s ...
- java getDocumentBase() 得到的文件夹路径
参考一个百度知道上的回答 举例说来,假设你的项目文件是xx,而这个xx文件夹是在D盘下的yy文件夹里,即项目文件的完整路径D:\yy\xx,则编译运行文件后,在xx文件夹里会产生名为build的文件夹 ...
- UWP Windows10开发更新磁贴和动态更新磁贴
下面将介绍两种方式如何在windows10 uwp开发中如何更新应用磁贴: 实际上windows的磁贴就是用xml实现的,你只需要创建相应格式的xml就可以实现动态磁贴了 一,手动更新磁贴 二,轮询更 ...
- 外文翻译 《How we decide》赛场上的四分卫 第四节
这是第一章的最后一节. 书的导言 本章第一节 本章第二节 本章第三节 制作肥皂剧是非常不易的.整个制作组都要很紧张的工作,每天都要拍摄一些新的事件.新的大转折的剧情需要被想象出来,新的剧本需要被编写, ...
- 了解移动用户的隐私期望:一种基于推荐的Crowdsourcing方法
应学习之需,最近一段时间阅读了一篇论文,特写下总结,若有纰漏,还望指出. 目录 引言 推荐机制 实现 评估 心得 1.1 为什么要了解移动用户的隐私期望 1.移动设备的广泛使用存在一些潜在的隐私威胁和 ...