Byte beating 8.25 pen questions

Today, do a little online pen questions byte beating 8.25, as if sensing the opening and development of the subject is the same.

The third question

There are millions of people every day on the vibrato, if user A and B interact with less than 3 times, we believe that A and B belong to the oil, if A and B are soybean oil, soybean oil also B and C, then A and C mutual as soybean oil, soybean oil, we define the bottle is to have groups of direct and indirect composed of friends.
Given a N * N matrix M, all user interactions on behalf of vibrato, if M [i] [j] = 5, then the i-th and j th user to interact with 5 times. The same number of user interactions to 0. Please calculate the number of all bottles of soybean oil output and the vibrato found.
Enter the first number is N, the content input matrix behind.

Since the first to see the third questions, so give the code of the third question, said the Internet is a method disjoint-set, and I have not specifically studied, is the method they want, is simply an element associated set when the total number of different elements is not associated with this range, the count index is increased again "infection" related elements, the last index number is associated with the same group index.


int main()
{
    int num;
    cin >> num;
    vector<vector<int>> input(num + 1, vector<int>(num + 1, 0));
    for (auto i = 1; i <= num; i++)
        for (auto j = 1; j <= num; j++)
        {
            int tmp;
            cin >> tmp;
            input[i][j] = tmp;
        }

    int index = 0;
    vector<int> res(num + 1, -1);//由于用户从1开始计数,vector从0开始计数,为了习惯,增加一行一列空内容。
    for (auto i = 1; i <= num; i++)
        for (auto j = i + 1; j <= num; j++)
        {
            if (input[i][j] > 2)
            {
                if (res[i] == -1)
                {
                    index++;
                    res[i] = index;
                    res[j] = index;

                }
                else
                {
                    res[j] = res[i];
                }
            }
        }
   //如果有相关联的用户,则感染其为同index,否则则将相关联的用户设置为新的index计数,向后继续传播。
    for (auto i = 1; i <= num; i++)
        if (res[i] == -1)
        {
            index++;
            res[i] = index;
        }
   //没有被感染过的用户视作单一豆油瓶,也是一个独立的计数。
    cout << index << endl;
    int z;
    cin >> z;
    return 0;
}

Guess you like

Origin www.cnblogs.com/leoyu800/p/11409641.html