"Map+ dictionary order" Hdu 4039 the social Network

Source: Internet
Author: User

"Map+ dictionary order" HDU 4039 the Social network topic Link: HDU 4039 the Social network topic

Simulation of a social network system, first give you n friends, for everyone, the system will recommend to him some people to make friends, the system recommended is: his friend's friend and he is not a friend, and only recommend his friends with the most common friends, if it would recommend more than a dictionary order from small to large output, if one is not recommended output " -”



Proficiency map Map: Dictionary sequence itself the key word in the map container is stored according to the dictionary order, so it is only possible to iterate through the container, and then output the string that satisfies the test instructions.

Talk about the idea.
    • (1) First vector build, no map, map to the string mapping, equivalent to the number. Note that vector storage space is twice times the number of points, the author here to initialize WA! Qaq;
    • (2) Violence to find O (n^2), the amount of data is small: first Mark ok1[]-with a is a friend of true; then Mark Ok2, a friend of friend of a, for two laps, during maintenance of a maximum value dp-with a of the maximum value of a common friend; Finally, traversing the map, Find out if a friend of friends, and A is not a friend, and a has the most common friends, good around the mouth (ㄒoㄒ), the output can be found

Reference Code
/*====================================*|* Map + dictionary order + Graph adjacency Table storage *|\*====================================*//*author:hacker_vision*/#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )#define EPS 1e-8#define LL Long Longusing namespace  STD;Const int_max =1e3+Ten;intn,q,dp,ok2[_max<<1];BOOLok1[_max<<1];stringS1,S2; vector<int>child[_max<<1];//vector adjacency Table storage graph, twice times the number of sides ~!!  Map<string,int>mp Map<string,int>:: Iterator it;BOOLQuerystrings) {//Violent find O (n^2), V is friend of U, X is friend of V and not friend of U  intU = mp[s];//String s corresponding to the numberCLR (Ok1,0);//ok1[x] means X is not a friend   for(inti =0; i < child[u].size (); + + i)//Mark All Friends of UOk1[child[u][i]] =true; Ok1[u] =true; CLR (OK2,0);//ok2[x] means X and a have several common friendsDP =0;//DP: Has the same maximum value as a friend, and a is not a friend   for(inti =0; i < child[u].size (); + + i) {intv = child[u][i]; for(intj =0; J < Child[v].size (); + + j) {intx = Child[v][j];if(Ok1[x])Continue;//If with a is a friend, no meaning, do not have to deal withOK2[X] + +;    DP = MAX (dp,ok2[x]); }  }BOOLOK =true;//Control output format    for(it = Mp.begin (); It! = Mp.end (); + + it)The//map container itself is sorted out in the dictionary order.    if(ok1[it->second]==false&&ok2[it->second]==dp&&ok2[it->second]) {if(OK) {cout<<it->first;ok =false;}Else cout<<" "<<it->first; }if(!ok) {puts("");return true;}return false;}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge   intTCin>>T;intCNT =1; while(t--) {scanf("%d%d", &n,&q); Mp.clear (); for(inti =1; I <=2*n; + + i) child[i].clear ();//Initialize    inttop =1; for(inti =0; i < n; + + i) {Cin>>s1>>s2;if(!MP[S1]) mp[s1] = top++;if(!mp[s2]) mp[s2] = top++;//string as map mapChild[mp[s1]].push_back (Mp[s2]); Child[mp[s2]].push_back (Mp[s1]);//non-direction diagram establishment}printf("Case%d:\n", cnt++); for(inti =0; i < Q; + + i) {Cin>>s1;if(!query (S1))puts("-"); }  }return 0;}
    • Bold Ctrl + B
    • Italic Body Ctrl + I
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Map+ dictionary order" Hdu 4039 the social Network

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.