Skip to content

Instantly share code, notes, and snippets.

@Vicfred
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save Vicfred/9531736 to your computer and use it in GitHub Desktop.

Select an option

Save Vicfred/9531736 to your computer and use it in GitHub Desktop.
Virtual Friends
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2498
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cmath>
#include <cassert>
#include <climits>
using namespace std;
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
// returns S % N, where N is a power of 2
#define modulo(S, N) ((S) & (N - 1))
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) \
((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))
#define s(T) scanf("%d", &T)
#define sl(T) scanf("%lld", &T);
#define fill(a, val) memset(a,val, sizeof(a))
#define all(x) x.begin(), x.end()
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define INF 9223372036854775807
#define UINF 18446744073709551615
#define inf 2147483647
#define linf 4294967295
typedef long long int ll;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> ii;
typedef vector<ii> vii;
class UnionFind {
private:
vi p, rank, setSize;
int numSets;
public:
UnionFind(int N) {
setSize.assign(N, 1); numSets = N; rank.assign(N, 0);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) { numSets--;
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; }
else { p[x] = y; setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++; } } }
int numDisjointSets() { return numSets; }
int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
int main(int argc, char *argv[]) {
int t, f, n;
string oli, holi;
scanf("%d", &t);
while(t--) {
n = 0;
map<string,int> friends;
scanf("%d", &f);
UnionFind Virtual(1000000);
while(f--) {
cin >> oli >> holi;
if(friends[oli] == 0) {
friends[oli] = ++n;
}
if(friends[holi] == 0) {
friends[holi] = ++n;
}
Virtual.unionSet(friends[oli],friends[holi]);
cout << Virtual.sizeOfSet(friends[oli]) << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment