HDU 5486 Difference of Clustering 图论

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5486

题意:

给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操作。

分离:[m1,m2,m3]->[m1],[m2],[m3]

合并:分离的逆操作

不变:[m1,m2,m3]->[m1,m2,m3]

题解;

以集合为单位建图,(一个元素从集合s1到s2则建一条边连接集合s1,s2,注意要删除重边)

然后对于每个点,与它相邻的点如果入度都为1,则为分离操作,

把图转置,再跑一遍分离就是合并。

如果一个集合只有一条连向自己的边,那么说明它是1:1操作。

代码:

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

const int maxn = 1e6 + 10;

int n,_max;
map<pair<int, int>, int> mp;
vector<int> G[maxn], G2[maxn];
int in[maxn], in2[maxn];

void init() {
	_max = -1;
	mp.clear();
	for (int i = 0; i <maxn; i++) G[i].clear(),G2[i].clear();
	memset(in, 0, sizeof(in));
	memset(in2, 0, sizeof(in2));
}

int main() {
	int tc,kase=0;
	scanf("%d", &tc);
	while (tc--) {
		scanf("%d", &n);
		init();
		for (int i = 0; i < n; i++) {
			int u, v;
			_max = max(_max, u);
			_max = max(_max, v);
			scanf("%d%d", &u, &v);
			if (!mp[make_pair(u, v)]) {
				mp[make_pair(u, v)]++;
				G[u].push_back(v);
				in[v]++;
				G2[v].push_back(u);
				in2[u]++;
			}
		}
		int ans1=0, ans2=0,ans3=0;
		for (int i = 0; i <= _max; i++) {
			int su = 1;
			for (int j = 0; j < G[i].size(); j++) {
				int v = G[i][j];
				if (in[v] > 1) { su = 0; break; }
			}
			if (su) {
				if (G[i].size() == 1) ans3++;
				else if(G[i].size()>1) ans2++;
			}
		}
		for (int i = 0; i <= _max; i++) {
			int su = 1;
			for (int j = 0; j < G2[i].size(); j++) {
				int v = G2[i][j];
				if (in2[v] > 1) { su = 0; break; }
			}
			if (su) {
				if (G2[i].size() == 1);
				else if(G2[i].size()>1) ans1++;
			}
		}
		printf("Case #%d: %d %d %d\n", ++kase, ans2,ans1, ans3);
	}
	return 0;
}
时间: 2024-10-06 03:26:06

HDU 5486 Difference of Clustering 图论的相关文章

HDU 4940 Destroy Transportation system(图论)

这道题目当时做的时候想的是,如果找到一个点他的d值之和大于 d+b值之和,就可以.竟然就这么过了啊.不过题解上还有一种做法,好像有点难.以后在补一补那种做法吧. Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 289    Accepted Submission(s): 1

HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an

HDU 4715 Difference Between Primes (素数表+二分)

Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte

hdu 4598 Difference

Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 621    Accepted Submission(s): 161 Problem Description A graph is a difference if every vertex vi can be assigned a real number ai and

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=

HDU 5936 Difference(折半搜索(中途相遇法))

http://acm.hdu.edu.cn/showproblem.php?pid=5936 题意: 定义了这样一种算法,现在给出x和k的值,问有多少个y是符合条件的. 思路: y最多只有10位,再多x就是负的了. 这样的话可以将y分为前后两部分,我们先枚举后5位的情况,然后再枚举前5位的情况,通过二分查找找到匹配的项,这样就大大的降低了时间复杂度. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring&g

hdu 4715 Difference Between Primes (二分查找)

Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr

POJ 2253 Difference of Clustering

题意:给出一堆点,求从起点到终点的所有通路中相邻点的距离的最大值的最小值.(意思就是自己百度吧……) 解法:用相邻点的最大值作为权值代替路径的距离跑最短路或者最小生成树.然后我写了一个我以为是优化过的dijkstra但好像是prim的东西- -啊差不多啦…… 总之用优先队列维护权值进行广搜……然后交G++一直wa也不知道为啥……交了C++就过了…… 代码: #include<stdio.h> #include<iostream> #include<algorithm>

HDU 4421 Bit Magic (图论-2SAT)

Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my ac