HDU 4582 DFS spanning tree(DFS+贪心)(2013ACM-ICPC杭州赛区全国邀请赛)
Given a graph G, we process DFS on G starting from vertex 1 and get a DFS spanning tree T, then you should choose some edges of G so that all T-Simple Circles contains at least one edge that you choose.
Please minimize the number of edges you choose.
For each case, the first line contains two integers n and m denoting the number of vertices and edges. The vertexes are numbered from 1 to n.
The following m lines describe the graph. Each line contains two integers xi and yi, denoting an edge between vertex xi and yi(xi ≠ yi).
Note that the first n-1 edges of input construct a DFS spanning tree T which is generated by DFS from vertex 1.
Input ends with n = 0 and m = 0
(1 <= n <= 2000, 1 <= m <= 20000, 1 <= xi, yi <= n)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = ; int dep[MAXN]; struct Edge {
int x, y, id;
void read(int i) {
id = i;
scanf("%d%d", &x, &y);
}
void adjust() {
if(dep[x] > dep[y]) swap(x, y);
}
bool operator < (const Edge &rhs) const {
return dep[x] > dep[rhs.x];
}
} e[MAXE]; int n, m;
int head[MAXN], fa[MAXN];
int to[MAXN * ], next[MAXN * ];
int ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge2(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void bfs() {
memset(dep, -, sizeof(dep));
queue<int> que; que.push();
dep[] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dep[v] == -) {
fa[v] = u;
dep[v] = dep[u] + ;
que.push(v);
}
}
}
} bool vis[MAXN]; bool check(Edge &p) {
int now = p.y;
while(fa[now] != p.x) {
if(vis[now]) break;
now = fa[now];
}
if(!vis[now]) {
vis[now] = true;
return false;
}
else return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
for(int i = ; i <= m; ++i) e[i].read(i);
init();
for(int i = ; i < n; ++i) add_edge2(e[i].x, e[i].y);
bfs();
for(int i = n; i <= m; ++i) e[i].adjust();
sort(e + , e + m + );
memset(vis, , sizeof(vis));
int ans = ;
for(int i = ; i <= m; ++i)
if(e[i].id >= n && !check(e[i])) ++ans;
printf("%d\n", ans);
}
}
HDU 4582 DFS spanning tree(DFS+贪心)(2013ACM-ICPC杭州赛区全国邀请赛)的更多相关文章
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 4408 Minimum Spanning Tree
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- [hdu4582]DFS spanning tree
考虑每一条非树边都连接了祖先和儿子,类似于序列上的问题,从底往上算,当发现如果走到某个环的祖先,且这个环中还没有被选到,那么就将最浅的那条边贪心选择即可具体实现可以使用bitset维护当前子树的询问, ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...
- HDU 4896 Minimal Spanning Tree(矩阵高速功率)
意甲冠军: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一 ...
- HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...
随机推荐
- Vue nodejs商城项目- 前后端数据传递
.利用Mongoose查询MongoDB 通过mongoose插件可以简捷地从mondodb中获取数据,首先安装mongoose: cnpm install mongoose --save 使用m ...
- c# 获取网络流量
public class ip_helper{enum Constants {MAX_INTERFACE_NAME_LEN=256, MAXLEN_PHYSADDR=8,MAXLEN_IFDESCR= ...
- zepto 基础知识(5)
81.width width() 类型:number width(value) 类型:self width(function(index,oldWidth){....}) 类型:self 获取对象集合 ...
- Java常用的正则校验
1.非负整数: (^[1-9]+[0-9]*$)|(^[0]{1}$) 或 (^[1-9]+[0-9]*$)|0 2.非正整数: (^-[1-9]+[0-9]*$)|(^[0]{1}$) 或 (^-[ ...
- linux系统基础之---RPM管理(基于centos7.4)
- 14.2 multiprocessing--多线程
本模块提供了多进程进行共同协同工作的功能.由于Python存在GIL锁,对于多线程来说,这只是部分代码可以使用多CPU的优势,对于想全部使用多CPU的性能,让每一个任务都充分地使用CPU,那么使用多进 ...
- Static关键字,遇到的问题_1
一.问题 父类代码: ...
- 学习photoshop心得
简要的学习了ps的三大功能p图,抠图,作图, p图主要是学了换脸这一效果,用到套索工具,把范冰冰的脸接到郭德纲身上, 首先使用套索工具把脸圈起来 然后移动到 另一个人脸上 再然后混合图层,自动混合 差 ...
- python学习之常用模块
- Redis缓存数据库的安装与配置(2)
1.为php安装redis客户端扩展 wget https://github.com/nicolasff/phpredis/archive/master.zip tar xf phpredis-mas ...