1003. Erdős Number
2013-09-04 10:53
381 查看
Time Limit: 1sec Memory Limit:256MB Description The Erdős number describes the "collaborative distance" between a person and mathematician Paul Erdős. To be assigned an Erdős number, an author must co-write a research paper with an author with a finite Erdős number. Paul Erdős has an Erdős number of zero. Anybody else's Erdős number is k + 1 where k is the lowest Erdős number of any coauthor. Erdős wrote around 1,500 mathematical articles in his lifetime, mostly co-written. He had 511 direct collaborators; these are the people with Erdős number 1. The people who have collaborated with them (but not with Erdős himself) have an Erdős number of 2, those who have collaborated with people who have an Erdős number of 2 (but not with Erdős or anyone with an Erdős number of 1) have an Erdős number of 3, and so forth. (from wikipedia.org). Input Assume that Paul Erdős is numbered 0, other actors are numbered by positive integers between 1 to 100. The first line is the number of test cases. Each test case start with the number m of actors in a line, then m pairs of authors are followed. Output For each test case, each author's Erdős number is printed out, if it is finite, in a separate line in the form "author number: Erdősnumber" in the order of increasing actor number, and finally a dotted line "---" is printed out in a separate line. Sample Input Copy sample input to clipboard 2 1 0 1 3 0 4 0 2 2 3 Sample Output 1:1 --- 2:1 3:2 4:1 --- |
直接上代码吧
// Problem#: 8592 // Submission#: 2196678 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include<iostream> #include<queue> #include<memory.h> using namespace std; queue<int>qu; int id[105]; bool visit[105]; bool adj[105][105]; int countt; void bfs() { while(!qu.empty()) { int tmp=qu.front(); //visit[tmp]=true;这里不能标记,一直是WA,因为考虑到一个顺序的问题 for(int i=0;i<105;i++) { if(adj[tmp][i]&&!visit[i]) { visit[i]=true;//进入队列就标记为访问过 id[i]=id[tmp]+1; qu.push(i); } } qu.pop(); } } int main() { int casen; cin>>casen; while(casen--) { while(!qu.empty()) qu.pop(); memset(adj,false,sizeof(adj)); memset(visit,false,sizeof(visit)); int m; cin>>m; int temp=m; while(m--) { int a,b; cin>>a>>b; adj[a][b]=true; adj[b][a]=true; } memset(id,-1,sizeof(id)); id[0]=0; visit[0]=1; qu.push(0); bfs(); for (int i = 1; i < 105; ++i) { /* code */ if(id[i]!=-1) cout<<i<<":"<<id[i]<<endl; } cout<<"---"<<endl; } }
相关文章推荐
- 使用row_number()实现分页
- Lua中table.sort() 报错 attempt to compare number with nil和invalid order function for sorting
- [Leetcode]268. Missing Number
- HDU 1003
- HDU 4727 The Number Off of FFF (水题)
- getPhysicalNumberOfCells 与 getLastCellNum的区别
- HDU 3652 B-number (数位DP)
- plsql中declare用法、number用法、字符串拼接用法(||),as关键字用法
- HDU 4727 The Number Off of FFF 解题报告
- Epic - Seed Number
- RandomNumberUtil
- The used SELECT statements have a different number of columns 错误解决
- HDU 1003 Max Sum
- struts2 java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决
- Create Maximum Number
- FZU 1649 Prime number or not (大素数判定)
- BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)
- Codeforces546D:Soldier and Number Game(求质因子个数)
- [LEETCODE] 268. Missing Number
- hdu1394 Minimum Inversion Number