好题都是可以像这道题一样拿来一遍一遍做的,我们可以从中一次次的学到不一样的知识——题记
题目的网址
这一道题我做了4遍,WA了十来发吧,错的多,理解的乱,于是便会出现这样的问题,还好给弄懂了,不亏。
最详细的代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std; //'A'查询、'D'链接
typedef long long ll;
int N,M;
int root[100005]; //每个元素的根结点所在
int team[100005]; //每个点在哪个阵营中?
int fid(int x)
{
if(x==root[x]) return x;
int temp=fid(root[x]);
team[x]=(team[x]+team[root[x]])%2; //每次访问到这个节点的时候就是这个节点更新的时候,我们将它与父亲的关系建立起来,因为我们链接树通过父节点的“虚假”链接
return root[x]=temp;
}
void mix(int x, int y)
{
int u=fid(x); //这里的fid()函数就是会把x、y分别更新到对应正确的目前值
int v=fid(y);
if(u!=v) //说实话可以不用这一层操作,因为他们一定不再同一个阵营(这道题不是那种判断类的)
{
root[u]=v;
team[u]=(team[x]+team[y]+1)%2; //分别代表“x到它原本根结点距离”、“y到根结点距离”、“x、y链接时候造成的差‘1’的情况”
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(team, 0, sizeof(team));
scanf("%d%d",&N,&M);
for(int i=1; i<=N; i++) root[i]=i;
while(M--)
{
char s[3];
int e1,e2;
scanf("%s %d%d",s,&e1,&e2);
if(s[0]=='A')
{
int fx=fid(e1); //此时代表了我们查到的根的同时更新了e1、e2所在位置的阵营归属
int fy=fid(e2);
if(fx!=fy) printf("Not sure yet.\n");
else if(team[e1]==team[e2]) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else
{
mix(e1, e2);
}
}
}
return 0;
}
最初两次写的较洛基的代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
using namespace std;
typedef long long ll;
const int maxN=1e5+7;
int root[maxN], team[maxN];
int N,M;
int fid(int x)
{
if(x==root[x]) return x;
int temp=fid(root[x]);
team[x]=(team[root[x]]+team[x])%2;
return root[x]=temp;
}
void mix(int x, int y)
{
int u=fid(x);
int v=fid(y);
root[u]=v; //将x的根结点指向y的根结点,使得y的根结点为总的根结点
team[u]=(team[x]+team[y]+1)%2; //x的根结点受到与x本身距离、y到y的根结点以及x到y的距离影响
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=0; i<=N; i++) root[i]=i;
memset(team, 0, sizeof(team));
while(M--)
{
char s[3];
int e1,e2;
scanf("%s %d%d",s,&e1,&e2);
if(s[0]=='D')
{
mix(e1, e2);
}
else
{
int u=fid(e1);
int v=fid(e2);
if(u!=v) printf("Not sure yet.\n");
else if(team[e1]==team[e2]) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
}
}
return 0;
}
//Find them, Catch them POJ - 1703
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
using namespace std;
typedef long long ll;
const int maxN=1e5+5;
int root[maxN]; //根结点的位置,是否可测
int team_[maxN]; //'0'、'1'两个队伍,因为要通过查根节点判断他们是否可判所以0~1即可(还方便)
int N,M;
int fid(int x)
{
if(x==root[x]) return x;
int temp=fid(root[x]); //寻找根结点
team_[x]=(team_[root[x]]+team_[x])%2; //与上一个点的异或,更新!——先去找自己的直属父节点
return root[x]=temp;
}
void mix(int x, int y)
{
int u=fid(x);
int v=fid(y);
root[u]=v; //不存在u==v的情况,所以可以直接判断
if(team_[y]==0) team_[u]=1-team_[x]; //y的根结点是x的根结点,所以要衔接上与x的关系(因为x、y链接,所以只差一位)
else team_[u]=team_[x];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=0; i<=N; i++) root[i]=i;
memset(team_, 0, sizeof(team_));
while(M--)
{
char s[3];
int e1,e2;
scanf("%s",s);
scanf("%d%d",&e1,&e2);
if(s[0]=='A')
{
int u=fid(e1);
int v=fid(e2);
if(u!=v) printf("Not sure yet.\n");
else if(team_[e1]==team_[e2]) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else
{
mix(e1, e2);
}
}
}
return 0;
}