028:A Bug’s Life
描述
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.
样例输入
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
样例输出
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
提示
Huge input,scanf is recommended.
来源
TUD Programming Contest 2005, Darmstadt, Germany
查看 提交 统计 提问
我的提交记录
如果你看到了一个交了那么多次傻子,没错,是我,为啥,因为我想交一个可以循环输入的程序,这道题不允许,这道题如果你交一个可以循环输入的WA,欢迎您!!
# 结果 时间
37 Accepted 2020-08-27
36 Accepted 2020-08-27
35 Accepted 2020-08-27
34 Compile Error 2020-08-27
33 Accepted 2020-08-27
32 Wrong Answer 2020-08-27
31 Wrong Answer 2020-08-27
30 Wrong Answer 2020-08-27
29 Wrong Answer 2020-08-27
28 Compile Error 2020-08-27
27 Wrong Answer 2020-08-27
26 Wrong Answer 2020-08-27
25 Wrong Answer 2020-08-27
24 Wrong Answer 2020-08-27
23 Wrong Answer 2020-08-27
22 Wrong Answer 2020-08-27
21 Compile Error 2020-08-27
20 Wrong Answer 2020-08-27
19 Wrong Answer 2020-08-27
18 Wrong Answer 2020-08-27
17 Output Limit Exceeded 2020-08-27
16 Wrong Answer 2020-08-27
15 Wrong Answer 2020-08-27
14 Compile Error 2020-08-27
13 Compile Error 2020-08-27
12 Accepted 2020-08-27
11 Wrong Answer 2020-08-27
10 Wrong Answer 2020-08-27
9 Accepted 2020-08-27
8 Wrong Answer 2020-08-27
7 Wrong Answer 2020-08-27
6 Wrong Answer 2020-08-27
5 Wrong Answer 2020-08-27
4 Wrong Answer 2020-08-27
3 Wrong Answer 2020-08-27
2 Wrong Answer 2020-08-27
1 Wrong Answer 2020-08-27
方法详解
传送门
PS:只讲方法,不讲题,这两个题差不多
方法一:
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll int
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll inf = 10000000000000005;
ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;
string s1, s2, s;
ll tree[2000010], relative[2000010];
ll head(ll x) {
if (tree[tree[x]] == tree[x])return tree[x];
head(tree[x]);
relative[x] = (relative[x] + relative[tree[x]]) & 1;
return tree[x] = tree[tree[x]];
}
void cmp(ll x, ll y) {
ll xx = head(x), yy = head(y);
if (xx != yy) {
tree[xx] = yy;
relative[xx] = (relative[x] + relative[y] + 1) & 1;
}
xx = head(x), yy = head(y);
if (xx == yy && relative[x] == relative[y])flag = 1;
}
int main() {
cin >> k;
ll tt = 0;
while (k--) {
cin >> n >> m;
for (i = flag = 0, t = w = -1; i <= n; i++)tree[i] = i, relative[i] = 0;
for (i = 0; i < m; i++) {
cin >> x >> y;
if (flag == 0)cmp(x, y);
}
cout << "Scenario #" << (tt++) << ":" << endl << (flag ? "Suspicious bugs found!" : "No suspicious bugs found!") << endl << endl;
}
}
方法二:
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll int
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll inf = 10000000000000005;
ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;
string s1, s2, s;
ll tree[2000010], relative[2000010];
ll head(ll x) {
return x == tree[x] ? x : tree[x] = head(tree[x]);
}
int main() {
cin >> k;
ll tt = 0;
while (k--) {
cin >> n >> m;
for (i = flag = 0, t = w = -1; i <= n; i++)tree[i] = i, relative[i] = 0;
for (i = 0; i < m; i++) {
cin >> x >> y;
if (head(x) == head(y) || flag == 1) {
flag = 1; continue;
}
ll a = relative[x], b = relative[y];
if (a) {
a = head(a); y = head(y);
if (a != y)tree[a] = y;
}
if (b) {
b = head(b); x = head(x);
if (b != x)tree[b] = x;
}
relative[x] = y; relative[y] = x;
}
cout << "Scenario #" << (tt++) << ":" << endl << (flag ? "Suspicious bugs found!" : "No suspicious bugs found!") << endl << endl;
}
}