Swust OJ 003 A Bug(判断图中是否有环)
来源:互联网 发布:常客网络创新 编辑:程序博客网 时间:2024/04/30 05:44
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
/* * 给臭虫m组序号a,b 则a,b两个相恋 默认a,b异性 问是否存在同性恋 比如 ab bc ac 那么 必存在同性恋 因为假设b为公 则a,c为母 而ac也是 相恋的故本题实质上是判断途中是否有环 有环必有同性恋...怎么判断图中有环 这个博客写的挺好的 http://www.cnblogs.com/xwdreamer/archive/2011/06/11/2297008.html * 你问我为什么这个Swust OJ这么多毛病 还要用 母校情怀啊。。。*//* dfs版本加邻接矩阵版本 */#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>#include <stack>#include <cmath>#include <queue>using namespace std;int map[1001][1001];bool vis[1001][1001];int n, m;int cnt = 0;bool ans = false;void dfs(int a,int b) {if (a == b) { cnt++;if (cnt == 2) {ans = 1;return;}}for (int i = 1; i <= n; i++) {if (a != i && !vis[a][i] && !vis[i][a] && map[a][i]==1) {vis[a][i] = vis[i][a] = 1;dfs(i, b);vis[a][i] = vis[i][a] = 0;}}}int main(){int t;cin >> t;for (int i = 1; i <= t; i++) {printf("Scenario #%d:\n",i);cin >> n >> m;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)map[i][j] = 0;for (int j = 0; j < m; j++) {int a, b;cin >> a >> b;map[a][b] = 1;map[b][a] = 1;}bool flag = true;for (int k = 1; k <= n; k++) {cnt = 0;ans = false;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++)vis[i][j] = 0;}dfs(k, k);if (ans) {cout << "Suspicious bugs found!" << endl ;flag = false;break;}}if(flag)cout << "No suspicious bugs found!" << endl;}return 0;}
- Swust OJ 003 A Bug(判断图中是否有环)
- leetcode OJ 判断单链表中是否有环
- oj-15-K-判断字符串中是否有某个字母
- 编译期判断有向图中是否有环
- 判断有向图中是否有环
- 判断有向图中是否有环
- (hdu step 5.1.1)A Bug's Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)
- 判断图是否有环
- 判断图是否有环
- 图- 拓扑序列- 判断图中是否有环
- 判断无向图中是否有环
- 利用拓扑序列判断图中是否有环原理
- 判断图中是否有环的方法
- 判断有向图中是否存在环
- 判断有向图是否有环
- 判断有向图是否有环
- 有向图判断是否有环
- 判断有向图是否有环
- Maven常用命令
- Linux中常用操作命令
- linux扩充/home内存容量
- Logger框架应用
- Android Studio 常用快捷键
- Swust OJ 003 A Bug(判断图中是否有环)
- python 把一个m*n的二维矩阵转换成一个m*n行,三列的列表
- Hadoop学习笔记—15.HBase框架学习(基础知识篇)
- 面试经常遇到setTimeout循环例子
- Android内存泄露分析以及处理
- 发布自己的Angular2库初探
- my_ping 与mysql长链接的问题 设置重连选项
- 《将博客搬至CSDN》
- 数组排序(冒泡、选择、插入、快排、希尔、桶排序、堆排序)