FZU 2155 盟国

 Problem 2155 盟国

 Problem Description

世界上存在着N个国家,简单起见,编号从0~N-1,假如a国和b国是盟国,b国和c国是盟国,那么a国和c国也是盟国。另外每个国家都有权宣布退盟(注意,退盟后还可以再结盟)。

定义下面两个操作:

“M X Y” :X国和Y国结盟

“S X” :X国宣布退盟

 Input

多组case。

每组case输入一个N和M (1 ≤ N ≤ 100000 , 1 ≤ M ≤ 1000000),N是国家数,M是操作数。

接下来输入M行操作

当N=0,M=0时,结束输入

 Output

对每组case输出最终有多少个联盟,格式见样例。

 Sample Input

5 6

M 0 1

M 1 2

M 1 3

S 1

M 1 2

S 3

3 1

M 1 2

0 0

 Sample Output

Case #1: 3

Case #2: 2

代码:

/**
*  解题思路: 含删除的并查集题,用一个real[]数组标记每个点的真实位置,
*  目的是删除一个点后,将他的位置移到原总数组的末尾,
*  这样原来的数组也不会被破坏。
*/
#include <stdio.h>
#include <string.h>
#define MAX 500005
int father[MAX];
int real[MAX];
int vis[MAX];
int n, m, all;
int find(int x)
{
	if (father[x] == x)
		return x;
	else
		return (father[x] = find(father[x]));
}
void merge(int a, int b)
{
	int x, y;
	x = find(a);
	y = find(b);
	if (x != y)
		father[x] = y;
}
int main()
{
	int t = 1;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		if (0 == n && 0 == m)
			return 0;
		char op;
		all = n;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++){
			father[i] = i;
			real[i] = i;
		}
		for (int i = 0; i < m; i++)
		{
			getchar();
			scanf("%c", &op);
			if ('M' == op){
				int a, b;
				scanf("%d%d", &a, &b);
				merge(real[a], real[b]);  //将真实位置相连
			}
			else
			{
				int x;
				scanf("%d", &x);
				father[all] = all;
				real[x] = all++;  // 删除x点,就把x的真实位置放到原数组末尾。
			}
		}
		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			int u = find(real[i]);
			if (0 == vis[u]){
				vis[u] = 1;
				ans++;
			}
		}
		printf("Case #%d: %d\n", t++, ans);
	}
	return 0;
}
时间: 2024-10-07 06:00:00

FZU 2155 盟国的相关文章

FZU - 2155 - 盟国 (带删除的并查集~~)

Problem 2155 盟国 Accept: 140    Submit: 464 Time Limit: 5000 mSec    Memory Limit : 32768 KB  Problem Description 世界上存在着N个国家,简单起见,编号从0~N-1,假如a国和b国是盟国,b国和c国是盟国,那么a国和c国也是盟国.另外每个国家都有权宣布退盟(注意,退盟后还可以再结盟). 定义下面两个操作: "M X Y" :X国和Y国结盟 "S X" :X国

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 1096 QS Network

QS Network Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 1096 64-bit integer IO format: %I64d      Java class name: Main In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS.

FZU 1759 欧拉函数 降幂公式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a singl

ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this boar

FZU 2112 并查集、欧拉通路

原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个联通分量,我们可以用一个并查集来维护,假设有n个联通分量,我们就需要n-1条边把他们连起来. 最后对于每个联通分量来说,我们要使它能一次走完,就是要求他是否满足欧拉通路,也就是这个联通分量中至多有2个度为奇数的点,每多出2个度为奇数的点,就多需要一条边(因为单个连通分量的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点). 1 #i

POJ 2155 Matrix (D区段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. I

FZU Problem 2102 Solve equation (数学啊 )

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2102 Problem Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in

FZU Problem 2104 Floor problem (数学啊 )

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2104 Problem Description In this problem, we have f(n,x)=Floor[n/x]. Here Floor[x] is the biggest integer such that no larger than x. For example, Floor[1.1]=Floor[1.9]=1, Floor[2.0]=2. You are given 3 posit