uva 12356 Army Buddies

时间:2022-05-23 15:18:44

简单的并查集应用。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = + ;
int ll[maxn], rr[maxn];
int f1(int x)
{
if (x != ll[x]) ll[x] = f1(ll[x]);
return ll[x];
}
int f2(int x)
{
if (x != rr[x]) rr[x] = f2(rr[x]);
return rr[x];
}
int main()
{
int n, m, i, a, b, j;
while (~scanf("%d%d", &n, &m))
{
if (n == && m == ) break;
for (i = ; i <= n; i++) ll[i] = i, rr[i] = i;
for (i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
       for (j = a; j <= b; j++) ll[j] = a;
ll[a] = a - ;
for (j = a; j <= b; j++) rr[j] = b;
rr[b] = b + ;
int zuo = f1(a);
int you = f2(b);
if (zuo == || zuo == n + ) printf("* ");
else printf("%d ", zuo);
if (you == n + || you == ) printf("*\n");
else printf("%d\n", you);
}
printf("-\n");
}
return ;
}