Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤104 ) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 … BK
where K is the number of birds in this picture, and Bi 's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104 .After the pictures there is a positive number Q (≤104 ) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes if the two birds belong to the same tree, or No if not.
Sample Input:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No
思路
在一张照片中选出一只鸟作为该照片中所有鸟的“father”,一个father代表一棵树。之后只要是一棵树上的鸟就归入该树的father下面。最后father数即树的数量。对不同的鸟,如果它们的father相同则说明是一棵树上的,否则是不同树。这里就用并查集来解决
#include <iostream>
#include <set>
using namespace std;
int father[10001];
set<int> birds;
set<int> trees;
void init() { for (int i = 0; i <= 10000; i++) father[i] = i; }
int find_father(int);
void Union(int, int);
int main()
{
init();
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int K, one;
cin >> K;
if (K) cin >> one;
birds.insert(one);
for (int j = 1; j < K; j++)
{
int next;
cin >> next;
birds.insert(next); //set可以自动过滤重复的数
Union(one, next);
}
}
for (set<int>::iterator it = birds.begin(); it != birds.end(); it++)
trees.insert(find_father(*it));
//几个father就有几棵树
cout << trees.size() << " " << birds.size() << endl;
int K;
cin >> K;
for (int i = 0; i < K; i++)
{
int b1, b2;
cin >> b1 >> b2;
//father相同则表示属于同一棵树
if (find_father(b1) == find_father(b2))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
int find_father(int x)
{
int n = x;
while (x != father[x])
x = father[x];
while (n != father[n])
{
int z = n;
n = father[n];
father[z] = x;
}
return x;
}
void Union(int a, int b)
{
int faa = find_father(a);
int fab = find_father(b);
if (faa != fab)
father[faa] = fab;
}
自己的做法,测试点1、3、4出错,暂时不知道原因。思路是两个散列表:一只鸟对应一棵树和一棵树对应该树上的所有鸟。每次输入一行,即一张照片上的所有鸟时,如果其中一只鸟有自己的树,说明照片上的所有鸟都属于该棵树,就将这些鸟全部输入到树-鸟
的散列表中,并且在鸟-树
的散列表中设置这些鸟对应的树。如果照片上的所有鸟都没有树,就新种一棵,然后做上述操作。
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int treeNum = 1, birdNum = 0;
map<int, int> bird; //鸟-树
map<int, vector<int> > tree; //树-鸟
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int K, treeNo = 0;
cin >> K;
vector<int> tmp;
for (int j = 0; j < K; j++)
{
int b;
cin >> b;
tmp.push_back(b);
if (bird.find(b) != bird.end()) //如果有只鸟有自己的树
treeNo = bird[b];
//说明整个照片中的鸟都属于这棵树
}
if (!treeNo)
//treeNo为0说明照片上的鸟都没有自己的树,则把它们全放在一棵新树上
treeNo = treeNum++;
for (int k = 0; k < tmp.size(); k++)
{
if (bird.find(tmp[k]) == bird.end())
{
bird[tmp[k]] = treeNo;
//给鸟对应的树编号
tree[treeNo].push_back(tmp[k]);
//且鸟归入该棵树中
}
}
}
cout << treeNum - 1 << " " << bird.size() << endl;
int Q;
cin >> Q;
for (int i = 0; i < Q; i++)
{
int b1, b2;
cin >> b1 >> b2;
if (bird[b1] == bird[b2])
cout << "Yes" << endl;
else
cout << "No" << endl;
}
system("pause");
return 0;
}