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;
}

最新文章

  1. jQuery-1.9.1源码分析系列(十六)ajax——jsonp原理
  2. [LeetCode] H-Index 求H指数
  3. APP产品交互设计资源汇总(不断更新中...)
  4. $.ajx的用法
  5. Xcode 8 Swift 类似插件方法
  6. python之列表、字典、集合
  7. SQL保留关键字不能用作表名
  8. ASP.NET运行时详解 集成模式和经典模式
  9. PHP JS HTML ASP页面跳转代码 延时跳转代码 返回到上一界面并刷新 JS弹出指定大小的新窗口
  10. Linux之装机指南
  11. [Bootstrap] 1. container &amp; container-fluid
  12. Linux查看磁盘块大小
  13. AppStore IPv6-only审核被拒原因分析及解决方案-b
  14. tomcat解压版安装(摘自网络)
  15. Jsp中out.println()与System.out.println()的区别
  16. | 线段树-地平线horizon
  17. Java使用OkHttps工具类调用外部接口
  18. 借助dubbo-admin来管理你的服务
  19. 2602 ACM 杭电 骨头容器 01背包
  20. 检测鼠标是否在UI上unity

热门文章

  1. OpenGL ES 2.0 Shader 调试新思路(一): 改变提问方式
  2. 关于 xcode5 的no matching provisioning profiles found
  3. Linux - 用户操作
  4. &lt;header&gt;&lt;footer&gt;引用
  5. HDU 1262 寻找素数对 模拟题
  6. 02 uni-app框架学习:设置全局样式统一每个页面的背景颜色
  7. 洛谷 P4609: [FJOI2016] 建筑师
  8. if语句引起的bug
  9. nginx在使用非80端口做反向代理【转】
  10. node.js模块、包