-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarriageprob.cpp
More file actions
executable file
·74 lines (57 loc) · 1.11 KB
/
marriageprob.cpp
File metadata and controls
executable file
·74 lines (57 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long int
#define SIZE 2000000
#define ft first
#define sd second
#define pb push_back
ll mod = 1000000007;
ll root[SIZE], size[SIZE];
ll getroot(ll x)
{
if(x != root[x]) {
root[x] = getroot(root[x]);
}
return root[x];
}
void unionNodes(ll u, ll v)
{
ll r_u=getroot(u);
ll r_v=getroot(v);
if(size[r_u]>size[r_v])
{
root[r_v]=root[r_u];
size[r_u]+=size[r_v];
}
else
{
root[r_u]=root[r_v];
size[r_v]+=size[r_u];
}
}
int main(void)
{
ll x, y, ans=0;
cin>>x>>y;
ll q1, q2, q3, n=(x+y);
for(int i=0; i<n; i++){ root[i]=i; size[i]=1; }
ll u, v;
cin>>q1;
for(ll i=0; i<q1; i++){ cin>>u>>v; unionNodes(u-1, v-1); }
cin>>q2;
for(ll j=0; j<q2; j++){ cin>>u>>v; unionNodes(x+u-1, x+v-1); }
cin>>q3;
for(ll k=0; k<q3; k++){cin>>u>>v; unionNodes(u-1, x+v-1); }
ll total[n][2];
memset(total, 0, sizeof(total));
for(ll i=0; i<n; i++)
{
ll it=getroot(i);
if(i<x) total[it][0]++;
else total[it][1]++;
}
for(int i=0; i<n; i++)
ans+=total[i][0]*( y-total[i][1] );
cout<<ans<<"\n";
return 0;
}