Problem Description
Consider a Depth-First-Search(DFS) spanning tree T of a undirected connected graph G, we define a T-Simple Circle as a path v1, v2, ..., vk (v1 = vk) in G that contains at most one edge which not belongs to the DFS spanning tree T.
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.
 
Input
There are at most 100 test cases.
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)
 
Output
For each case, output the number of minimal edges that you choose.
 
PS:有个大于号写反了居然过了样例……样例不要这么坑爹好吗……
 
代码(812MS):
 #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杭州赛区全国邀请赛)的更多相关文章

  1. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. hdu 4408 Minimum Spanning Tree

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  3. HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...

  4. HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

    Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. [hdu4582]DFS spanning tree

    考虑每一条非树边都连接了祖先和儿子,类似于序列上的问题,从底往上算,当发现如果走到某个环的祖先,且这个环中还没有被选到,那么就将最浅的那条边贪心选择即可具体实现可以使用bitset维护当前子树的询问, ...

  6. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  7. [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...

  8. HDU 4896 Minimal Spanning Tree(矩阵高速功率)

    意甲冠军: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一 ...

  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 ...

随机推荐

  1. o'Reill的SVG精髓(第二版)学习笔记——第七章

    第七章:路径 所有描述轮廓的数据都放在<path>元素的d属性中(d是data的缩写).路径数据包括单个字符的命令,比如M表示moveto,L表示lineto.接着是该命令的坐标信息. 7 ...

  2. 上传文件,经过Zuul,中文文件名乱码解决办法

    转载请标明出处: http://blog.csdn.net/forezp/article/details/77170470 本文出自方志朋的博客 问题描述 在项目中又一个上传文件的oss服务,直接调用 ...

  3. mysql if...else 的使用

    select case when tca.id = '3' then 'vw' else epc_code end as epccode,tfp.product_id, tfp.vender, tfp ...

  4. LVS NAT,DR,TUN三种负载原理

    负载均衡简单介绍 用通俗的话来说负载均衡,就是通过不同的调度机制将用户的请求分派到后端不同的服务器.缓解服务器的请求压力,实现负载均衡的方案有多种,下面简单说说了解的几种方式: DNS 负载:利用DN ...

  5. keepalived入门

    简介 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服 ...

  6. 部署node api的二三事

    当接到node开发node api的时候,我就想用docker来部署,众所周知,node的版本更新迭代很快.很多以前需要babel后才能采用的方法正在不断被node 原生的支持.如果随便更换生产服务器 ...

  7. 国内maven库链接地址,链接阿里的库,下载很快!!!

    <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http:/ ...

  8. 中国农产品信息网站scrapy-redis分布式爬取数据

    ---恢复内容开始--- 基于scrapy_redis和mongodb的分布式爬虫 项目需求: 1:自动抓取每一个农产品的详细数据 2:对抓取的数据进行存储 第一步: 创建scrapy项目 创建爬虫文 ...

  9. Failed to read candidate component class错误分析

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...

  10. Linux中的目录功能(Red Hat 7)

    目录的基本功能: /bin:存放普通用户使用的命令 /sbin:存放管理员可以执行的命令 /home:存放普通的家目录 如张三家目录为/home/zhangsan /root:管理员的家目录 /etc ...