forked from IElgohary/Uva-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForest.cpp
executable file
·103 lines (82 loc) · 1.66 KB
/
Forest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int ppl[100];
int opn;
int rnk[100];
bool op[100][100];
int findset(int a)
{
if ( ppl[a] == a)
return a;
return ppl[a] = findset(ppl[a]);
}
void unionF(int a , int b)
{
int x = findset(a);
int y = findset(b);
if ( y != x )
{
opn--;
if ( rnk[x] > rnk[y])
{
ppl[y] = x;
}
else if ( rnk[y] > rnk[x])
{
ppl[x] = y;
}
else
{
ppl[y] = x;
rnk[x] ++;
}
}
}
bool same ( int i , int j, int t)
{
for ( int k = 0 ; k < t ; k++)
{
if ( op[i][k] != op[j][k])
return false;
}
return true;
}
int main()
{
int tc;
cin >> tc;
while ( tc--)
{
int p , t;
cin >> p >> t;
for ( int i = 0 ; i < p ; i++)
ppl[i]=i;
opn = p;
memset(op , false, sizeof(op));
string l;
cin.ignore(10000,'\n');
while (getline(cin, l) && l.length() > 0) {
int i, j;
istringstream strm(l);
strm >> i >> j;
op[i-1][j-1] = true;
}
for ( int i = 0 ; i < p ; i++)
{
for ( int j = i+1 ; j < p ; j++)
{
if ( same (i , j, t))
{
unionF(i , j);
}
}
}
if ( tc > 0)
cout << opn << "\n"<< endl;
else
cout<< opn << "\n";
}
return 0;
}