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 ...
随机推荐
- 20165227 2017-2018-2《Java程序设计》课程总结
20165227 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1 简要内容: 记忆深刻的老师 我期望的师生关系 对于Java学习的看法 预备作业2 简要内 ...
- ubuntu 安装(install) pwntcha[一个做"验证码识别"的开源程序]
一.安装 1. sudo apt-get install libsdl1.2-dev libsdl1.2debian sudo apt-get install libsdl1.2-dev(比较大,10 ...
- Android 6.0 变更
Android 6.0(API 级别 23)除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 如果您之前发布过 Andr ...
- wpf image 指定Stretch="None" 不拉伸的时候,仍然拉伸的解决办法
I think TI82 is right on this issue. The image become bigger than you expect because its dpi doesn't ...
- VS Code折腾记 - (4) 常用必备插件推荐【前端】
前言 这篇文章只要让你做一些基础的配置,把vscode变得更加顺手: 插件的需求不是一成不变,有些插件我已经移除了..在最新的VSCODE 1.9.1中, 部分以前用插件实现的功能已经集成了,那就没有 ...
- watch案例解析(element-ui el-select 无法选中问题剖析)
fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:子组件中明明有watch value,为什么this.$emit('inpu ...
- .NetCore 下使用多个DbContext
一个项目中使用多个DbContext 或者种数据库的多个DbContext 业务需要 单个DbContext使用不需要给出说明 1.dotnet ef migrations add migration ...
- 《高性能MySQL》学习笔记
第1章 MySQL架构与历史 1.2 并发控制 MySQL在两个层面实现并发控制:服务器层与存储引擎层. 读锁和写锁: 在处理并发读或写时,可以通过实现一个由两种锁组成的系统来解决问题. 这两种锁通常 ...
- hdu 5038 (2014北京网络赛G 排序水题)
题意:有n个数字,带入10000 - (100 - ai) ^ 2公式得到n个数,输出n个数中频率最大的数,如果有并列就按值从小到大都输出输出,如果频率相同的数字是全部的n个数,就输出Bad....题 ...
- 浙江省“一卡通”异地就医,C#调用省一卡通动态库
前言,最近学习调用 浙江省一卡通业务,主要就是调用一个DLL,动态库文件,这个动态库是浙大网新研发的. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分 ...