题目链接:https://www.luogu.org/problemnew/show/P2860

考虑在无向图上缩点。

运用到边双、桥的知识。

缩点后统计度为1的点。

度为1是有一条路径,度为2是有两条路径。

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300000 + 10;
struct edge{
int next, to, from;
}e[maxn<<2];
int head[maxn], cnt;
bool vis[maxn];
int n, m, u[maxn], v[maxn], d[maxn], ans;
stack<int> s;
int dfn[maxn], low[maxn], tim, color[maxn], num;
void add(int u, int v)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++tim;
for(int i = head[x]; i != -1; i = e[i].next)
if(!vis[i])
{
int v = e[i].to;
if(dfn[v]) low[x] = min(low[x], dfn[v]);
else
{
s.push(v);
vis[i] = vis[(i&1)?i+1:i-1] = 1;
tarjan(v);
vis[i] = vis[(i&1)?i+1:i-1] = 0;
low[x] = min(low[x], low[v]);
}
}
if(dfn[x] == low[x])
{
num++;
while(!s.empty())
{
color[s.top()] = num;
if(s.top() == x) {
s.pop();
break;
}
s.pop();
}
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d",&u[i],&v[i]);
add(u[i],v[i]); add(v[i],u[i]);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) num = 0, s.push(i), tarjan(i);
for(int i = 1; i <= m; i++)
{
if(color[u[i]] != color[v[i]])
d[color[u[i]]]++, d[color[v[i]]]++;
}
for(int i = 1; i <= n; i++) if(d[i] == 1) ans++;
printf("%d",(ans+1)/2);
return 0;
}

【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解的更多相关文章

  1. luogu P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...

  2. luogu P2860 [USACO06JAN]冗余路径Redundant Paths |Tarjan

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  3. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  4. 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告

    P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...

  5. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  6. P2860 [USACO06JAN]冗余路径Redundant Paths tarjan

    题目链接 https://www.luogu.org/problemnew/show/P2860 思路 缩点,之后就成了个树一般的东西了 然后(叶子节点+1)/2就是答案,好像贪心的样子,lmc好像讲 ...

  7. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  8. P2860 [USACO06JAN]冗余路径Redundant Paths

    题解: 首先要边双缩点这很显然 然后变成树上问题 发现dp,dfs好像不太对 考虑一下度数 发现只要在度数为1的点之间连边 但我好像不太会证明这个东西.. 网上也没有看到比较正确的证明方法和连边策略. ...

  9. 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths

    P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...

随机推荐

  1. Beam概念学习系列之Pipeline Runners

    不多说,直接上干货! https://beam.apache.org/get-started/beam-overview/ 在 Beam 管道上运行引擎会根据你选择的分布式处理引擎,其中兼容的 API ...

  2. TOJ 2641 Gene

    描述 How can millions of different and complex structures be built using only a few simple building bl ...

  3. Java性能调优-jstack-jstat-jmap

    0. 必须在java进程的用户下执行 a). 先排查自己业务代码,再第三方的开源代码 b). 工具类都在jdk/bin目录下, 实现代码在tools.jar中 1. jstack-线程快照-死锁/阻塞 ...

  4. nyoj 791——Color the fence——————【贪心】

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  5. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

  6. .net编程知识点

    一.编程思想 OOP(面向对象) 面向对象三大特性(多态如何体现)及五项原则 AOP(面向切面编程) 面向切面编程静态植入和动态植入 二.c#23种设计模式 三.Castle是针对.NET平台的一个开 ...

  7. 调试.NET程序OutOfMemoryException (转载)

    原文地址:http://blog.csdn.net/directionofear/article/details/8009427 应用程序调试,需要有个常规的调试思路,应对各类问题最基本的调试手段是什 ...

  8. Jedis Cluster源码分析

    最近一个项目用到Jedis客户端,需要对这个客户端进行改造.看了一下Jedis Cluster源码,做个记录 首先,说核心内容, 在Jedis源码中,关于cluster有个两个重要的map.一个是no ...

  9. python 中函数

    函数   def 函数名(形参):形参不用在前面定义,局部变量   参数      必须参数            必须以正确的顺序传参      关键字参数        加入关键字后可以不需要正确 ...

  10. Python爬虫实战:将网页转换为pdf电子书

    写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天就琢磨着写一个爬虫,将廖雪峰的 ...