hdu 4462 Scaring the Birds || 2012 Asia Hangzhou Regional Contest || bfs
来源:互联网 发布:淘宝上面的人参 编辑:程序博客网 时间:2024/05/21 06:46
hud 4462 请戳
题意:
小疯子和小傻子是好朋友,今天他们玩一个关于朋友的游戏:
从一个有 n 个人的花名册里面求出一个最小的 k 值,任意两个人通过小于k个人介绍认识的。(任意两个人认识的是由最少的人介绍决定的。)
如果 a 和 b 是朋友, b 和 c 是朋友,那么 a 和 c 成为朋友是通过 b 介绍认识的。
好难啊,来帮帮小傻子!!!思路:
bfs就出来了。
其中这里学到了:
(1).map<string, int> mp;
的映射,要不是知道了这个,不知道这道题要多麻烦才能解出来。
(2).ios::sync_with_stdio(0); cin.tie(0);
确实是比scanf(), printf()
快一些,但是唯一的局限就是,输入输出只能使用,cin>>, cout<<
输出换行也只能使用\n
。复杂度:
时间复杂度:O(n^2 * lg(n))
空间复杂度:O(n^2)代码
/* ***********************************************Author :IlovezilianCreated Time :2015/9/2 22:52:10File Name :1007_1.cpp************************************************ */#include <bits/stdc++.h>using namespace std;const int N = 1010, mod = 1e9+7, INF = 1000001;map<string, int> mp;int n, dis[N][N];vector<int> G[N];bool vis[N];queue<int> q;void bfs(int x){ memset(vis, 0, sizeof(vis[0]) * n); while(!q.empty()) q.pop(); vis[x] = 1; dis[x][x] = 0; q.push(x); while(!q.empty()) { int u = q.front(); q.pop(); int sz = G[u].size(); for(int j = 0; j < sz; j ++) if(!vis[G[u][j]]) { int v = G[u][j]; q.push(v); vis[v] = 1; dis[x][v] = dis[x][u] + 1; } }}void solve(){ string s1, s2; while(cin>>n && n) { mp.clear(); for(int i = 0; i < n; i ++) G[i].clear(); for(int i = 0; i < n; i ++) { cin>>s1; mp[s1] = i; } int m; cin>>m; for(int i = 0; i < m; i ++) { cin>>s1>>s2; int x = mp[s1], y = mp[s2]; G[x].push_back(y); G[y].push_back(x); } for(int i = 0; i < n; i ++) for(int j = 0; j < n; j ++) dis[i][j] = INF; dis[0][0] = 0; for(int i = 0; i < n; i ++) bfs(i); int ans = 0; for(int i = 0; i < n; i ++) for(int j = i + 1; j < n; j ++) ans = max(ans, dis[i][j]); if(ans == INF) ans = -1; cout<<ans<<'\n'; //cout<<flush;开始以为要加这个 }}int main(){ ios::sync_with_stdio(0); cin.tie(0); //freopen("","r",stdin); //freopen("","w",stdout); solve(); return 0;}
0 0
- hdu 4462 Scaring the Birds || 2012 Asia Hangzhou Regional Contest || bfs
- hdu4462 Scaring the Birds || 2012 Asia Hangzhou Regional Contest || 简单暴力
- 2012 Asia Hangzhou Regional Contest--hdu4462Scaring the Birds(IDA)
- HDU 4462(Scaring the Birds)
- HDU 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- hdu 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- 2012 Asia Hangzhou/Jinhua Regional Contest
- 训练赛:2012 Asia Hangzhou Regional Contest
- 2012 Asia Hangzhou Regional Contest--hdu4463Outlets(mst)
- HDU Scaring the Birds
- 2013 Asia Hangzhou Regional Contest
- HDU 3681 Prison Break 二分+搜索 The 35th ACM-ICPC Asia Regional Contest (Hangzhou)
- hdu 4462 Scaring the Birds 状态压缩
- [dfs+水] hdu 4462 Scaring the Birds
- hdu 4462 Scaring the Birds (暴力)
- POJ-Domino Effect(最短路)
- Android 加载大量图片的三级缓存处理
- HDOJ-203
- c语言二维数组的定义和初始化
- java基础面试题(四)
- hdu 4462 Scaring the Birds || 2012 Asia Hangzhou Regional Contest || bfs
- [2015/09/03] 快速幂算法 - 大数取余
- poj3667 Hotel
- 程序员找工作需要看的书
- Word术语A4(210×297)上、下3.8cm,左、右3.2cm,页眉、页脚3.0cm如何在LaTeX中设置
- hdu1532 Drainage Ditches(最大流)
- Combination Sum
- HDU2093-考试排名-模拟-水题
- 到放假的时候了,该写点学习总结了