Xtreme9.0 - Communities 强连通
Xtreme9.0 - Communities
题目连接:
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/communities
Description
Social media networks and the amount of information they aggregate is astonishing. With so much information, new patterns and interactions of human society can be identified.
In our social network, the relationships model the flow of information, and they are directed. Alice may subscribe to Bob's newsfeed, while Bob does not subscribe to Alice's. Moreover, the flow of information in our network is such that a person can see the newsfeeds of all people who could reach the person following along a path in the network. Suppose, then, that Alice subscribes to Bob's newsfeed, Bob subscribes to Chuck's newsfeed, and Chuck subscribes to Dave's newsfeed. This would correspond to a simple linear graph:
Alice <- Bob <- Chuck <- Dave
Then Dave would be able to read his own news items only; Chuck would be able to read news items posted by either Dave or himself; Bob would be able to read news items posted by either Chuck, Dave or himself; and Alice would be able to read everyone's news items. Note that everyone can read their own newsfeed.
We are interested in the defining a community metric for our social network. We define a community as a group of people who are able to see all news items posted by any member of the group. As an example, in the figure below, there are two communities, each shown in a different color.
communities.jpg
Note that in the community shown in green above, Jose, Willy, and Elena can all read each other's posts. While Jose, Willy, and Elena can also read Javier's news items. However, Javier cannot read news items from Jose, Willy, or Elena, and is therefore not included in their community.
Your task is to identify the sizes of these communities from biggest to smallest.
Input
The first line of input will contain two space separated integers: the total number of people that devise the social network, n (1 <= n <= 10000) and m, the number of communities for which you should print the size. The following lines will contain a directed relationship between 2 people. If the line reads "Jon Peter", then Peter subscribes to Jon's news feed, and the relation is Jon -> Peter.
The word "END" will appear on a line by itself after the list of relationships.
All of the names are strings containing fewer than 50 characters.
Output
The output consists of m lines, where each line will correspond to the size of a community from biggest to smallest. If there are fewer than m communities, after outputting the size of all existing communities, output lines containing “Does not apply!” for the missing values.
Sample Input
6 2
Jose Willy
Willy Elena
Elena Jose
Diego Javier
Javier Gregorio
Gregorio Diego
Javier Jose
END
Sample Output
3
3
Hint
题意
让你从大到小输出每个连通块的大小
题解
tarjan或者两次dfs都可以
我直接抓了份我幼年时期写的2次dfs的代码
233
代码
#include<bits/stdc++.h>
using namespace std;
map<string,int>H;
const int max_v=10005;
int V=0;
int getid(string s){
if(!H[s])H[s]=++V;
return H[s];
}
vector<int> G[max_v];
vector<int> rG[max_v];
vector<int> vs;
bool used[max_v];
int cmp[max_v];
int sz[max_v];
void add_edge(int from,int to)
{
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v)
{
used[v]=true;
for(int i=0;i<G[v].size();i++)
{
if(!used[G[v][i]])
dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k)
{
used[v]=true;
cmp[v]=k;
sz[k]++;
for(int i=0;i<rG[v].size();i++)
{
if(!used[rG[v][i]])
rdfs(rG[v][i],k);
}
}
int scc()
{
memset(used,0,sizeof(used));
vs.clear();
for(int v=1;v<=V;v++)
{
if(!used[v])
dfs(v);
}
memset(used,0,sizeof(used));
int k=0;
for(int i=vs.size()-1;i>=0;i--)
{
if(!used[vs[i]])
rdfs(vs[i],k++);
}
return k;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
string s1,s2;
while(cin>>s1){
if(s1=="END")break;
cin>>s2;
G[getid(s1)].push_back(getid(s2));
rG[getid(s2)].push_back(getid(s1));
}
int p=scc();
vector<int>Ans;
for(int i=0;i<p;i++)
Ans.push_back(-sz[i]);
sort(Ans.begin(),Ans.end());
for(int i=0;i<min(m,(int)Ans.size());i++)
cout<<-Ans[i]<<endl;
for(int i=Ans.size();i<m;i++)
cout<<"Does not apply!"<<endl;
}
Xtreme9.0 - Communities 强连通的更多相关文章
- Xtreme9.0 - Light Gremlins 容斥
Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...
- IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!
Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...
- Xtreme9.0 - Block Art 线段树
Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...
- Xtreme9.0 - Taco Stand 数学
Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...
- Xtreme9.0 - Pattern 3 KMP
Pattern 3 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...
- Xtreme9.0 - Car Spark 动态规划
Car Spark 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...
- IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings
Dictionary Strings 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictio ...
- Xtreme9.0 - Mr. Pippo's Pizza 数学
Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...
- POJ2186 (强连通分量缩点后出度为0的分量内点个数)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27820 Accepted: 11208 De ...
随机推荐
- HDU 1729 类NIM 求SG
每次有n个盒子,每个盒子有容量上限,每次操作可以放入石头,数量为不超过当前盒子中数量的平方,不能操作者输. 一个盒子算一个子游戏. 对于一个盒子其容量为s,当前石子数为x,那么如果有a满足 $a \t ...
- jquery操作select(取值,设置选中)[转]
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- 树莓派编译安装opencv3 (2019.1.6更新)
一.更新系统 sudo apt-get update sudo apt-get upgrade sudo rpi-update #重启系统 sudo reboot 二.安装依赖库及程序 sudo ap ...
- elasticsearch RTF版本介绍
说明:elastic search官方版本没有集成中文分词以及各种插件,需要手动配置,手动编译jar,对Windows用户很不友好.下载地址:https://github.com/medcl/elas ...
- Flask源码解析:Flask上下文
一.上下文(Context) 什么是上下文: 每一段程序都有很多外部变量.只有像Add这种简单的函数才是没有外部变量的.一旦你的一段程序有了外部变量,这段程序就不完整,不能独立运行.你为了使他们运行, ...
- PGP工作原理及其安全体制
现代信息社会里,当电子邮件广受欢迎的同时,其安全性问题也很突出.实际上,电子邮件的传递过程是邮件在网络上反复复制的过程,其网络传输路径不确定,很容易遭到不明身份者的窃取.篡改.冒用甚至恶意破坏,给收发 ...
- 深入理解java虚拟机-01 走进java
第一章是对java的产生,历史的整体介绍 java的使用很广泛,安装jdk的时候会看到一句广告语runs in 10 billions machines.使用java的设备多达几十亿台 1.概述 优点 ...
- Densenet-Tensorflow
在寻找densnet网络的时候,我发现了一个结构清晰完整的网络代码,在此作备份. https://github.com/taki0112/Densenet-Tensorflow Densenet-Te ...
- activiti helloworld 续
todo... 8.开发流程部署功能 9.开发简单任务待办功能 10.开发简单任务办理功能 11.开发页面activiti流程跟踪图形展现功能 12.集成网页流程设计器
- pyqt5-基础
PyQt5是一套来自Digia的Qt5应用框架和Python的粘合剂.支持Python2.x和Python3.x版本. PyQt5以一套Python模块的形式来实现功能.它包含了超过620个类,600 ...