HDU5923-Prediction

Prediction

                                                                       Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                 Total Submission(s): 824    Accepted Submission(s): 207


Problem Description
There is a graph  G=VG,EG   with   |VG|=n  and  |EG|=m , and a magic tree  T=VT,ET)  rooted at 1, which contains m vertices.

Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.

Each query includes a set  S(SVT) , and you should tell Mr. Frog the number of components in the modified graph  G=(VG,EG) , where  EG  is a set of edges in which every edge corresponds to a vertex v in magic tree T satisfying at least one of the following two conditions:

vS .
v is an ancestor of some vertices in S.

Note that the queries are independent, and namely one query will not influence another.
 

Input
The input contains several test cases and the first line of the input data is an integer T, denoting the number of test cases.

For each test case, the first line contains two integers n and m( 1n500,1m10000 ), where n is the number of vertices and m is the number of edges.

The second line contains m - 1 integers describing the magic tree, i-th integer represents the parent of the (i + 1)-th vertex.

Then the following m lines describe the edges of the graph G. Each line contains two integers u and v indicating the two ends of the edge.

The next line contains only one integer q( 1q50000 ), indicating the number of queries.

Then the following q lines represent queries,  i-th line represents the i-th query, which contains an integer  ki  followed by  ki  integers representing the set  Si .

It is guarenteed that  qi=1ki300000 .
 

Output
For each case, print a line "Case #x:", where x is the case number (starting from 1).

For each query, output a single line containing only one integer representing the answer, namely the number of components.
 

Sample Input
  
  
1 5 4 1 1 3 1 2 2 3 3 4 4 5 3 1 2 2 2 3 2 2 4
 

Sample Output
  
  
Case #1: 3 2 1
Hint
magic tree and the original graph in the sample are: In the first query, S = {2} and the modified graph G' = {{1, 2, 3, 4}, {(1, 2), (2, 3)}}, thus the number of the components in the modified graph is 3. In the second query, S = {1, 2, 3}, where 1 is the ancestor of 2 (and 3) in the magic tree, and the modified graph G'' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3, 4)}}, therefore the number of the components in the modified graph is 2. In the third query, S = {1, 2, 3, 4}, where 1 is the ancestor of 2 (and 4), 3 is the ancestor of 4, and the modified graph G' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3,4), (4, 5)}}, therefore the answer equals to 1.
 

Source
 

Recommend
wange2014
 

题意: 有一个n个节点,m条边的图和一个m个节点的树(根结点是1),树的每个节点对应图的一个边。一共有q次询问,每次询问在树中选中一些节点的集合,将这些节点和它们的父亲节点所代表的的边连起来,问图中有多少个联通块

解题思路:可以建立m个并查集,在遍历的时候预处理出每一个节点及其祖先节点的并查集。然后查询的时候只需要把这些并查集合并即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int f[10009][509], n, m;
int s[10009], nt[10009], e[10009];
int u[10009], v[10009];

int Find(int k, int x)
{
	return f[k][x] == x ? x : f[k][x] = Find(k, f[k][x]);
}

void dfs(int k, int fa)
{
	for (int i = 1; i <= n; i++) f[k][i] = f[fa][i];
	int uu = Find(k, u[k]), vv = Find(k, v[k]);
	if (uu != vv) f[k][uu] = vv;
	for (int i = s[k]; ~i; i = nt[i])
	{
		int v = e[i];
		if (v != fa) dfs(v, k);
	}
}

int main()
{
	int t, cas = 0;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d", &n, &m);
		memset(s, -1, sizeof s);
		int cnt = 1, k;
		for (int i = 2; i <= m; i++)
		{
			scanf("%d", &k);
			nt[cnt] = s[k], s[k] = cnt, e[cnt++] = i;
		}
		for (int i = 1; i <= m; i++) scanf("%d %d", &u[i], &v[i]);
		for (int i = 1; i <= n; i++) f[0][i] = i;
		dfs(1, 0);
		printf("Case #%d:\n", ++cas);
		int q;
		scanf("%d", &q);
		while (q--)
		{
			scanf("%d", &k);
			for (int i = 1; i <= n; i++) f[0][i] = i;
			int ans = 0;
			for (int i = 0; i < k; i++)
			{
				int x;
				scanf("%d", &x);
				for (int j = 1; j <= n; j++)
				{
					int xx = Find(x, j);
					if (xx != j)
					{
						int fa1 = Find(0, j), fa2 = Find(0, xx);
						if (fa1 != fa2) f[0][fa1] = fa2;
					}
				}
			}
			for (int i = 1; i <= n; i++)
				if (f[0][i] == i) ans++;
			printf("%d\n", ans);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值