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.
Input
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.
Output
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.
题目:教授假设这种虫子有两种性别,且只有异性的虫子之间才有交流,每个虫子从1开始编号。接下来给出每对虫子之间的交流,问是不是只有异性之间才有交流。
思路:带权并查集板子题(不清楚带权并查集的点这里------>传送门)。同性权值为0,异性权值为1。
1 1 #include <iostream> 2 2 #include <cstdio> 3 3 #include <algorithm> 4 4 #include <queue> 5 5 #include <vector> 6 6 #include <cmath> 7 7 8 8 using namespace std; 9 9 1010 #define ll long long 1111 #define pb push_back 1212 #define fi first 1313 #define se second 1414 1515 const int N = 2000 << 1; 1616 struct node 1717 { 1818 int rt, v; 1919 }fa[N]; 2020 2121 int Find(int x) 2222 { 2323 if(fa[x].rt == x) return x; 2424 else{ 2525 int tmp = fa[x].rt; 2626 fa[x].rt = Find(tmp); 2727 fa[x].v = (fa[x].v + fa[tmp].v) % 2; 2828 return fa[x].rt; 2929 } 3030 } 3131 3232 bool Union(int x, int y) 3333 { 3434 int fax = Find(x); 3535 int fay = Find(y); 3636 if(fax != fay){ 3737 fa[fay].rt = fax; 3838 fa[fay].v = (fa[x].v + 1 - fa[y].v) % 2; 3939 return true; 4040 }else{ 4141 if(0 == (fa[x].v + 1 - fa[y].v) % 2) return true; 4242 else return false; 4343 } 4444 } 4545 4646 void solve() 4747 { 4848 int T; 4949 scanf("%d", &T); 5050 for(int _case = 1; _case <= T; ++_case){ 5151 int n, m; 5252 scanf("%d%d", &n, &m); 5353 for(int i = 0; i <= n; ++i){ 5454 fa[i].rt = i; 5555 fa[i].v = 0; 5656 } 5757 vector<pair<int ,int > > info; 5858 int x, y; 5959 for(int i = 1; i <= m; ++i){ 6060 scanf("%d%d", &x, &y); 6161 info.pb({x, y}); 6262 } 6363 6464 int error = 0; 6565 for(int i = 0; i < m; ++i){ 6666 x = info[i].fi; 6767 y = info[i].se; 6868 if(!Union(x, y)){ 6969 error = 1; 7070 break; 7171 } 7272 } 7373 7474 printf("Scenario #%d:\n", _case); 7575 printf("%s\n\n", error == 1 ? "Suspicious bugs found!" : "No suspicious bugs found!"); 7676 } 7777 } 7878 7979 int main() 8080 { 8181 8282 solve(); 8383 8484 return 0; 8585 }