Posted in BFS, Kuangbin

POJ 3126 Prime Path/ kuangbin 1F

12/23/2017
Problem: http://poj.org/problem?id=3126
Solution: BFS.

#include<iostream>
#include<queue>
#include<string.h> 
using namespace std;
int mp[10000];

struct node {
	int num, cnt;
	node(int numm, int cntt) {
		num = numm;
		cnt = cntt;
	}
};

queue<node> q;

bool prime(int n) {
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
} 

void digit(node n) {
	int div = 0, rem = 0;
	int tmp = 1;	
	for (int i = 1; i <= 3; i++) {
		tmp *= 10;
		div = n.num / tmp;
		rem = n.num % (tmp / 10);
		for (int j = 0; j <= 9; j++) {
			int newNum = div * tmp + j * (tmp / 10) + rem;
			// cout << newNum << endl; 
			if (prime(newNum) && !mp[newNum]) {
				// cout << newNum << endl;
				q.push(node(newNum, n.cnt + 1)); 
				mp[newNum] = 1;
			} 
		}
	}
	rem = n.num % 1000;
	for (int j = 1; j <= 9; j++) {
		int newNum = j * 1000 + rem;
		if (prime(newNum) && !mp[newNum]) {
			// cout << newNum << endl; 
			q.push(node(newNum, n.cnt + 1));
			mp[newNum] = 1;
		}
	}
}

int main() {
	int n;
	cin >> n;
	while (n--) {
		int start = 0, end = 0;
		cin >> start >> end;
		memset(mp, 0, sizeof(mp));
		while (!q.empty()) {
			q.pop();
		}
		node head = node(start, 0);
		mp[start] = 1;
		q.push(head);
		while (!q.empty() && q.front().num != end) {
			digit(q.front());
			q.pop();
		}
		if (q.empty()) {
			cout << "Impossible" << endl; 
		} else {
			cout << q.front().cnt << endl;			
		}
	}
	return 0;
}
Posted in Graph theory

hdu 1272/ kuangbin 5-M(graph)

10/17/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1272
Solution: Use the identity that for a tree, the number of nodes should be one
more than that of edges. Pay special attention to that there could be
zero node, but the answer should still be true.
Note: Another possible solution, using dsu, is posted here: https://prionanthaspace.wordpress.com/2017/10/18/hdu-1272-kuangbin-5-m/


#include<iostream>
#include<string.h>
using namespace std;

int flag[100005] = {0};

int main() {
	int a, b;
	int cntn = 0, cnte = 0;
	while(cin >> a >> b) {
		if (a == -1 && b == -1) {
			return 0;
		} else if (!a && !b) {
			bool ans = (cntn == cnte + 1) || cntn == 0; 
			cout << (ans? "Yes": "No") << endl;
			memset(flag, 0, sizeof(flag));
			cntn = 0;
			cnte = 0;
			continue;
		}
		cnte ++;
		if (!flag[a]) {
			flag[a] = 1;
			cntn ++;
		}
		if (!flag[b]) {
			flag[b] = 1;
			cntn ++;
		}
	}
	return 0;
} 

Posted in DSU

hdu 1272/ kuangbin 5-M(dsu)

10/17/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1272
Solution: DSU. However, also need to check if all dots are connected. For that,
simply loop every dot that exists, and check if all their father are
the same.
Note: This problem would be much more easier if we consider the identity of a
tree, which is the number of dot is always one more than that of edges.
Link: https://prionanthaspace.wordpress.com/2017/10/18/hdu-1272-kuangbin-5-mgraph/


#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;

int parent[100005] = {0};
int flag[100005] = {0};

int find(int a) {
	if (parent[a] == a) {
		return a;
	} else {
		return parent[a] = find(parent[a]);
	}
}

void merge(int a, int b) {
	int p1 = find(a);
	int p2 = find(b);
	if (p1 != p2) {
		parent[p1] = p2;
	}
}

int main() {
	int a, b;
	bool ans = true;
	int maxx = 0;
	int minn = 100005;
	for (int i = 1; i <= 100005; i++) {
		parent[i] = i;
	}	
	memset(flag, 0, sizeof(flag));
	while (cin >> a >> b) {
		if (a == -1 && b == -1) {
			return 0;
		} else if (!a && !b) {
			if (ans) {
				int tmp = find(minn);
				for (int i = minn + 1; i <= maxx; i++) {
					if (flag[i] && find(i) != tmp) {
						// cout << tmp << ' ' << find(i) << endl;
						ans = false;
						break;
					}
				}
			}		
			cout << (ans? "Yes" :"No") << endl;
			maxx = 0;
			minn = 100005;			
			ans = true;
			for (int i = 1; i <= 100005; i++) {
				parent[i] = i;
			}
			memset(flag, 0, sizeof(flag));
			continue;
		}
		minn = min(minn, min(a, b));
		maxx = max(maxx, max(a, b));
		flag[a] = 1;
		flag[b] = 1;
		if (find(a) != find(b)) {
			merge(a, b);
		} else {
			ans = false;
		}
	}
	return 0;
}

Posted in DSU

HDU 1213/ kuangbin 5-C

10/16/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1213


#include<iostream>
#include<string.h>
using namespace std;

int parent[1005] = {0};
int cnt = 0;

int find(int a) {
	if (parent[a] == a) {
		return a;
	} else {
		return parent[a] = find(parent[a]);
	}
}

void merge(int a, int b) {
	int p1 = find(a);
	int p2 = find(b);
	if (p1 != p2) {
		parent[p1] = p2;
		cnt --;
	}
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		cnt =0;
		int n, m;
		cin >> n >> m;
		cnt = n;
		for (int i = 1; i <= n; i++) {
			parent[i] = i;
		}
		for (int i = 1; i <= m; i++) {
			int a, b;
			cin >> a >> b;
			if (find(a) != find(b)) {
				merge(a, b);
			}
		}
		cout << cnt << endl;
	}
	return 0;
} 

Posted in BFS, Kuangbin

POJ 1426/ kuangbin 1E

10/16/2017
Problem: http://poj.org/problem?id=1426
Solution: BFS. Manage a queue, and for each time push current number + 0 or 1 in
the end all the way untill curr is a multiple of given n. For some
mysterious reason limited to long long…


#include<iostream>
#include<queue>
using namespace std;

int main() {
	long long n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		long long ans = 0;
		queue<long long> q;
		q.push(1);
		while (!q.empty()) {
			long long cur = q.front();
			q.pop();
			if (cur % n == 0) {
				cout << cur << endl;
				break;
			} else {
				q.push(cur * 10);
				q.push(cur * 10 + 1);
			}
		}
	}
	return 0;
} 

Posted in BFS, Kuangbin

POJ 3278/ kuangbin 1C

10/13/2017
Problem: http://poj.org/problem?id=3278
Solution: For every element x in the queue, add in x-1. x+1, x*2.


#include<iostream>
#include<queue>
using namespace std;

bool v[100005];

bool inrange(int x) {
	return (x >= 0) && (x <= 100000); 
}

struct node{
	int t;
	int x;
	node(int tt, int xx) {
		t = tt;
		x = xx;
	}
};

int main() {
	int n, k;
	cin >> n >> k;
	queue<node> q;
	q.push(node(0, n));
	v[n] = true;
	while(!q.empty()) {
		node curr = q.front();
		q.pop();
		if (curr.x == k) {
			cout << curr.t;
			return 0;
		}
		if (inrange(curr.x - 1) && !v[curr.x - 1]) {
			q.push(node(curr.t + 1, curr.x - 1));
			v[curr.x - 1] = true;
		} 
		if (inrange(curr.x + 1) && !v[curr.x + 1]) {
			q.push(node(curr.t + 1, curr.x + 1));
			v[curr.x + 1] = true;
		}
		if (inrange(curr.x * 2) && !v[curr.x * 2]) {
			q.push(node(curr.t + 1, curr.x * 2));
			v[curr.x * 2] = true;
		}
	}
	return 0;
} 

Posted in BFS, Kuangbin

POJ 2251 Dungeon Master/ kuangbin 1B

10/13/2017
Problem: http://poj.org/problem?id=2251
Solution: A plain bfs searching. Only changing 2D to 3D, but the usage is the strategy is the same.


#include<iostream>
#include<string.h>
#include<cstring>
#include<queue>
using namespace std;

int x, y, z;
int xs, ys, zs, xe, ye, ze;
int step = 0;
char map[35][35][35];
bool v[35][35][35];

int dx[10] = {1, 0, 0, -1, 0, 0}; 
int dy[10] = {0, 1, 0, 0, -1, 0}; 
int dz[10] = {0, 0, 1, 0, 0, -1}; 

struct node{
	int time;
	int x, y, z;
	node(int tt, int xx, int yy, int zz) {
		time = tt;
		x = xx;
		y = yy;
		z = zz;
	}
};

bool inrange(int xx, int yy, int zz) {
	return (xx >= 0 && yy >= 0 && zz >= 0 && xx < x && yy < y && zz < z);
}

bool bfs() {
	queue<node> q;
	q.push(node(0, xs, ys, zs));
	v[xs][ys][zs] = true;
	while (!q.empty()) {
		node curr = q.front();
		q.pop();
		if (curr.x == xe && curr.y == ye && curr.z == ze) {
			step = curr.time;
			return true;
		} else {
			for (int i = 0; i < 6; i++) {
				int xx = curr.x + dx[i];
				int yy = curr.y + dy[i];
				int zz = curr.z + dz[i];
				if (inrange(xx, yy, zz) && (map[xx][yy][zz] == '.' || map[xx][yy][zz] == 'E') && !v[xx][yy][zz]) {
					q.push(node(curr.time + 1, xx, yy, zz));
					v[xx][yy][zz] = true;
				}
			}
		}
	}
	return false;
}

int main() {
	while (cin >> x >> y >> z) {
		if (x == 0 && y == 0 && z == 0) {
			return 0;
		}
		memset(map, 0, sizeof(map));
		memset(v, 0, sizeof(v));
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				cin >> map[i][j];
				for (int k = 0; k < z; k++) {
					if (map[i][j][k] == 'S') {
						xs = i;
						ys = j;
						zs = k;
					} else if (map[i][j][k] == 'E') {
						xe = i;
						ye = j;
						ze = k;
					}
				}
			}
		}
		if (bfs()) {
			cout << "Escaped in " << step << " minute(s).";
		} else {
			cout << "Trapped!";
		}
		cout << endl;
	}
	return 0;
}

Posted in Segment Tree

POJ 2528

8/18/2017
Problem: http://poj.org/problem?id=2528
Solution: Segment tree, obviously. But since there’re 10^7 bricks, segment tree
only would bring up a MLE. That’s why we are using hashing: if no
endpoints of posters lie on segment a-b, it doesn’t really matter how
long this segment is. We can simply map it to a segment of length 2.
Note: In regard to the expression “a||b”, if a is already true then b would not
be checked. That’s where I spent two days debugging..


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

struct poster {
	int l, r;
} p[10100];

int x[20200] = {0};
int hash[10000010] = {0};
int nodecnt = 0;

struct node {
	int l, r;
	bool c;
	node *pl, *pr;
};

node tree[1000005];

void build (node *root, int l, int r) {
	root->c = false;
	root->l = l;
	root->r = r;
	if (l == r) {
		return;
	}
	nodecnt++;
	root->pl = tree + nodecnt;
	nodecnt++;
	root->pr = tree + nodecnt;
	int mid = (l + r) / 2;
	build(root->pl, l, mid);
	build(root->pr, mid + 1, r);
}

bool post (node *root, int l, int r) {
	// cout << root->l << ' ' << root->r << ' ' << root->c << endl;
	if (root->c) {
		return false;
	}
	if (root->l == l && root->r == r) {
		root->c = true;
		return true;
	}
	bool b = false;
	int mid = (root->l + root->r) / 2;
	if (r <= mid) {
		b = post(root->pl, l, r);
	} else if (l > mid) {
		b = post(root->pr, l, r);
	} else {
		bool b1 = post(root->pl, l, mid);
		bool b2 = post(root->pr, mid + 1, r);
		b = b1 || b2;
	}
	if (root->pl->c && root->pr->c) {
		root->c = true;
	}
	return b;
}

int main() {
	int c;
	scanf("%d", &c);
	while (c--) {
		memset(x, 0, sizeof(x));
		memset(hash, 0, sizeof(hash));
		memset(p, 0, sizeof(p));
		memset(tree, 0, sizeof(tree));
		nodecnt = 0;
		int n;
		scanf("%d", &n);
		// cout << n << endl;
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			scanf("%d%d", &p[i].l, &p[i].r);
			x[cnt++] = p[i].l;
			x[cnt++] = p[i].r;
		} 
		sort(x, x + cnt);
		cnt = unique(x, x + cnt) - x;
//		for (int i = 0; i < cnt; i++) {
//			cout << x[i] << ' ';
//		}
		int no = 0;
		for (int i = 0; i < cnt; i++) {
//			cout << no << ' ';
			hash[x[i]] = no;
			if (i < cnt - 1) {
				if (x[i + 1] - x[i] == 1) {
					no++;
				} else {
					no += 2;
				}
			}
		}
		build(tree, 0, no);
		int ans = 0;
		for (int i = n - 1; i >= 0; i--) {
			// cout << hash[p[i].l] << ' ' << hash[p[i].r] << endl; 
			if (post(tree, hash[p[i].l], hash[p[i].r])) {
				ans++;
				// cout << i << endl;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

Posted in Segment Tree

POJ 4368 A Simple Problem with Integers

8/11/2017
Problem: http://poj.org/problem?id=3468
Solution: Record the sum for each segment. For time efficiency, here we do not
add immediately after an “add” operation. Instead, if reaching a
complete segment, simply record the inc value onto that exact point.
It would only be added to the individual points beneath when part of
the segment is queried.


#include<iostream>
#include<cstdio>
using namespace std;
int nodecnt = 0;

struct node{
	int l, r;
	node *pl, *pr;
	long long sum;
	long long inc;
};

node tree[200010];

void build (node *root, int l, int r) {
	root->l = l;
	root->r = r;
	root->sum = root->inc = 0;
	if (l == r) {
		return;
	}
	nodecnt++;
	root->pl = tree + nodecnt;
	nodecnt++;
	root->pr = tree + nodecnt;
	int mid = (l + r) / 2;
	build(root->pl, l, mid);
	build(root->pr, mid + 1, r);
}

void insert (node *root, int i, int v) {
	if (root->l == i && root->r == i) {
		root->sum = v;
		return;
	}
	root->sum += v;
	int mid = (root->l + root->r) / 2;
	if (i <= mid) {
		insert(root->pl, i, v);
	} else {
		insert(root->pr, i, v);
	}
}

void add (node *root, int l, int r, long long c) {
	// cout << l << ' ' << r << ' ' << c << endl;
	if (root->l == l && root->r == r) {
		root->inc += c;
		return;
	}
	root->sum += c * (r - l + 1);
	int mid = (root->l + root->r) / 2;
	if (r <= mid) {
		add (root->pl, l, r, c);
	} else if (l > mid) {
		add (root->pr, l, r, c);
	} else {
		add (root->pl, l, mid, c);
		add (root->pr, mid + 1, r, c);
	}
}

long long search (node *root, int l, int r) {
	// cout << l << ' ' << r << endl;
	if (root->l == l && root->r == r) {
		return root->sum + root->inc * (r - l + 1);
	}
	root->sum += (root->r - root->l + 1) * root->inc;
	int mid = (root->l + root->r) / 2;
	add (root->pl, root->l, mid, root->inc);
	add (root->pr, mid + 1, root->r, root->inc);
	root->inc = 0;
	if (r <= mid) {
		return search(root->pl, l, r);
	} else if (l > mid) {
		return search(root->pr, l, r);
	} else {
		return search(root->pl, l, mid) + search(root->pr, mid + 1, r);
	}
}

int main() {
	int n, q;
	scanf("%d%d", &n, &q);
	build(tree, 1, n);
	for (int i = 1; i <= n; i++) {
		int tmp;
		scanf("%d", &tmp);
		insert(tree, i, tmp);
	}
	for (int i = 1; i <= q; i ++) {
		char ch;
		cin >> ch;
		// scanf("%c", &ch);
		if (ch == 'Q') {
			int a, b;
			scanf("%d%d", &a, &b);
			printf("%lld\n", search(tree, a, b));
		} else {
			int a, b;
			long long c;
			scanf("%d%d%lld", &a, &b, &c);
			add(tree, a, b, c);
		}
	}
	return 0;
}


Posted in ACM-ICPC, Segment Tree

POJ 3264 Balanced Lineup

8/11/2017
Problem: http://poj.org/problem?id=3264
Solution: Record the max and min value for each segment.


#include<iostream>
#include<climits>
#include<cmath>
#include<cstdio>
using namespace std;
int mi = INT_MAX;
int ma = INT_MIN;

struct node{
	int l, r;
	int max;
	int min;
};

node t[800005] = {0};

void build (int root, int l, int r) {
	t[root].l = l;
	t[root].r = r;
	t[root].max = INT_MIN;
	t[root].min = INT_MAX;
	if (l != r) {
		build(root * 2 + 1, l, (l + r) / 2);
		build(root * 2 + 2, (l + r) / 2 + 1, r);
	}
}

void insert (int root, int i, int v) {
	if (t[root].l == t[root].r) {
		t[root].min = v;
		t[root].max = v;
		return;
	}
	t[root].min = min(t[root].min, v);
	t[root].max = max(t[root].max, v);
	if (i <= (t[root].l + t[root].r) / 2) {
		insert (root * 2 + 1, i, v);
	} else {
		insert (root * 2 + 2, i, v);
	}
}

void search (int root, int l, int r) {
	int mid = (t[root].l + t[root].r) / 2;
	if (mi <= t[root].min && ma >= t[root].max) {
		return;
	}
	if (t[root].l == l && t[root].r == r) {
		mi = min(mi, t[root].min);
		ma = max(ma, t[root].max);
		return;
	}
	if (r <= mid) {
		search (root * 2 + 1, l, r);
	} else if (l >= mid + 1) {
		search (root * 2 + 2, l, r);
	} else {
		search (root * 2 + 1, l, mid);
		search (root * 2 + 2, mid + 1, r);
	}
	return;
}

int main() {
	int n, q;
	scanf("%d%d", &n, &q);
	build(0, 1, n);
	int tmp;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &tmp);
		insert(0, i, tmp);
	}
	for (int i = 1; i <= q; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		mi = INT_MAX;
		ma = INT_MIN;
		search(0, a, b);
		printf("%d\n", ma - mi);
	}
	return 0;
}