【HDU】3849By Recognizing These Guys...(tarjan,先记录时间戳)

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.

题目大意:现在有N个人,然后就是M个关系,人与人之间的关系,让你找出他们中间的桥(删除这一条边后图的联通分量增加),然后找出图中的桥,最后是让按照题目中边给出的顺序输出的(比较麻烦)

思路:就像是上面说的,基本上也就是那个输出桥的地方比较麻烦的,但是也是没关系的,我们可以先跑一边tarjan,然后利用里面记录的时间戳来判断他是不是一个桥,我们通过tarjan的知识可以知道,如果 u-v中low[v]>pre[u]那么这个边就是桥,反过来也是一样的,无向图

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int maxn=10000+10;
int pre[maxn],low[maxn];
int dfs_clock;
vector<int >G[maxn];
int tarjan(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            int lowv=tarjan(v,u);
            lowu=min(lowu,lowv);
        }
        else if(pre[v]<pre[u]&&v!=fa)
            lowu=min(pre[v],lowu);
    }
    return low[u]=lowu;
}
map<string ,int >mp;
struct Node
{
    string s1,s2;
    int u,v;
    bool flag;
    Node(){}
    Node(string s1,string s2,int u,int v):s1(s1),s2(s2),u(u),v(v){flag=false;}
}a[100000+10];
int main()
{
    int t;
    scanf("%d",&t);
    int n,m;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(pre,0,sizeof(pre));
        memset(low,0,sizeof(low));
        dfs_clock=0;
        int id=0;
        mp.clear();
        for(int i=1;i<=n;i++)G[i].clear();
        for(int i=1;i<=m;i++)
        {
            string s1,s2;
            cin>>s1>>s2;
            if(mp[s1]==0) mp[s1]=++id;
            if(mp[s2]==0) mp[s2]=++id;
            int x=mp[s1],y=mp[s2];
            G[x].push_back(y);
            G[y].push_back(x);
            a[i]=Node(s1,s2,x,y);
        }
        tarjan(1,-1);
        bool flag=true;
        for(int i=1;i<=n;i++)if(!pre[i])
        {
            flag=false;
            break;
        }
        if(!flag)
            printf("0\n");
        else
        {
            int ans=0;
            for(int i=1;i<=m;i++)
            {
                int u=a[i].u,v=a[i].v;
                if(low[u]>pre[v]||low[v]>pre[u]) ans++,a[i].flag=true;
            }
            printf("%d\n",ans);
            for(int i=1;i<=m;i++)if(a[i].flag)
                cout<<a[i].s1<<" "<<a[i].s2<<endl;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值