中国海洋大学第四届朗讯杯高级组 Super Phyllis
来源:互联网 发布:壁虎老师java视频教程 编辑:程序博客网 时间:2024/04/29 21:55
Super Phyllis
Time Limit: 1000MS Memory limit: 65536K
题目描述
Phyllis works for a large, multi-national corporation. She moves from department to department where her job is to uncover and remove any redundancies inherent in the day-to-day activities of the workers. And she is quite good at her job.
During her most recent assignment, she was given the following chart displaying the chain of commandwithin the department:
Whenever anyone needs to send a report to their bosses, they use the above chart, sending one report along each arrow. Phyllis realized almost instantly that there were redundancies here. Specifically, since D sends a report to B and B sends a report to A, there\'s really no need for D to send a report to A directly, since it will be summarized in the report B sends to A. Thus the connection from D to A can be removed. If there had also been a connection from C to B, then the connections from D to B and C to A could have been removed as well.
Phyllis would like your help with this. Given a description of a chart like the one above, she would like a program that identifies all connections that can be removed from the chart.
输入
输出
示例输入
5D BD CD AB AC A6D BD CD AC BB AC A1Danny Tessa0
示例输出
Case 1: 1 D,ACase 2: 3 C,A D,A D,BCase 3: 0
提示
来源
因为数组开小一直疯狂的RE,我还一直以为是queue出了问题,在SDUT的的OJ上丧心病狂的提交了20多遍最后换成vector才过掉。
首先这个题是不存在环的。
对每个点进行bfs。开始的点层数设为1,依层拓展,如果除了第一层之外的某层拓展到第二层的点,也就是说某个点连接着第二层的点,则说明从出发点(第一层的点)到这个第二层的点之间的边可以删掉。对所有点bfs就可以得到所有多余的边。
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <string>#include <map>#define MAXN 205using namespace std;map<string,int> mp1;map<int,string> mp2;bool gl[MAXN][MAXN];int n,cnt;int d[MAXN];string ans[20000];void bfs(int v){ memset(d,0,sizeof(d)); queue<int> que; que.push(v); d[v]=1; while(!que.empty()) { int tmp=que.front(); que.pop(); for(int i=1; i<=n; ++i) if(gl[tmp][i]) { if(!d[i]) { d[i]=d[tmp]+1; que.push(i); } else { if(d[i]==2&&gl[v][i]) { string str=mp2[v]+","+mp2[i]; ans[cnt].clear(); ans[cnt++]=str; gl[v][i]=false; } } } }}int main(){ int m,kase=0; while(cin>>m&&m) { string a,b; memset(gl,0,sizeof(gl)); mp1.clear(); mp2.clear(); n=0; for(int i=0; i<m; ++i) { cin>>a>>b; if(!mp1[a]) { ++n; mp1[a]=n; mp2[n]=a; } if(!mp1[b]) { ++n; mp1[b]=n; mp2[n]=b; } gl[mp1[a]][mp1[b]]=true; } cnt=0; for(int i=1; i<=n; ++i) bfs(i); sort(ans,ans+cnt); cout<<"Case "<<++kase<<": "<<cnt; for(int i=0; i<cnt; ++i) cout<<" "<<ans[i]; cout<<endl; } return 0;}
- 中国海洋大学第四届朗讯杯高级组 Super Phyllis
- 中国海洋大学第四届朗讯杯高级组 Cash Cow
- 中国海洋大学第四届朗讯杯高级组 Playing Fair with Cryptography
- 中国海洋大学第四届朗讯杯高级组 The Urge to Merge
- 中国海洋大学考研
- 中国海洋大学 软件工程 考研交流
- 中国海洋大学各个校区都有什么专业
- 第四部分:高级话题
- 第四章高级查询
- 第四章 高级查询
- 第四章:高级查询
- 第四章 高级特性
- 第四篇 高级指针
- SQL高级 第四章 高级查询
- 新视野大学英语 第四册 翻译
- 《读大学 怎么读》 第四篇
- 大学学习笔记—单片机第四章
- 第四届逻辑推理大赛中南大学初赛
- stm32f103学习心得
- extern "C"的用法解析
- Java事务处理全解析(一)——Java事务处理的基本问题
- 关于js中for in的缺陷浅析
- JavaWeb 服务启动时,在后台启动加载一个线程。
- 中国海洋大学第四届朗讯杯高级组 Super Phyllis
- 给ListBox添加双击事件
- C#操作office进行Excel图表创建,保存本地,word获取
- 推荐有关git的一张图片和2个网站
- BeagleBone Black USB一线通(1)
- 黑马程序员 JAVA提高——反射
- NoSQL--查询存储、修改以及管理
- C link table question
- OpenGL中实现双缓冲技术的方法