2016暑假集训训练2 C题 食物链

带权并查集傻逼题

用拆点方法草过。

拆点代码:

# include <stdio.h>
# include <string.h>
int set[150005], n, k, d;

int find(int x)
{
	int s, temp;
	for (s=x; set[s]>=0; s=set[s])
		;
	while (s != x) {
		temp = set[x];
		set[x] = s;
		x = temp;
	}
	return s;
}

void union_set(int x, int y)
{
	x = find(x); y = find(y);
	int temp = set[x] + set[y];
	if (x != y) {
		if (set[x] > set[y]) {
			set[x] = y;
			set[y] = temp;
		}
		else {
			set[y] = x;
			set[x] = temp;
		}
	}
}

int main (void)
{
	int x, y, cnt=0;
	scanf("%d%d",&n,&k);
	memset(set,-1,sizeof(set));
	while (k--) {
		scanf("%d%d%d",&d,&x,&y);
		if (x>n || y>n) {
			cnt++;
			continue;
		}
		if (d == 1) {
			if (find(x) == find(y+n) || find(x) == find(y+2*n)) {
				cnt++;
			}
			else {
				union_set(x,y);
				union_set(x+n,y+n);
				union_set(x+2*n,y+2*n);
			}
		}
		else {
			if (find(x) == find(y) || find(x) == find(y+2*n))
				cnt++;
			else {
				union_set(x,y+n);
				union_set(x+n,y+n*2);
				union_set(x+n*2,y);
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}

  

时间: 2024-10-15 03:08:30

2016暑假集训训练2 C题 食物链的相关文章

2016暑假集训训练2 H题 Frosh Week

求逆序数的傻逼题. 用归并排序或者树状数组或者线段树 归并排序代码: # include<stdio.h> # define MAXN 500100 int a[MAXN],b[MAXN]; long long ans; void merge(int l, int r) { int k, p, q; if(l == r) return; int mid= (l+r)>>1; merge(l,mid); merge(mid+1,r); p = l; q = mid+1; k = l;

2016暑假集训训练2 I题 Doing Homework again

傻逼贪心题. 先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序.. 然后按顺序,从截止日期开始往前找没有占用掉的时间. 如果找不到了,则加到罚分里面. # include <stdio.h> # include <stdlib.h> # include <string.h> int h[1005][2], n, visit[1005]; int comp(const void * a, const void * b) { return *(((int*)b)+

2016暑假集训训练2 N题 Play on Words

水题不解释.... # include <stdio.h> # include <string.h> int dee[26], set[26], node[26], n; char s[1002]; int find(int x) { int s, temp; for (s=x; set[s]>=0; s=set[s]) ; while (s!=x) { temp = set[x]; set[x] = s; x = temp; } return s; } void union

acm集训训练赛A题【签到题】

一.题目 Description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An intersection point is any point on the grid which is formed by t

acm集训训练赛B题【排序+模拟】

一.原题 Description Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you

[暑假集训]区域赛套题集

2014-07-03 [浙江第11届省赛]ZOJ 3785 What day is that day?  (打表找循环节) [暑假集训]区域赛套题集

2016HUAS暑假集训训练题 G - Oil Deposits

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

2016暑假集训补题系列

Codefoeces 496E(贪心+二分) 题意:一场演出有N段演奏,每段演奏都有一个音域,有M个演唱家,每个演唱家也分别有自己的音域,而且同一场演出中能演唱最多K次.问是否能够使所有的演奏都能进行,如果能应该怎样分配演唱顺序. 思路:先排个序,把每段演奏以及每个人都按照音域[L,R],R小的优先,相同的情况下,L小的优先.然后对每个人依次操作.每次操作把演奏的R小于等同于该演唱者的R的部分放入multiset中(这里要用到其自动排序和可二分查找的功能),然后二分找到集合中刚好满足演奏的L大于

2016HUAS暑假集训训练题 F - 简单计算器

Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔.没有非法表达式.当一行中只有0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位. Sample Input 1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output 3.00 13.36 分析: 本题