【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解
题目链接: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】 题解的更多相关文章
- luogu P2860 [USACO06JAN]冗余路径Redundant Paths
		
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...
 - 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. ...
 - LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)
		
传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...
 - 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告
		
P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...
 - 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)
		
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
 - P2860 [USACO06JAN]冗余路径Redundant Paths tarjan
		
题目链接 https://www.luogu.org/problemnew/show/P2860 思路 缩点,之后就成了个树一般的东西了 然后(叶子节点+1)/2就是答案,好像贪心的样子,lmc好像讲 ...
 - 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths
		
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
 - P2860 [USACO06JAN]冗余路径Redundant Paths
		
题解: 首先要边双缩点这很显然 然后变成树上问题 发现dp,dfs好像不太对 考虑一下度数 发现只要在度数为1的点之间连边 但我好像不太会证明这个东西.. 网上也没有看到比较正确的证明方法和连边策略. ...
 - 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths
		
P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...
 
随机推荐
- go语言解析网页利器goquery使用教程(爬虫必备)
			
某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...
 - Task的一些用法总结
			
一.Task和多线程以及异常的捕获示例代码: static void Main(string[] args) { // 产生CancellationToken的类,该类允许使用Cancel方法终止线程 ...
 - Linux虚拟内存系统详解
			
本文章以Linux为例,讲解一下虚拟内存系统的工作原理,windows系统的原理也是大同小异,有兴趣的读者可以自行查阅相关资料. linux内核以及它管理用户内存的机制,下面我们以应用程序gonzo的 ...
 - Python正则表达
			
```# -*- coding:utf-8 -*-import re re - Support for regular expressions (RE).正则表达式是一个特殊的字符序列,它能帮助你方便 ...
 - C# HashTable 使用用法详解
			
C#中如何操作HashTable类呢?本文将给你答案,哈希表(Hashtable)简述在.NET Framework中, 一,Hashtable是System.Collections命名空间提供的一个 ...
 - Web开发HTTP中URI和URL的情感纠葛
			
作者:郭无心链接:https://www.zhihu.com/question/21950864/answer/66779836来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
 - Facebook 爬虫
			
title: Facebook 爬虫 tags: [python3, facebook, scrapy, splash, 爬虫] date: 2018-06-02 09:42:06 categorie ...
 - sort遇到的问题
			
var arr = [2,10,6,9,7,8]; var arr1 = arr.sort(); var arr2 = arr.sort(function(a,b){ if (a>b){ ret ...
 - drupal基础
			
http://www.cnblogs.com/yaoliang11/archive/2009/07/31/1535883.html
 - Java入门到精通——调错篇之Spring2.5利用aspect实现AOP时报错: error at ::0 can't find referenced pointcut  XXX
			
一.问题描述及原因. 利用Aspect注解实现AOP的时候出现了error at ::0 can't find referenced pointcut XXX.一看我以为注解写错了,结果通过查询相关资 ...