HDU 3172:virtual Friends

Source: Internet
Author: User

Dating Network. There are a number of data, each set of data to give the N friends network, each dating network to give a M-friendship relationship, ask the dating network there are several people.

Use and check set to do, the idea is to take the right and the idea of the set, the weight of each relationship as 1, each more a person in the relationship between the root node plus 1. The problem to note is that it has a large amount of test data, CIN, cout with too much time out, so in the output to use printf. There is also a person name, so you can use map to create a map.


#include <cstdio> #include <string> #include <map> #include <iostream> using namespace std;
const int MAXN = 100005;
int FA[MAXN], NUM[MAXN];

Map <string, int> Fri;

int find (int x) {return (fa[x] = = x)? x:fa[x] = Find (Fa[x]);}
    void Union (int x, int y) {int fx, FY; FX = find (x);
    FY = find (y);
        if (FX = = FY) {printf ("%d\n", Num[fx]);
    return;
        } else {fa[fy] = FX;
        NUM[FX] + = Num[fy];
        printf ("%d\n", Num[fx]);
    return;
    }} int main () {//freopen ("In.txt", "R", stdin);
    int n;
            while (CIN >> N) {while (n--) {fri.clear ();
            int m, number = 1;
            Cin >> m;
                if (M = = 0) {cout << "0" << Endl;
            Continue;
                } while (M-) {string A, B;
                Cin >> a >> b; if (!fRi[a]) {Fri[a] = number + +;
                    Fa[fri[a]] = Fri[a];
                Num[fri[a]] = 1;
                    } if (!fri[b]) {Fri[b] = number + +;
                    FA[FRI[B]] = fri[b];
                NUM[FRI[B]] = 1;
            } Union (Fri[a], fri[b]);
}}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.