HDU 4582 DFS spanning tree(DFS+贪心)(2013ACM-ICPC杭州赛区全国邀请赛)

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 v 1, v 2, ..., v k (v 1 = v k) 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 x i and y i, denoting an edge between vertex x i and y i(x i ≠ y i).
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 <= x i, y i <= n)
 
Output
For each case, output the number of minimal edges that you choose.
 
PS:有个大于号写反了居然过了样例……样例不要这么坑爹好吗……
 
代码(812MS):
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <cstring>
 6 using namespace std;
 7 
 8 const int MAXN = 2010;
 9 const int MAXE = 20010;
10 
11 int dep[MAXN];
12 
13 struct Edge {
14     int x, y, id;
15     void read(int i) {
16         id = i;
17         scanf("%d%d", &x, &y);
18     }
19     void adjust() {
20         if(dep[x] > dep[y]) swap(x, y);
21     }
22     bool operator < (const Edge &rhs) const {
23         return dep[x] > dep[rhs.x];
24     }
25 } e[MAXE];
26 
27 int n, m;
28 int head[MAXN], fa[MAXN];
29 int to[MAXN * 2], next[MAXN * 2];
30 int ecnt;
31 
32 void init() {
33     memset(head, 0, sizeof(head));
34     ecnt = 1;
35 }
36 
37 void add_edge2(int u, int v) {
38     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
39     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
40 }
41 
42 void bfs() {
43     memset(dep, -1, sizeof(dep));
44     queue<int> que; que.push(1);
45     dep[1] = 0;
46     while(!que.empty()) {
47         int u = que.front(); que.pop();
48         for(int p = head[u]; p; p = next[p]) {
49             int &v = to[p];
50             if(dep[v] == -1) {
51                 fa[v] = u;
52                 dep[v] = dep[u] + 1;
53                 que.push(v);
54             }
55         }
56     }
57 }
58 
59 bool vis[MAXN];
60 
61 bool check(Edge &p) {
62     int now = p.y;
63     while(fa[now] != p.x) {
64         if(vis[now]) break;
65         now = fa[now];
66     }
67     if(!vis[now]) {
68         vis[now] = true;
69         return false;
70     }
71     else return true;
72 }
73 
74 int main() {
75     while(scanf("%d%d", &n, &m) != EOF) {
76         if(n == 0 && m == 0) break;
77         for(int i = 1; i <= m; ++i) e[i].read(i);
78         init();
79         for(int i = 1; i < n; ++i) add_edge2(e[i].x, e[i].y);
80         bfs();
81         for(int i = n; i <= m; ++i) e[i].adjust();
82         sort(e + 1, e + m + 1);
83         memset(vis, 0, sizeof(vis));
84         int ans = 0;
85         for(int i = 1; i <= m; ++i)
86             if(e[i].id >= n && !check(e[i])) ++ans;
87         printf("%d\n", ans);
88     }
89 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/3275823.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值