牛客小白月赛12 I (tarjan求割边)
题目链接:https://ac.nowcoder.com/acm/contest/392/I
题目大意:一个含有n个顶点m条边的图,求经过所有顶点必须要经过的边数。
例:
输入:
5 5
1 2
2 3
3 4
4 5
3 5
输出:
3
解题思路:比赛的时候想的是,如果一个顶点不在环里,那与它相连的边就必定是一定要经过的边,所有可以用拓扑排序把不在环上的顶点进行统计一下,每去一个顶点必定去掉一条边,所以我们可以用总的边数减去不在环上的点的个数,不过这有个问题就是当有n个顶点,n-1条边的时候,产生的结果会是-1,开始没考虑这种情况WA了两发,我们只需要把答案和0取个最大值就好了。
然后就是出的题解用的是tarjan,显然必要的边是割边,我们用总边数减去割边数就可以了。
Tarjan做法:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int n,m,head[maxn],par[maxn],dfn[maxn],low[maxn],tot,cnt,ans;
struct node{
int to,next;
}edge[*maxn];
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void Tarjan(int u){
dfn[u]=low[u]=++cnt;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(!dfn[v]){
par[v]=u;
Tarjan(v);
if(low[v]>dfn[u]) ans++;
low[u]=min(low[u],low[v]);
}
else if(v!=par[u]) low[u]=min(low[u],dfn[v]);
}
}
int main(){
cin>>n>>m;
for(int i=;i<=n;i++)head[i]=-;
for(int i=;i<=m;i++){
int u,v;
cin>>u>>v;
add(u,v);
add(v,u);
}
Tarjan();
cout<<m-ans<<endl;
return ;
}
拓扑做法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<cmath>
#include<list>
#include<deque>
#include<cstdlib>
#include<bitset>
#include<stack>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=;
const int maxn=;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int dir[][]={{,},{-,},{,},{,-}};
int n,m,in[],sum;
vector<int> mp[];
void topsort(){
queue<int> que;
for(int i=;i<=n;i++){
if(in[i]==){
que.push(i); sum++;
}
}
while(que.size()){
int u=que.front();
que.pop();
int size=mp[u].size();
for(int i=;i<size;i++){
int v=mp[u][i];
in[v]--;
if(in[v]==){
que.push(v);
sum++;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
in[u]++; in[v]++;
}
topsort();
int ans=max(m-sum,);
cout<<ans<<endl;
return ;
}
牛客小白月赛12 I (tarjan求割边)的更多相关文章
- 牛客小白月赛12 I 华华和月月逛公园 (tarjian 求桥)
链接:https://ac.nowcoder.com/acm/contest/392/I 来源:牛客网 华华和月月逛公园 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K, ...
- 牛客小白月赛12 F 华华开始学信息学 (分块+树状数组)
链接:https://ac.nowcoder.com/acm/contest/392/F来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536K ...
- 牛客网 牛客小白月赛12 B.华华教月月做数学-A^B mod P-快速幂+快速乘
链接:https://ac.nowcoder.com/acm/contest/392/B来源:牛客网 华华教月月做数学 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...
- 牛客小白月赛6 水题 求n!在m进制下末尾0的个数 数论
链接:https://www.nowcoder.com/acm/contest/135/C来源:牛客网 题目描述 其中,f(1)=1;f(2)=1;Z皇后的方案数:即在Z×Z的棋盘上放置Z个皇后,使其 ...
- 牛客小白月赛12 D 月月给华华出题 (欧拉函数,数论,线筛)
链接:https://ac.nowcoder.com/acm/contest/392/D 来源:牛客网 月月给华华出题 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K, ...
- 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)
链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...
- 牛客小白月赛12 C 华华给月月出题 (积性函数,线性筛)
链接:https://ac.nowcoder.com/acm/contest/392/C 来源:牛客网 华华给月月出题 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K, ...
- 牛客小白月赛12 J 月月查华华的手机 (序列自动机模板题)
链接:https://ac.nowcoder.com/acm/contest/392/J 来源:牛客网 题目描述 月月和华华一起去吃饭了.期间华华有事出去了一会儿,没有带手机.月月出于人类最单纯的好奇 ...
- 牛客小白月赛12 I 华华和月月逛公园 Tarjan算法求隔边
题目链接:https://ac.nowcoder.com/acm/contest/392/I 题意:给你一个连通的无向图,问图的隔边有多少条 输入:N,M分别是点数和边数 之后M行每行两个正整数u,v ...
随机推荐
- python3 九九乘法表打印花式操作(然并卵)
# 九九乘法表# 方法一# for i in range(1, 10):# for j in range(1, i+1):# print('{}x{}={}\t'.format(i, j, i*j), ...
- spring遇到的Error applying BeanValidation relational constraints
spring3.1+hibernate4集成测试时遇到的问题: log4j:WARN No appenders could be found for logger (org.springframewo ...
- python爬虫之requests的基本使用
简介 Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,Requests它会比urllib更加方便,可以节约我们大量的工作. 一 ...
- Window上安装—Docker 笔记
本文转自:http://cnodejs.org/topic/55a24267419f1e8a23a64367 需求 想玩nodeClub 源码跑起来,结果window 上各种报错,各种依赖软件要装的感 ...
- python设计模式第二十三天【状态模式】
1.应用场景 (1)通过改变对象的内部状态从而改变对象的行为,一般表现为状态的顺序执行 2.代码实现 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from ...
- linux通过命令行查看MySQL编码并修改-简洁版方法
云服务器环境:CentOS 7.4 因为服务器配置较低,故使用MySQL5.5 未进行设置前 1.查看字符编码: mysql> show variables like '%character%' ...
- falsk 项目中日志设置
app/__init__.py: 1 import logging from logging.handlers import RotatingFileHandler ''' 开发中使用DEBUG级别, ...
- 魔术方法:__set、__get
__set: 在设置对象里边不能直接设置(或没有)的属性值的时候,自动去被调用 class Track { private $track_name; public function __set($na ...
- ADO.NET工具类(三)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 【建模应用】PCA主成分分析原理详解
原文载于此:http://blog.csdn.net/zhongkelee/article/details/44064401 一.PCA简介 1. 相关背景 上完陈恩红老师的<机器学习与知识发现 ...