acm食物链

来源:百度知道 编辑:UC知道 时间:2024/06/07 05:50:46
这是我做的acm食物链题目。
我在自己的机上运行没有错误。
可是提交上去却总是说我答案错误。
一下是源代码。
希望有高手能帮我解答

#include<iostream>
#include<vector>
using namespace std;

struct node
{
int parent;
int rank;
};

void compress(int x,node * p)
{
if(p[p[x].parent].parent<0) return;
compress(p[x].parent,p);
p[x].rank=(p[x].rank+p[p[x].parent].rank)%3;
p[x].parent=p[p[x].parent].parent;
return;
}

int root(int x,node * p)
{
if(p[x].parent<0) return x;
else {compress(x,p);return p[x].parent;}
}

void unite(int x,int y,node * p,int rank)
{
if(p[x].parent<p[y].parent) {p[x].parent+=p[y].parent;p[y].parent=x;p[y].rank=rank;}
else {p[y].parent+=p[x].parent;p[x].parent=y;p[x].rank=(3-rank)%3;}
}

int main()
{
int n,k,total=0,d,x,y,xroot,yroot,rank;
node * p;
cin>>n>>k;
p=new n

#include <iostream>
#include <cstring>
using namespace std;

int a[50001], s[150001];
int S=-3;

int b()
{
return(S+=3);
}

int f(int i)
{
if(s[i]+1)return s[i]=f(s[i]);
return i;
}

int main()
{
int n, k, c(0);
memset(a, -1, sizeof(a));
memset(s, -1, sizeof(s));
cin>>n>>k;
while(k--)
{
int r, x, y;
scanf("%d%d%d", &r, &x, &y);
if(r<1||r>2||x<1||x>n||y<1||y>n)
{
++c;
continue;
}
a[x]=a[x]==-1?b():f(a[x]);
a[y]=a[y]==-1?b():f(a[y]);
if(a[x]/3-a[y]/3)
{
int xx=a[x]/3*3, yy=a[y]/3*3, dd=(a[x]%3-a[y]%3+r+2)%3;
if(rand()&1)
{
swap(xx, yy);
dd=(3-dd)%3;
}
for(int i=0; i!=3; ++i)
s[yy+i]=xx+(dd+i)%3;
a[x]=f(a[x]), a[y]=f(a[y]);
}
if((a[x]+r+2)%3-a[y]%3)
++c;
}
cout<<c<<endl;
}