大意略。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 100010;
int pa[maxn];
void init()
{
for(int i = 0; i < 100001; i++) pa[i] = i;
}
int find(int x)
{
return x == pa[x] ? x : pa[x] = find(pa[x]);
}
int main()
{
int x, y;
while(~scanf("%d", &x))
{
init();
int ans = 0;
while(x != -1)
{
scanf("%d", &y);
x = find(x), y = find(y);
if(x != y) pa[x] = y;
else ans++;
scanf("%d", &x);
}
printf("%d\n", ans);
}
return 0;
}