using
System;
using
System.Collections.Generic;
class
GFG{
static
List<
int
> par =
new
List<
int
>();
static
List<List<
int
>> sz =
new
List<List<
int
>>();
static
void
init(List<
char
> nodes)
{
int
n = nodes.Count;
for
(
int
i = 0; i <= n; ++i)
{
par.Add(i);
if
(i == 0)
{
sz.Add(
new
List<
int
>(){0, 0});
}
if
(i > 0)
{
if
(nodes[i - 1] ==
'P'
)
{
sz.Add(
new
List<
int
>(){0, 1});
}
else
{
sz.Add(
new
List<
int
>(){1, 0});
}
}
}
}
static
int
parent(
int
i)
{
while
(par[i] != i)
{
i = par[i];
}
return
i;
}
static
void
union(
int
a,
int
b)
{
a = parent(a);
b = parent(b);
if
(a == b)
{
return
;
}
int
sz_a = sz[a][0] + sz[a][1];
int
sz_b = sz[b][0] + sz[b][1];
if
(sz_a < sz_b)
{
int
temp = a;
a = b;
b = temp;
}
par[b] = a;
sz[a][0] += sz[b][0];
sz[a][1] += sz[b][1];
return
;
}
static
void
influenced(List<
char
> nodes,
List<List<
int
>> connect)
{
int
n = nodes.Count;
init(nodes);
int
k = connect.Count;
for
(
int
i = 0; i < k; ++i)
{
union(connect[i][0], connect[i][1]);
}
int
ne = 0, ma = 0, pe = 0;
for
(
int
i = 1; i <= n; ++i)
{
int
x = parent(i);
if
(x == i)
{
if
(sz[i][0] == sz[i][1])
{
ne++;
}
else
if
(sz[i][0] > sz[i][1])
{
ma++;
}
else
{
pe++;
}
}
}
Console.WriteLine(
"P = "
+ pe +
"\nM = "
+
ma +
"\nN = "
+ ne);
}
static
public
void
Main()
{
List<
char
> nodes =
new
List<
char
>(){
'P'
,
'M'
,
'P'
,
'M'
,
'P'
};
List<List<
int
>> connect =
new
List<List<
int
>>();
connect.Add(
new
List<
int
>(){1, 3});
connect.Add(
new
List<
int
>(){3, 5});
connect.Add(
new
List<
int
>(){4, 5});
influenced(nodes, connect);
}
}