题目链接
题目链接
Lily is fascinated by numbers. She believes the whole world revolves around them, and that everything is connected by numbers. Her friends, Alice, Bob, Charlie and Diane, are not convinced. But she gives them an example:
Alice lives in house number 25 on her street, but that is exactly Bob’s age. Bob is born on June 4th, and Charlie was his parents’ fourth child. Finally, Diane has five fingers on her left hand, which happens to be the same as the number of toes that Bob has on his right foot!
This shows that her friends are all connected—either directly or indirectly—by numbers. But she still has to convince her family as well as her coworkers.
Given a group of n
individuals, and a set of numbers that describe each individual, help Lily come up with a proof that shows that everyone in this group is either directly or indirectly connected by numbers, or determine that this is not possible.
Input
The input consists of:
One line with an integer n
(2≤n≤2⋅105), the number of individuals in the group. The individuals are numbered from 1 to n
.
n
lines, describing the individuals in the group.
The i
th such line starts with an integer mi (1≤mi≤2⋅105), the number of numbers that describe individual i
.
The remainder of the line has mi
distinct integers di,1,…,di,mi (1≤di,j≤109 for each j), the set of numbers that describe individual i
.
It is guaranteed that the sum over all mi
is at most 2⋅105
.
Output
Output a proof in the form of n−1
lines, each of which contains three integers p, q and r, where p and q are distinct individuals that are both described by the number r
. Using only these relations, it must be possible to show that any pair of individuals in the group are connected either directly or indirectly.
If no such proof exists, output “impossible”. If there are multiple proofs, you may output any one of them.
Sample Input 1 Sample Output 1
6
2 17 10
1 5
2 10 22
3 17 22 9
2 17 8
3 9 22 16
impossible
Sample Input 2 Sample Output 2
6
2 17 10
2 5 10
2 10 22
3 17 22 9
2 17 8
3 9 22 16
1 3 10
2 3 10
3 4 22
4 5 17
4 6 9
这套题目这个人写啦博客,在这里粘一下,题意直接看他的吧(懒狗基操)。。。在1.4里面。
添加链接描述
思路:因为id是1e9的没法直接开数组或者线性跑,所以用map来直接查找其对应在那一个节点里面。(map的第一键值是id,第二键值是第几个独立的组。)然后就是一个并查集用来做prim最小生成树。最后因为是在输入过程中就会产生答案,所以我用vector来存储啦一下答案(三个值,用node作为一个节点存储)。
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int N=2e5+30;
typedef long long int ll;
map < int, int > ma;
int f[N];
void init()
{
for(int i=0;i<N;i++)
{
f[i]=i;
}
}
int getf(int x)
{
if(x==f[x])
return x;
return f[x]=getf(f[x]);
}
void Merge(int x,int y)
{
int a=getf(x);
int b=getf(y);
if(a!=b)
f[a]=b;
return ;
}
struct node
{
int a,b,c;
}q,w[N];
vector<node> vis;
int main()
{
int k,n,num=0;
cin>>n;
init();
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int x;
scanf("%d",&x);
if(!ma[x])
{
ma[x]=i;
}
else
{
int v=ma[x];
if(getf(v)!=getf(i))
{
Merge(i,v);
num++;
q.a=i;
q.b=v;
q.c=x;
vis.push_back(q);
}
}
}
}
if(num!=n-1)
{printf("impossible\n");
return 0;
}
int len=vis.size();
for(int i=0;i<len;i++)
{
q=vis[i];
printf("%d %d %d\n",q.a,q.b,q.c);
}
return 0;
}