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 ...
随机推荐
- 搭建Hadoop2.6.0+Eclipse开发调试环境
上一篇在win7虚拟机下搭建了hadoop2.6.0伪分布式环境.为了开发调试方便,本文介绍在eclipse下搭建开发环境,连接和提交任务到hadoop集群. 1. 环境 Eclipse版本Luna ...
- [NOI2002]荒岛野人(exgcd,枚举)
题目描述 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,…,M.岛上住着N个野人,一开始依次住在山洞C1,C2,…,CN中,以后每年,第i个野人会沿顺时针向前走Pi ...
- c++cmb
#include<windows.h> #include<bits/stdc++.h> using namespace std; ]; int main() { printf( ...
- BZOJ1569: [JSOI2008]Blue Mary的职员分配(dp 暴力)
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 483 Solved: 189[Submit][Status][Discuss] Descriptio ...
- Spring Boot2.4双数据源的配置
相较于单数据源,双数据源配置有时候在数据分库的时候可能更加有利 但是在参考诸多博客以及书籍(汪云飞的实战书)的时候,发现对于spring boot1.X是完全没问题的,一旦切换到spring boot ...
- Linux下文件字符编码格式检测和转换
目前多数情况下, 我们遇到的非英文字符文件都是使用UTF-8编码的, 这时一般我们查看这些文件的内容都不会有问题. 不过有时, 我们有可能会遇到非UTF-8编码的文件, 比如中文的GBK编码, 或者俄 ...
- python应用:TXT文件的读写
python读写TXT文件不需要导入包 python中常用的读写方式: 文件打开模式 描述 r 以只读模式打开文件,并将文件指针指向文件头:如果文件不存在会报错 w 以只写模式打开文件,并将文件指针指 ...
- R语言学习笔记(十五):获取文件和目录信息
file.info() 参数是表示文件名称的字符串向量,函数会给出每个文件的大小.创建时间.是否为目录等信息. > file.info("z.txt") size isdir ...
- 为什么我要放弃javaScript数据结构与算法(第一章)—— JavaScript简介
数据结构与算法一直是我算比较薄弱的地方,希望通过阅读<javaScript数据结构与算法>可以有所改变,我相信接下来的记录不单单对于我自己有帮助,也可以帮助到一些这方面的小白,接下来让我们 ...
- python2.7练习小例子(十七)
17):题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制. 程序分析: ...