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): 2885    Accepted Submission(s):
726

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
 
第一次直接用数组处理字符串  超时了   这是超时代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define MAX 100100
#define INF 0x7fffff
using namespace std;
int n,m;
int head[MAX],ans;
int dfn[MAX],low[MAX];
int dfsclock;
char str1[50],str2[50];
char p[MAX][20];
int num,mark;
char ant[MAX][20],ano[MAX][20];
struct node
{
int beg,end,next;
}edge[MAX];
void init()
{
ans=num=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
node E={u,v,head[u]};
edge[ans]=E;
head[u]=ans++;
}
void getmap()
{
int i,j,a,b;
int sum=1;
for(i=1;i<=m;i++)
{
scanf("%s%s",str1,str2);
int x,y;
x=y=INF;
a=b=0;
for(j=1;j<sum;j++)
{
if(strcmp(str1,p[j])==0)
{
x=j;
a=j;
}
if(strcmp(str2,p[j])==0)
{
y=j;
b=j;
}
if(x!=INF&&y!=INF)
break;
}
if(x==INF)
{
x=sum++;
a=x;
strcpy(p[x],str1);
}
if(y==INF)
{
y=sum++;
b=y;
strcpy(p[y],str2);
}
add(a,b);
add(b,a);
}
}
void tarjan(int u,int fa)
{
int i,v;
low[u]=dfn[u]=++dfsclock;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(v==fa)
continue;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
{
strcpy(ant[num],p[edge[i].beg]);
strcpy(ano[num++],p[edge[i].end]);
}
}
else
low[u]=min(low[u],dfn[v]);
}
}
void find()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
dfsclock=0;
tarjan(1,-1);
mark=0;
for(i=1;i<=n;i++)
{
if(!dfn[i])
{
mark=1;
break;
}
}
}
void solve()
{
int i,j;
if(mark)
{
printf("0\n");
return ;
}
printf("%d\n",num);
for(i=0;i<num;i++)
{
printf("%s %s\n",ant[i],ano[i]);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
find();
solve();
}
return 0;
}

  后来看别人都是用的map处理  我不会用map 苦逼啊

#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#define MAX 200010
#define INF 0x7fffff
using namespace std;
int n,m;
int head[MAX],ans;
int dfn[MAX],low[MAX];
int dfsclock;
char str1[50],str2[50];
map<string, int>q1;
map<int, string>q2;
int num,mark,bridge;
struct node
{
int beg,end,cnt,next;
}edge[MAX];
void init()
{
ans=num=bridge=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
node E={u,v,0,head[u]};
edge[ans]=E;
head[u]=ans++;
}
void getmap()
{
int i,j,a,b;
int sum=1;
q1.clear();
q2.clear();
for(i=1;i<=m;i++)
{
scanf("%s%s",str1,str2);
if(q1[str1]==0)//貌似是因为map中都是一对一的情况,即同一个字符串不可能重复出现
{
q1[str1]=sum;
q2[sum]=str1;
sum++;
}
if(q1[str2]==0)
{
q1[str2]=sum;
q2[sum]=str2;
sum++;
}
add(q1[str1],q1[str2]);
add(q1[str2],q1[str1]);
}
}
void tarjan(int u,int fa)
{
int i,v;
low[u]=dfn[u]=++dfsclock;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(v==fa)
continue;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
{
edge[i].cnt=edge[i^1].cnt=1;
bridge++;
}
}
else
low[u]=min(low[u],dfn[v]);
}
}
void find()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
dfsclock=0;
tarjan(1,-1);
mark=1;
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
mark=0;
return ;
}
}
}
void solve()
{
int i,j;
if(mark==0)
printf("0\n");
else
{
printf("%d\n",bridge);
for(i=0;i<ans;i=i+2)
{
if(edge[i].cnt==1)
printf("%s %s\n",q2[edge[i].beg].c_str(),q2[edge[i].end].c_str());
} //我查了map的常用函数,但是没找到这个.c_str .......
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
find();
solve();
}
return 0;
}

  

 

hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&&输出桥&&字符串处理】的更多相关文章

  1. HDU 3849 By Recognizing These Guys, We Find Social Networks Useful(双连通)

    HDU 3849 By Recognizing These Guys, We Find Social Networks Useful pid=3849" target="_blan ...

  2. HDU 3849 By Recognizing These Guys, We Find Social Networks Useful

    By Recognizing These Guys, We Find Social Networks Useful Time Limit: 1000ms Memory Limit: 65536KB T ...

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

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

  5. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  6. hdoj 4738 Caocao's Bridges【双连通分量求桥】

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 3849 (双联通求桥)

    一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,, #include <iostream> #include <cstring> #in ...

  8. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. Automotive Security的一些资料和心得(4):Automotive Safeguards

    通常一辆汽车会包括超过80个ECUs.所有软件代码大小正在快速增加,将会超过1GB.软件protection是必不可少的. 1. 软件保护 1.1. 安全boot Software violating ...

  2. Android手势监听

    public class MainActivity extends Activity { /* * 要实现手指在屏幕上左右滑动的事件需要实例化对象GestureDetector,new * Gestu ...

  3. HAProxy 的负载均衡服务器,Redis 的缓存服务器

    问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...

  4. sjtu1590 强迫症

    Description BS96发布了一套有\(m\)个band柄绘的新badge,kuma先生想要拿到04的badge于是进行了抽抽抽. kuma先生一共抽了\(n\)个badge.他把所有的bad ...

  5. [BZOJ 4033] [HAOI2015] T1 【树形DP】

    题目链接:BZOJ - 4033 题目分析 使用树形DP,用 f[i][j] 表示在以 i 为根的子树,有 j 个黑点的最大权值. 这个权值指的是,这个子树内部的点对间距离的贡献,以及 i 和 Fat ...

  6. dropdownlist无刷新传值

    既然局部刷新,其实没有必要用服务器控件,即便用了服务器控件,也不应该将AutoPostBack="true" ,这将导致页面回发并刷新,因此去掉下拉框的该属性 至于局部改变div的 ...

  7. usb cdc 协议

    什么是CDC类 (Communication Device Class) USB的CDC类是USB通信设备类 (Communication Device Class)的简称.CDC类是USB组织定义的 ...

  8. Ubuntu zookeeper-3.5.0-alpha启动错误 zkEnv.sh: Syntax error: "(" unexpected (expecting "fi")(转)

    昨天小猿我把Ubuntu Server64位上的 zookeeper换成了最新版本的,结果启动的时候出错:之前zookeeper-3.3.6是没有任何问题的,换成了zookeeper3.5出现了下面的 ...

  9. .Net remoting, Webservice,WCF,Socket区别

    传统上,我们把计算机后台程序(Daemon)提供的功能,称为"服务"(service).比如,让一个杀毒软件在后台运行,它会自动监控系统,那么这种自动监控就是一个"服务& ...

  10. Web Service学习笔记(webservice、soap、wsdl、jws详细分析)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...