首页 技术 正文
技术 2022年11月16日
0 收藏 509 点赞 4,894 浏览 1473 个字

题目大意:

例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈。

分析

就是裸的求强连通分量,直接用Tarjan解决

代码

#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <stack>
using namespace std;
int N,M;
string NAME[40];
map<string,int> dict;
stack<int> S;
int tot=0; //这一道题特有的存点..
int cnt=0; //强连通数目
int TIME=0; //时间戳
int DFN[40],Low[40]; //DNF 时间戳,Low ,u及u的子树最小的时间戳
bool INSTACK[40]; //判断是否在栈内
int Belong[40]; //存储属于哪一个强连通分量;
struct Edge{
int to;
Edge *next;
}E[20000],*EE;
struct Node{
Edge *first;
}G[50];
void Link(int a,int b)
{
EE->to=b;EE->next=G[a].first;G[a].first=EE++;
}
void input()
{
EE=E;
tot=0;
TIME=0;
cnt=0;
string a,b;
dict.clear();
memset(G,0,sizeof(G));
memset(DFN,0,sizeof(DFN));
for(int i=1;i<=M;i++)
{
cin>>a>>b;
if(dict[a]==0)
{
dict[a]=++tot;
NAME[tot]=a;
}
if(dict[b]==0)
{
dict[b]=++tot;
NAME[tot]=b;
}
Link(dict[a],dict[b]);
}
}
void Tarjan(int u)
{
DFN[u]=Low[u]=++TIME;
S.push(u);
INSTACK[u]=true;
for(Edge *p=G[u].first;p;p=p->next)
{
if(DFN[p->to]==0)
{
Tarjan(p->to);
Low[u]=min(Low[u],Low[p->to]);
}
else if(INSTACK[p->to]==true)
Low[u]=min(Low[u],DFN[p->to]);
}
int k;
if(DFN[u]==Low[u])
{
int ok=0;
cnt++;
do
{
k=S.top();
S.pop();
INSTACK[k]=false;
Belong[k]=cnt;
if(ok==0)
{
ok=1;
cout<<NAME[k];
}
else cout<<", "<<NAME[k];
}while(k!=u);
cout<<endl;
}
}
void solve()
{
for(int i=1;i<=N;i++)
{
if(DFN[i]==0)
Tarjan(i);
}
}
int main()
{
int CASE=0;
// freopen("a.in","r",stdin);
while(cin>>N>>M&&(N||M))
{
printf("Calling circles for data set %d:\n",++CASE);
input();
solve();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850