zoukankan      html  css  js  c++  java
  • 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 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):
     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
    ", ans);
    88     }
    89 }
    View Code
  • 相关阅读:
    写给刚接触Active Directory的朋友(转)
    vim中缩进的设置以及将tab转换成空格
    Ubuntu12.04 更新源
    实验三电子公文传输系统1个人贡献
    20191325实验四 Web服务器1socket编程
    20191325学习笔记10
    OpenEuler 中C与汇编的混合编程
    20191325第十三章学习笔记
    20191323第十四章学习笔记
    http 三次握手 四次挥手,面试题
  • 原文地址:https://www.cnblogs.com/oyking/p/3275823.html
Copyright © 2011-2022 走看看