Skip to content

Instantly share code, notes, and snippets.

@jackyyf
Created August 8, 2014 18:57
Show Gist options
  • Save jackyyf/1fad50d2c917edd07780 to your computer and use it in GitHub Desktop.
Save jackyyf/1fad50d2c917edd07780 to your computer and use it in GitHub Desktop.
Gist by paste.py @ 2014-08-09 02:56:53.839974
#include <bits/stdc++.h>
using namespace std;
namespace Solve {
template<class T> inline vector<T>& operator<< (vector<T> &storage, const T& data) {
storage.push_back(data);
return storage;
}
template<class T> inline void operator!(const vector<T>& vect) {
for(const T& data : vect) {
cout << data << " ";
}
}
template<class T> inline queue<T>& operator<< (queue<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline stack<T>& operator<< (stack<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline queue<T>& operator>> (queue<T> &storage, T& data) {
data = storage.front();
storage.pop();
return storage;
}
template<class T> inline stack<T>& operator>> (stack<T> &storage, T& data) {
data = storage.top();
storage.pop();
return storage;
}
int Qual[131072];
void main(void) {
ios::sync_with_stdio(false);
register int i, j;
int N;
cin >> N;
memset(Qual, 0xC0, sizeof Qual);
for(i = 0; i < N; ++ i) {
int x, y;
cin >> x >> y;
Qual[x] = max(Qual[x], y);
}
int cur_qual = 0x3F3F3F3F;
for(i = N; i > 0; -- i) {
if (Qual[i] < 0) continue;
if (cur_qual < Qual[i]) {
cout << "Happy Alex" << endl;
return ;
}
cur_qual = Qual[i];
}
cout << "Poor Alex" << endl;
}
}
int main(void) {
Solve::main();
}
#include <bits/stdc++.h>
using namespace std;
namespace Solve {
template<class T> inline vector<T>& operator<< (vector<T> &storage, const T& data) {
storage.push_back(data);
return storage;
}
template<class T> inline void operator!(const vector<T>& vect) {
for(const T& data : vect) {
cout << data << " ";
}
}
template<class T> inline queue<T>& operator<< (queue<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline stack<T>& operator<< (stack<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline queue<T>& operator>> (queue<T> &storage, T& data) {
data = storage.front();
storage.pop();
return storage;
}
template<class T> inline stack<T>& operator>> (stack<T> &storage, T& data) {
data = storage.top();
storage.pop();
return storage;
}
char data[131072];
char *ptr;
const int ans[4] = {
1 + 1 + 1 + 1,
1 + 2 + 3 + 4,
1 + 4 + 4 + 1,
1 + 3 + 2 + 4,
};
void main(void) {
ios::sync_with_stdio(false);
register int i, j;
memset(data, 32, sizeof(data));
ptr = data + 10;
cin >> ptr;
i = strlen(ptr);
int x;
sscanf(ptr + i - 2, "%d", &x);
printf("%d\n", ans[x & 3] % 5);
}
}
int main(void) {
Solve::main();
}
#include <bits/stdc++.h>
using namespace std;
namespace Solve {
template<class T> inline vector<T>& operator<< (vector<T> &storage, const T& data) {
storage.push_back(data);
return storage;
}
template<class T> inline void operator!(const vector<T>& vect) {
for(const T& data : vect) {
cout << data << " ";
}
}
template<class T> inline queue<T>& operator<< (queue<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline stack<T>& operator<< (stack<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline queue<T>& operator>> (queue<T> &storage, T& data) {
data = storage.front();
storage.pop();
return storage;
}
template<class T> inline stack<T>& operator>> (stack<T> &storage, T& data) {
data = storage.top();
storage.pop();
return storage;
}
long long _f[131072];
long long *f = _f + 10;
int cnt[131072];
void main(void) {
ios::sync_with_stdio(false);
register int i, j;
int N;
cin >> N;
memset(cnt, 0, sizeof cnt);
for(i = 0; i < N; ++ i) {
int x;
cin >> x;
++ cnt[x];
}
for(i = 1; i <= 100000; ++ i) {
f[i] = max(f[i - 2] + ((long long) i) * cnt[i], f[i - 1]);
}
cout << f[100000] << endl;
}
}
int main(void) {
Solve::main();
}
#include <bits/stdc++.h>
using namespace std;
namespace Solve {
template<class T> inline vector<T>& operator<< (vector<T> &storage, const T& data) {
storage.push_back(data);
return storage;
}
template<class T> inline void operator!(const vector<T>& vect) {
for(const T& data : vect) {
cout << data << " ";
}
}
template<class T> inline queue<T>& operator<< (queue<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline stack<T>& operator<< (stack<T> &storage, const T& data) {
storage.push(data);
return storage;
}
template<class T> inline queue<T>& operator>> (queue<T> &storage, T& data) {
data = storage.front();
storage.pop();
return storage;
}
template<class T> inline stack<T>& operator>> (stack<T> &storage, T& data) {
data = storage.top();
storage.pop();
return storage;
}
bool visited[131072];
vector<int> edge[131072];
int fa[131072];
int depth[131072];
int val[131072];
int far, dist;
queue<int> Q;
int Fa(const int &x) {
if (x != fa[x]) return fa[x] = Fa(fa[x]);
return x;
}
void merge(const int &x, const int &y) {
if (Fa(x) == Fa(y)) return;
int va = val[Fa(x)], vb = val[Fa(y)];
int nv = max(max(va, vb), ((1 + va) >> 1) + ((1 + vb) >> 1) + 1);
fa[Fa(y)] = Fa(x);
val[Fa(x)] = nv;
}
void getfar(const int &who) {
Q << who;
memset(depth, -1, sizeof depth);
depth[who] = 0;
far = who, dist = 0;
int now;
while(Q.size()) {
Q >> now;
for(int &next : edge[now]) {
if (depth[next] < 0) {
depth[next] = depth[now] + 1;
if (depth[next] > dist) {
dist = depth[far = next];
}
Q << next;
}
}
}
}
int getline() {
Q << far;
memset(depth, -1, sizeof depth);
depth[far] = 0;
dist = 0;
int now;
while(Q.size()) {
Q >> now;
fa[now] = far; /* merge */
visited[now] = true;
for(int &next : edge[now]) {
if (depth[next] < 0) {
depth[next] = depth[now] + 1;
Q << next;
if (depth[next] > dist) dist = depth[next];
}
}
}
return dist;
}
void main(void) {
ios::sync_with_stdio(false);
register int i, j;
int n, m, q;
cin >> n >> m >> q;
for(i = 0; i < m; ++ i) {
int x, y;
cin >> x >> y;
edge[x] << y;
edge[y] << x;
}
for(i = 1; i <= n; ++ i) fa[i] = i;
for(i = 1; i <= n; ++ i) {
if (!visited[i]) {
getfar(i);
int v = getline();
val[Fa(i)] = v;
}
}
for(i = 0; i < q; ++ i) {
int t;
cin >> t;
if (t == 1) {
int who;
cin >> who;
cout << val[Fa(who)] << endl;
continue;
}
// t == 2
int x, y;
cin >> x >> y;
merge(x, y);
}
}
}
int main(void) {
Solve::main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment