Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29554 | Accepted: 9086 |
Description
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.
Output
For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."
Sample Input
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
代码:
未按秩合并
#include<iostream>
#include<cstdio>
#include<cstring>
#define Maxn 100010
using namespace std;
char s[2];
int fa[Maxn],re[Maxn];
int findset(int x){
if(fa[x]!=x){
int t=fa[x];
fa[x]=findset(fa[x]);
re[x]=(re[x]+re[t])%2;
}
return fa[x];
}
void unionset(int a,int b,int aa,int bb){
fa[bb]=aa;
re[bb]=(re[a]+re[b]+1)%2;
}
int main()
{
int t,n,m,a,b;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++) fa[i]=i,re[i]=0;
while(m--){
scanf("%s%d%d",&s,&a,&b);
int aa=findset(a),bb=findset(b);
if(s[0]=='A'){
if(aa!=bb)
printf("Not sure yet.\n");
else if(re[a]!=re[b]) printf("In different gangs.\n");
else printf("In the same gang.\n");
}
else if(s[0]=='D'&&aa!=bb) unionset(a,b,aa,bb);
}
}
return 0;
}
按秩合并
#include<iostream>
#include<cstdio>
#include<cstring>
#define Maxn 100010
using namespace std;
char s[2];
int fa[Maxn],re[Maxn],rank[Maxn];
int findset(int x){
if(fa[x]!=x){
int t=fa[x];
fa[x]=findset(fa[x]);
re[x]=(re[x]+re[t])%2;
}
return fa[x];
}
void unionset(int a,int b,int aa,int bb){
if(rank[aa]>rank[bb]) swap(aa,bb);
fa[aa]=bb;
re[aa]=(re[a]+re[b]+1)%2;
if(rank[aa]==rank[bb]) rank[bb]++;
}
int main()
{
int t,n,m,a,b;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++) fa[i]=i,re[i]=rank[Maxn]=0;
while(m--){
scanf("%s%d%d",&s,&a,&b);
int aa=findset(a),bb=findset(b);
if(s[0]=='A'){
if(aa!=bb)
printf("Not sure yet.\n");
else if(re[a]!=re[b]) printf("In different gangs.\n");
else printf("In the same gang.\n");
}
else if(s[0]=='D'&&aa!=bb) unionset(a,b,aa,bb);
}
}
return 0;
}
注:又是一道种类并查集,现在越做越觉得这种题目很水了。这题可以按秩合并,但优化效果不佳,可能和后台测试数据有关。做这种题关键是分析关系,尽量构造出偏移向量来。
A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 27231 | Accepted: 8861 |
Description
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.
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.
Sample Input
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
Huge input,scanf is recommended.
代码:
#include<iostream>
#include<cstdio>
#define Maxn 2010
using namespace std;
int fa[Maxn],re[Maxn],rank[Maxn];
int findset(int x){
if(fa[x]!=x){
int t=fa[x];
fa[x]=findset(fa[x]);
re[x]=(re[x]+re[t])%2;
}
return fa[x];
}
void unionset(int a,int b,int aa,int bb){
if(rank[aa]>rank[bb]) swap(aa,bb);
fa[aa]=bb;
re[aa]=(re[a]+re[b]+1)%2;
if(rank[aa]==rank[bb]) rank[bb]++;
}
int main()
{
int t,n,m,a,b,index=0;
bool flag;
cin>>t;
while(t--){
cin>>n>>m;
flag=false;
for(int i=1;i<=n;i++) fa[i]=i,re[i]=rank[Maxn]=0;
while(m--){
scanf("%d%d",&a,&b);
if(flag==true) continue;
int aa=findset(a),bb=findset(b);
if(aa!=bb) unionset(a,b,aa,bb);
else if(re[a]==re[b]) flag=true;
}
printf("Scenario #%d:\n",++index);
if(flag) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n");
}
return 0;
}
题意:异性可以交配,若同性交配则Suspicious bugs found!,否则No suspicious bugs found!;
注:补上这题,是不是发现这两个题是一个模子刻出来的。