zoukankan      html  css  js  c++  java
  • HDU3849-By Recognizing These Guys, We Find Social Networks Useful(无向图的桥)

    By Recognizing These Guys, We Find Social Networks Useful

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
    Total Submission(s): 3840    Accepted Submission(s): 987


    Problem Description
    Social Network is popular these days.The Network helps us know about those guys who we are following intensely and makes us keep up our pace with the trend of modern times.
    But how?
    By what method can we know the infomation we wanna?In some websites,maybe Renren,based on social network,we mostly get the infomation by some relations with those "popular leaders".It seems that they know every lately news and are always online.They are alway publishing breaking news and by our relations with them we are informed of "almost everything".
    (Aha,"almost everything",what an impulsive society!)
    Now,it's time to know what our problem is.We want to know which are the key relations make us related with other ones in the social network.
    Well,what is the so-called key relation?
    It means if the relation is cancelled or does not exist anymore,we will permanently lose the relations with some guys in the social network.Apparently,we don't wanna lose relations with those guys.We must know which are these key relations so that we can maintain these relations better.
    We will give you a relation description map and you should find the key relations in it.
    We all know that the relation bewteen two guys is mutual,because this relation description map doesn't describe the relations in twitter or google+.For example,in the situation of this problem,if I know you,you know me,too.
     
    Input
    The input is a relation description map.
    In the first line,an integer t,represents the number of cases(t <= 5).
    In the second line,an integer n,represents the number of guys(1 <= n <= 10000) and an integer m,represents the number of relations between those guys(0 <= m <= 100000).
    From the second to the (m + 1)the line,in each line,there are two strings A and B(1 <= length[a],length[b] <= 15,assuming that only lowercase letters exist).
    We guanrantee that in the relation description map,no one has relations with himself(herself),and there won't be identical relations(namely,if "aaa bbb" has already exists in one line,in the following lines,there won't be any more "aaa bbb" or "bbb aaa").
    We won't guarantee that all these guys have relations with each other(no matter directly or indirectly),so of course,maybe there are no key relations in the relation description map.
     
    Output
    In the first line,output an integer n,represents the number of key relations in the relation description map.
    From the second line to the (n + 1)th line,output these key relations according to the order and format of the input.
     
    Sample Input
    
    
    1 4 4 saerdna aswmtjdsj aswmtjdsj mabodx mabodx biribiri aswmtjdsj biribiri
     
    Sample Output
    
    
    1 saerdna aswmtjdsj
     
    Source
     
    Recommend
    chenyongfu   |   We have carefully selected several similar problems for you:  3848 3851 3854 3857 3858 

    题解:这是简单的无向图的桥的题,这一题需要注意的是要求如果有桥,则需要按输入的顺序输出;我门客为其打上标记,记录其输入位次,因为为字符串,我用了两个map存储他们;

    代码为:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5 + 10;
    struct Node {
        int x, y, id, id1, id2;
        bool operator < (const Node c) const
        {
            return id<c.id;
        }
    } node[maxn << 1];
    struct Node2 {
        int y, id, id1, id2;
    };
    vector<Node2> ve[maxn];
    map<string, int> ma;
    map<int, string> mb;
    int low[maxn], dfn[maxn], visited[maxn];
    int n, m, dfn_clock;
    int nu;
    
    void init()
    {
        for (int i = 0; i <= n; i++) ve[i].clear();
        ma.clear(); mb.clear();
        memset(dfn, -1, sizeof dfn);
        memset(visited, 0, sizeof visited);
    }
    
    void dfs(int u, int fa)
    {
        Node p;  Node2 q;
        low[u] = dfn[u] = dfn_clock++;
        visited[u] = 1;
        for (int i = 0; i<ve[u].size(); i++)
        {
            q = ve[u][i];
            if (q.y == fa) continue;
            if (!visited[q.y])
            {
                dfs(q.y, u);
                low[u] = min(low[u], low[q.y]);
                if (low[q.y]>dfn[u])
                {
                    p.x = u; p.y = q.y; p.id = q.id;
                    p.id1 = q.id1; p.id2 = q.id2;
                    node[nu++] = p;
                }
            }
            else low[u] = min(low[u], dfn[q.y]);
        }
    }
    
    
    main()
    {
        int t, i, k, id;
        string s1, s2;
        Node2 p;
        cin >> t;
        while (t--)
        {
            cin >> n >> m;
            init(); k = 1; id = 1;
            while (m--)
            {
                cin >> s1 >> s2;
                if (ma[s1] == 0) ma[s1] = k, mb[k] = s1, ++k;
                if (ma[s2] == 0) ma[s2] = k, mb[k] = s2, ++k;
                int x = ma[s1], y = ma[s2];
                p.y = ma[s2]; p.id = id++; p.id1 = 1; p.id2 = 2;
                ve[x].push_back(p);
                p.y = ma[s1]; p.id = id++; p.id1 = 2; p.id2 = 1;
                ve[y].push_back(p);
            }
    
    
            dfn_clock = nu = 0;
            dfs(1, -1);
            int f = 1;
            for (i = 1; i <= n; i++)
            {
                if (dfn[i] == -1) break;
            }
            if (i <= n)
            {
                cout << 0 << endl;
                continue;
            }
            sort(node, node + nu);
            cout << nu << endl;
            for (i = 0; i<nu; i++)
            {
                if (node[i].id1<node[i].id2)
                    cout << mb[node[i].x] << " " << mb[node[i].y] << endl;
                else cout << mb[node[i].y] << " " << mb[node[i].x] << endl;
            }
        }
    }
    
     
    View Code
  • 相关阅读:
    React16+Redux 实战企业级大众点评Web App
    一类图上二选一构造问题
    O(1)判断两点之间是否有边
    ARC112F Die Siedler
    【学习笔记】同余最短路
    CF1034D Intervals of Intervals
    CF1034C Region Separation
    CF650E Clockwork Bomb
    莫队题三道(LOJ6273, CF1476G, CF700D)
    CF1290D Coffee Varieties (hard version)
  • 原文地址:https://www.cnblogs.com/csushl/p/9386525.html
Copyright © 2011-2022 走看看