标签:https char class uri ems 不同 ati 介绍 pre
int Find(int x)
{
int r = x;
while(pre[r] != r)
{
r = pre[r];
}
return r;
}
int Find(int x)
{
if(pre[x] == x) return x;
else
{
pre[x] = Find(pre[x]);
return pre[x];
}
}
int Find(int x)
{
int r = x;
while(pre[r] != r)
{
r = pre[r];
int i = x,int j;
while(i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
}
return r;
}
void Join(int x,int y)
{
int fx = Find(x),fy = Find(y);
if(fx != fy) pre[fx] = fy;
}
#include<stdio.h>
int pre[1000];
int Find(int x)
{
int r = x;
while(pre[r] != r)
{
r = pre[r];
int i = x,int j;
while(i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
}
return r;
}
int Join(int x,int y,int roadNum)
{
int fx = Find(x),fy = Find(y);
if(fx != fy)
{
pre[fx] = fy;
return roadNum-1;
}
else return roadNum;
}
void Init(int n)
{
for(int i = 1;i <= n;i++)
{
pre[i] = i;
}
}
int main()
{
int N,M,ret = 0,i,a,b;
while(scanf("%d",&N)&&N)
{
Init(N);
ret = N-1;
scanf("%d",&M);
for(i = 0;i < M;i++)
{
scanf("%d %d",&a,&b);
ret = Join(a,b,ret);
}
printf("%d\n",ret);
}
return 0;
}
Satisfiability of Equality Equations - LeetCode
修改的地方是从int改为char,其他没有什么不同。代码参见: https://www.cnblogs.com/multhree/p/10364652.html
未完待续
标签:https char class uri ems 不同 ati 介绍 pre
原文地址:https://www.cnblogs.com/multhree/p/10364653.html