Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
可以小小的改动一下In Reply To:我的c代码 (不带注释) Posted by:fxzy at 2006-03-04 20:15:45 我参考了你的代码,很有帮助。就是少了些解说,本质上使用并差集做的。 其实关系矩阵用一个就够了,为什弄了4个呢?? > #include <stdio.h> > #define maxn 50004 > > int parent[maxn]; > int relation[maxn]; > //int result[][3]={1 , -1 , 0, 0 , 1 , -1 , -1 , 0 , 1}; > //int result2[][3]={0 , 1 , -1 , -1 , 0 , 1 , 1 , -1 , 0}; > //int next[][3]={1 , -1 , 0 , -1 , 0 , 1 , 0 , 1 , -1}; > //int behind[][3]={0 , -1 , 1 , 1 , 0 , -1 , -1 , 1 , 0}; > int r3[3][3]={1, -1, 0, -1, 0, 1, 0, 1, -1}; > void find(int x,int &y,int &d) > { > int z,r,now; > d=0;z=x; > while (parent[x] > 0) > { //d = next[d+1][relation[x]+1]; d=r3[d+1][relation[x]+1]; > x = parent[x]; > } > y = x; now = d; > > while (z !=y ) > { > x = parent[z]; > parent[z] = y; > r = relation[z]; > relation[z] = now; > //now = behind[now+1][r+1]; now = r3[-r+1][now+1]; > z = x; > } > } > > void merge(int a,int b,int r) > { > if(parent[a]>parent[b]) > { > parent[b] = parent[a] + parent[b]; > parent[a] = b; > relation[a] = r; > } > else { > parent[a] = parent[a] + parent[b]; > parent[b] = a; > relation[b] = -r; > } > > } > > int main() > { > int n , k , lie , i ; > int x , y , a , b ; > int u , v , d , nr ; > scanf("%d%d",&n,&k); > > for(i=1;i<=n;i++) > { > parent[i] = -1; > relation[i] = 0; > } > > lie=0; > for(i=1;i<=k;i++) > { > scanf("%d%d%d",&d,&x,&y); > if(x>n||y>n) > { > lie+=1; > continue; > } > find(x , a , u); > find(y , b , v); > //if(d==2) nr = result[u+1][v+1]; > //else nr = result2[u+1][v+1]; nr=r3[r3[-u+1][d]+1][v+1]; > if(a==b) > { > if(nr!=0) > lie+=1; > } > > else merge(a , b , nr); > > } > > printf("%d\n",lie); > return 0; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator