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条 ...
随机推荐
- python之定时器Timer
timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={}) in ...
- Code:Blocks 中文乱码问题原因分析和解决方法
下面说说修改的地方. 1.修改源文件保存编码在:settings->Editor->gernal settings 看到右边的Encoding group Box了吗?如下图所示: Use ...
- windows 下使用命令行操作ftp
open 192.168.10.6 (连接到FTP主机) User allan\ftp (用户连接验证,注意这里的用户用到的是FTP服务器端创建的用户名) 123 ...
- DFS和BFS模板
DFS: 该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想 #include<cstdio> #include<cstring> #include<cstdli ...
- 洛谷 P2634 [国家集训队]聪聪可可
点分板子2333 注释都是错过的地方 #include<cstdio> #include<algorithm> using namespace std; typedef lon ...
- 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- JEECMS9.3集成dubbo操作记录
需求描述: 门户及其他应用系统需要查询JEECMS9.3中发布的栏目及数据,而其他系统都是基于dubbo开发的,因此想要将JEECMS9.3中集成dubbo并对外提供内容管理服务. 需求实现: 1.添 ...
- Snort里如何将读取的包记录存到二进制tcpdump文件下(图文详解)
不多说,直接上干货! 如果网络速度很快,或者想使日志更加紧凑以便以后的分析,那么应该使用二进制的日志文件格式.如tcpdump格式或者pcap格式. 这里,我们不需指定本地网络了,因为所以的东西都被 ...
- WindowForm.计算器
设计计算器: 外部变量: 数字键按钮: 运算符按钮事件代码: 清零按钮 等号按钮: 思维导图:
- iOS Programming UISplitViewController
iOS Programming UISplitViewController The iPad, on the other hand, has plenty of screen space to pr ...