-
Notifications
You must be signed in to change notification settings - Fork 0
/
cityCapers.cpp
149 lines (116 loc) · 3.25 KB
/
cityCapers.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <cstring>
#include <set>
#include <vector>
#include <algorithm>
#include <climits>
#include <utility>
#define MAX 100007
#define ll long long
int arr[MAX];
int size[MAX];
using namespace std;
struct cmp
{
bool operator() (const pair<int, int>& a, const pair<int, int>& b)
{
return a.second < b.second;
}
};
void printVector(multiset < pair<int,int>, cmp> input);
multiset < pair<int,int>, cmp > sizes;
void initialize(int N){
for(int i=1; i<=N; i++) {
arr[i] = i;
size[i] = 1;
sizes.insert(make_pair(i,1));
}
}
int root(int i){
while( arr[i] != i) {
i = arr[i];
}
return i;
}
bool find(int A,int B){
return root(A) == root(B);
}
void swap(int a,int b){
int temp = size[a];
size[a] = size[b];
size[b] = temp;
}
void union_set(int N,int A,int B){
int root_A = root(A);
int root_B = root(B);
if ( root_A == root_B )
return;
if ( size[root_B] > size[root_A] )
swap(root_A,root_B);
/*cout << "Before Union " << endl;
printVector(sizes);
cout << endl;
cout << root_A << " " << size[root_A] << endl;
cout << root_B << " " << size[root_B] << endl;
cout << endl;
*/
sizes.erase(sizes.find(make_pair(root_A,size[root_A])));
sizes.erase(sizes.find(make_pair(root_B,size[root_B])));
//cout << "Union " << endl;
//printVector(sizes);
//cout << "After union" << endl;
//cout << "here 1!" << endl;
size[root_A] += size[root_B];
arr[root_B] = root_A;
sizes.insert(make_pair(root_A,size[root_A]));
}
void printArray(int N){
for(int i=0; i<N; i++)
cout << arr[i] << " ";
cout << endl;
}
void printVector(multiset < pair<int,int>, cmp> input){
multiset < pair<int,int>, cmp> :: iterator it;
for(it = input.begin(); it!=input.end(); it++)
cout << it->first << " " << it->second << " ";
cout << endl;
}
ll getSetDifference(){
int large = (*(sizes.rbegin())).second;
int small = (*(sizes.begin())).second;
return large-small;
}
int getDifference(int N){
ll diff = 0;
ll min_item=LONG_MAX,max_item=LONG_MIN;
vector <ll> unique;
for(int i=1; i<=N; i++) {
ll count = 0;
for(int j=1; j<=N; j++) {
if(root(j) == i)
count++;
}
if (count) {
if(count > max_item)
max_item = count;
if(count < min_item)
min_item = count;
}
}
diff = max_item - min_item;
return diff;
}
int main(void){
memset(arr,0,sizeof arr);
int N,Q;
cin >> N >> Q;
initialize(N);
while(Q--) {
int i,j;
cin >> i >> j;
//cout << i << j << endl;
union_set(N,i,j);
cout << getSetDifference() << endl;
}
// printArray(N);
}