zoukankan      html  css  js  c++  java
  • Firetrucks Are Red

    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 nn 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 nn (2n21052≤n≤2⋅105), the number of individuals in the group. The individuals are numbered from 11 to nn.

    • nn lines, describing the individuals in the group.

      The iith such line starts with an integer mimi (1mi21051≤mi≤2⋅105), the number of numbers that describe individual ii.

      The remainder of the line has mimi distinct integers di,1,,di,midi,1,…,di,mi (1di,j1091≤di,j≤109 for each jj), the set of numbers that describe individual ii.

    It is guaranteed that the sum over all mimi is at most 21052⋅105.

    Output

    Output a proof in the form of n1n−1 lines, each of which contains three integers pp, qq and rr, where pp and qq are distinct individuals that are both described by the number rr. 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 1Sample 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 2Sample 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
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 998244353;
    const int N = 2e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m*n / gcd(m, n);
    }
    
    int f[200010];
    int find(int x) {
        return f[x] == x ? x : f[x] = find(f[x]);
    }
    bool add(int x, int y) {
        int fx = find(x), fy = find(y);
        if (fx != fy) {
            f[fx] = fy;
            return true;
        }
        return false;
    }
    int main() {
        int n;
        scanf("%d", &n);
        map<int, vector<int>> m;
        for (int i = 1; i <= n; i++)
        {
            f[i] = i;
            int j, k;
            scanf("%d", &j);
            while (j--) {
                scanf("%d", &k);
                m[k].push_back(i);
            }
        }
        vector<pair<PII, int>> ans;
        for (auto i : m) {
            for (int j = 1; j < i.second.size(); j++)
                if (add(i.second[j - 1], i.second[j]))
                    ans.push_back({ { i.second[j - 1], i.second[j] }, i.first });
        }
        if (ans.size() != n - 1)
            printf("impossible
    ");
        else
            for (auto i : ans)
                printf("%d %d %d
    ", i.first.first, i.first.second, i.second);
    }
  • 相关阅读:
    求n(n>=2)以内的质数/判断一个数是否质数——方法+细节优化
    poj1185炮兵阵地 正确代码及错误代码分析
    运算符优先级的几点注意
    mod(%)之规律(除数与被除数的正负分析)
    css背景
    Content-Type
    vue数组的增改和v-model的绑定使用Demo
    python open函数关于w+ r+ 读写操作的理解(转)
    http状态码解释
    cookie与token对比(转)
  • 原文地址:https://www.cnblogs.com/dealer/p/12859963.html
Copyright © 2011-2022 走看看